Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 588fc36

Browse files
committed
[feature] adding update module
1 parent c9f05f5 commit 588fc36

11 files changed

Lines changed: 142 additions & 66 deletions

File tree

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ <h1 class="menu-header">Page</h1>
286286

287287
<script src="js/helper.js"></script>
288288
<script src="js/data.js"></script>
289+
<script src="js/update.js"></script>
289290
<script src="js/state.js"></script>
290291
<script src="js/bookmarks.js"></script>
291292
<script src="js/control.js"></script>

js/bookmarks.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ var bookmarks = (function() {
215215
get: get,
216216
add: add,
217217
edit: edit,
218-
remove: remove,
219-
restore: restore
218+
remove: remove
220219
};
221220

222221
})();

js/control.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ var control = (function() {
108108
_layout();
109109
};
110110

111-
var _dependents = function(options) {
111+
var _dependents = function() {
112112
var _date = function() {
113113
var activeCount = 0;
114114
var toCheck = [state.get().header.date.show.date, state.get().header.date.show.day, state.get().header.date.show.month, state.get().header.date.show.year];

js/data.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var data = (function() {
1616

1717
var save = function() {
1818
var data = {
19+
version: version.get(),
1920
state: state.get(),
2021
bookmarks: bookmarks.get()
2122
};
@@ -28,17 +29,37 @@ var data = (function() {
2829
return data;
2930
};
3031

32+
var _checkForSavedData = function(data) {
33+
if (data) {
34+
console.log("data loaded");
35+
if (!("version" in data) || data.version != version.get()) {
36+
console.log("data version found less than current");
37+
data = update.render(data);
38+
set(saveName, JSON.stringify(data));
39+
} else {
40+
console.log("data version =", version.get());
41+
};
42+
} else {
43+
console.log("no data found to load");
44+
};
45+
};
46+
47+
var init = function() {
48+
_checkForSavedData(load());
49+
};
50+
3151
var wipe = function() {
3252
clear(saveName);
3353
};
3454

3555
return {
56+
init: init,
3657
save: save,
3758
clear: clear,
3859
set: set,
3960
get: get,
40-
wipe: wipe,
41-
load: load
61+
load: load,
62+
wipe: wipe
4263
};
4364

4465
})();

js/helper.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ var helper = (function() {
6969
return object;
7070
};
7171

72-
var applyOptions = function(defaultOptions, options) {
73-
if (defaultOptions && options) {
74-
if (options) {
75-
for (var key in options) {
76-
if (key in defaultOptions) {
77-
defaultOptions[key] = options[key];
72+
var applyOptions = function(options, override) {
73+
if (options && override) {
74+
if (override) {
75+
for (var key in override) {
76+
if (key in options) {
77+
options[key] = override[key];
7878
};
7979
};
8080
};
81-
return defaultOptions;
81+
return options;
8282
} else {
8383
return null;
8484
};
@@ -156,75 +156,75 @@ var helper = (function() {
156156
return array;
157157
};
158158

159-
function setObject(options) {
160-
var defaultOptions = {
159+
function setObject(override) {
160+
var options = {
161161
path: null,
162162
object: null,
163163
newValue: null
164164
};
165-
if (options) {
166-
var defaultOptions = applyOptions(defaultOptions, options);
165+
if (override) {
166+
var options = applyOptions(options, override);
167167
};
168-
var address = _makeAddress(defaultOptions.path);
168+
var address = _makeAddress(options.path);
169169
var _setData = function() {
170170
while (address.length > 1) {
171171
// shift off and store the first key
172172
var currentKey = address.shift();
173173
// if the key is not found make a new object
174-
if (!(currentKey in defaultOptions.object)) {
174+
if (!(currentKey in options.object)) {
175175
// make an empty object in the current object level
176176
if (isNaN(currentKey)) {
177-
defaultOptions.object[currentKey] = {};
177+
options.object[currentKey] = {};
178178
} else {
179-
defaultOptions.object[currentKey] = [];
179+
options.object[currentKey] = [];
180180
};
181181
};
182182
// drill down the object with the first key
183-
defaultOptions.object = defaultOptions.object[currentKey];
183+
options.object = options.object[currentKey];
184184
};
185185
var finalKey = address.shift();
186-
defaultOptions.object[finalKey] = defaultOptions.newValue;
186+
options.object[finalKey] = options.newValue;
187187
};
188-
if (defaultOptions.object != null && defaultOptions.path != null && defaultOptions.newValue != null) {
188+
if (options.object != null && options.path != null && options.newValue != null) {
189189
_setData();
190190
} else {
191191
return false;
192192
};
193193
};
194194

195-
function getObject(options) {
196-
var defaultOptions = {
195+
function getObject(override) {
196+
var options = {
197197
object: null,
198198
path: null
199199
};
200-
if (options) {
201-
var defaultOptions = applyOptions(defaultOptions, options);
200+
if (override) {
201+
var options = applyOptions(options, override);
202202
};
203-
var address = _makeAddress(defaultOptions.path);
203+
var address = _makeAddress(options.path);
204204
var _getData = function() {
205205
while (address.length > 1) {
206206
// shift off and store the first key
207207
var currentKey = address.shift();
208208
// if the key is not found make a new object
209-
if (!(currentKey in defaultOptions.object)) {
209+
if (!(currentKey in options.object)) {
210210
// make an empty object in the current object level
211211
if (isNaN(currentKey)) {
212-
defaultOptions.object[currentKey] = {};
212+
options.object[currentKey] = {};
213213
} else {
214-
defaultOptions.object[currentKey] = [];
214+
options.object[currentKey] = [];
215215
};
216216
};
217217
// drill down the object with the first key
218-
defaultOptions.object = defaultOptions.object[currentKey];
218+
options.object = options.object[currentKey];
219219
};
220220
var finalKey = address.shift();
221-
if (!(finalKey in defaultOptions.object)) {
221+
if (!(finalKey in options.object)) {
222222
return "";
223223
} else {
224-
return defaultOptions.object[finalKey];
224+
return options.object[finalKey];
225225
};
226226
};
227-
if (defaultOptions.object != null && defaultOptions.path != null) {
227+
if (options.object != null && options.path != null) {
228228
return _getData();
229229
} else {
230230
return false;

js/init.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
// log version
22
console.log("nightTab v", version.get(), "loaded");
33

4+
// check for old versions
5+
data.init();
6+
47
// bind and update controls
58
// render states
69
state.init();
710

8-
// close menu if left open
9-
menu.init();
10-
1111
// restore bookmarks
1212
bookmarks.init();
1313

14+
// close menu if left open
15+
menu.init();
16+
1417
// render input color value
1518
// render css accent var
1619
theme.init();

js/modal.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ var modal = (function() {
1111
};
1212
};
1313

14-
var render = function(options) {
15-
var defaultOptions = {
14+
var render = function(override) {
15+
var options = {
1616
heading: "Modal",
1717
content: "Body",
1818
action: null,
1919
actionText: "OK",
2020
cancelText: "Cancel",
2121
size: "medium"
2222
};
23-
if (options) {
24-
defaultOptions = helper.applyOptions(defaultOptions, options);
23+
if (override) {
24+
options = helper.applyOptions(options, override);
2525
};
2626
var makeModal = function() {
2727
var body = helper.e("body");
@@ -32,11 +32,11 @@ var modal = (function() {
3232
var modalWrapper = document.createElement("div");
3333
modalWrapper.setAttribute("class", "modal-wrapper");
3434
var modal = document.createElement("div");
35-
if (defaultOptions.size == "large") {
35+
if (options.size == "large") {
3636
modal.setAttribute("class", "modal modal-large");
37-
} else if (defaultOptions.size == "small") {
37+
} else if (options.size == "small") {
3838
modal.setAttribute("class", "modal modal-small");
39-
} else if (defaultOptions.size) {
39+
} else if (options.size) {
4040
modal.setAttribute("class", "modal");
4141
};
4242
modal.destroy = function() {
@@ -59,30 +59,30 @@ var modal = (function() {
5959
var actionButton = document.createElement("button");
6060
actionButton.setAttribute("tabindex", "1");
6161
actionButton.setAttribute("class", "button button-primary button-block");
62-
actionButton.textContent = defaultOptions.actionText;
62+
actionButton.textContent = options.actionText;
6363
var cancelButton = document.createElement("button");
6464
cancelButton.setAttribute("tabindex", "1");
6565
cancelButton.setAttribute("class", "button button-primary button-block");
66-
cancelButton.textContent = defaultOptions.cancelText;
66+
cancelButton.textContent = options.cancelText;
6767
modalControls.appendChild(cancelButton);
6868
modalControls.appendChild(actionButton);
69-
if (defaultOptions.heading != null) {
69+
if (options.heading != null) {
7070
var modalHeading = document.createElement("h1");
7171
modalHeading.setAttribute("tabindex", "1");
7272
modalHeading.setAttribute("class", "modal-heading");
73-
modalHeading.textContent = defaultOptions.heading;
73+
modalHeading.textContent = options.heading;
7474
modalBody.appendChild(modalHeading);
7575
};
76-
if (defaultOptions.content) {
77-
if (typeof defaultOptions.content == "string") {
76+
if (options.content) {
77+
if (typeof options.content == "string") {
7878
var container = document.createElement("div");
7979
container.setAttribute("class", "container");
8080
var para = document.createElement("p");
81-
para.textContent = defaultOptions.content;
81+
para.textContent = options.content;
8282
container.appendChild(para);
8383
modalBody.appendChild(container);
8484
} else {
85-
modalBody.appendChild(defaultOptions.content);
85+
modalBody.appendChild(options.content);
8686
};
8787
};
8888
modalWrapper.appendChild(modalBody);
@@ -99,8 +99,8 @@ var modal = (function() {
9999
actionButton.addEventListener("click", function(event) {
100100
this.destroy();
101101
shade.destroy();
102-
if (defaultOptions.action) {
103-
defaultOptions.action();
102+
if (options.action) {
103+
options.action();
104104
};
105105
}.bind(modal), false);
106106
cancelButton.addEventListener("click", function(event) {

js/shade.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ var shade = (function() {
1111
};
1212
};
1313

14-
var render = function(options) {
15-
var defaultOptions = {
14+
var render = function(override) {
15+
var options = {
1616
action: null,
1717
includeHeader: false
1818
};
19-
if (options) {
20-
defaultOptions = helper.applyOptions(defaultOptions, options);
19+
if (override) {
20+
options = helper.applyOptions(options, override);
2121
};
2222
var _destroy_previousShade = function() {
2323
if (previousShade != null) {
@@ -28,7 +28,7 @@ var shade = (function() {
2828
var body = helper.e("body");
2929
var shade = document.createElement("div");
3030
shade.setAttribute("class", "shade");
31-
if (defaultOptions.includeHeader) {
31+
if (options.includeHeader) {
3232
helper.addClass(shade, "m-shade-top");
3333
};
3434
shade.destroy = function() {
@@ -49,8 +49,8 @@ var shade = (function() {
4949
}.bind(shade), false);
5050
shade.addEventListener("click", function() {
5151
this.destroy();
52-
if (defaultOptions.action) {
53-
defaultOptions.action();
52+
if (options.action) {
53+
options.action();
5454
};
5555
}, false);
5656
previousShade = shade;

js/state.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ var state = (function() {
116116
return {
117117
init: init,
118118
get: get,
119-
change: change,
120-
restore: restore
119+
change: change
121120
};
122121

123122
})();

0 commit comments

Comments
 (0)