[refactor] improve data module

This commit is contained in:
Kuldeep M 2019-07-16 20:06:06 +01:00
parent 4c5296b816
commit 90bb0f010d
3 changed files with 78 additions and 85 deletions

View File

@ -2,70 +2,16 @@ var data = (function() {
var _saveName = "nitghTab"; var _saveName = "nitghTab";
var set = function(key, data) { var mod = {};
localStorage.setItem(key, data);
};
var get = function(key) { mod.import = function() {
return localStorage.getItem(key); var fileList = helper.e(".control-data-import").files;
}; if (fileList.length > 0) {
_validateJsonFile(fileList);
var remove = function(key) {
localStorage.removeItem(key);
};
var save = function() {
var data = {
nighttab: true,
version: version.get(),
state: state.get(),
bookmarks: bookmarks.get()
};
set(_saveName, JSON.stringify(data));
};
var wipe = function() {
remove(_saveName);
};
var load = function() {
return JSON.parse(get(_saveName));
};
var restore = function(data) {
if (data) {
if (!("version" in data) || data.version != version.get()) {
console.log("data version " + data.version + " found less than current");
data = update.run(data);
set(_saveName, JSON.stringify(data));
} else {
console.log("data version " + version.get() + " no need to run update");
set(_saveName, JSON.stringify(data));
};
} else {
console.log("no data found to load");
}; };
}; };
var clearData = function() { mod.export = function() {
var clearContent = helper.node("div");
var para1 = helper.node("p:Are you sure you want to clear all nightTab Bookmarks and Settings?. nightTab will restore to the default state.");
var para2 = helper.node("p:This can not be undone.");
clearContent.appendChild(para1);
clearContent.appendChild(para2);
modal.render({
heading: "Clear all nightTab data?",
content: clearContent,
successAction: function() {
wipe();
location.reload();
},
actionText: "Clear all data",
size: "small"
});
};
var exportData = function() {
var tempAchor = helper.node("a"); var tempAchor = helper.node("a");
var timeStamp = helper.getDateTime(); var timeStamp = helper.getDateTime();
var _timeStampPrefix = function(value) { var _timeStampPrefix = function(value) {
@ -81,16 +27,42 @@ var data = (function() {
timeStamp.month = _timeStampPrefix(timeStamp.month + 1); timeStamp.month = _timeStampPrefix(timeStamp.month + 1);
timeStamp.year = _timeStampPrefix(timeStamp.year); timeStamp.year = _timeStampPrefix(timeStamp.year);
timeStamp = timeStamp.hours + " " + timeStamp.minutes + " " + timeStamp.seconds + " - " + timeStamp.date + "." + timeStamp.month + "." + timeStamp.year; timeStamp = timeStamp.hours + " " + timeStamp.minutes + " " + timeStamp.seconds + " - " + timeStamp.date + "." + timeStamp.month + "." + timeStamp.year;
console.log(timeStamp);
var fileName = "nightTab backup - " + timeStamp + ".json"; var fileName = "nightTab backup - " + timeStamp + ".json";
var exportData = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(load())); var data = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(load()));
tempAchor.setAttribute("href", exportData); tempAchor.setAttribute("href", data);
tempAchor.setAttribute("download", fileName); tempAchor.setAttribute("download", fileName);
helper.e("html").appendChild(tempAchor); helper.e("html").appendChild(tempAchor);
tempAchor.click(); tempAchor.click();
tempAchor.remove(); tempAchor.remove();
}; };
mod.restore = function(data) {
if (data) {
if (!("version" in data) || data.version != version.get()) {
console.log("data version " + data.version + " found less than current");
data = update.run(data);
mod.set(_saveName, JSON.stringify(data));
} else {
console.log("data version " + version.get() + " no need to run update");
mod.set(_saveName, JSON.stringify(data));
};
} else {
console.log("no data found to load");
};
};
mod.set = function(key, data) {
localStorage.setItem(key, data);
};
mod.get = function(key) {
return localStorage.getItem(key);
};
mod.remove = function(key) {
localStorage.removeItem(key);
};
var bind = {}; var bind = {};
bind.feedback = { bind.feedback = {
@ -117,6 +89,24 @@ var data = (function() {
var render = {}; var render = {};
render.clear = function() {
var clearContent = helper.node("div");
var para1 = helper.node("p:Are you sure you want to clear all nightTab Bookmarks and Settings?. nightTab will restore to the default state.");
var para2 = helper.node("p:This can not be undone.");
clearContent.appendChild(para1);
clearContent.appendChild(para2);
modal.render({
heading: "Clear all nightTab data?",
content: clearContent,
successAction: function() {
wipe();
render.reload();
},
actionText: "Clear all data",
size: "small"
});
};
render.reload = function() { render.reload = function() {
location.reload(); location.reload();
}; };
@ -170,16 +160,6 @@ var data = (function() {
} }
}; };
var importData = function() {
// get files from input
var fileList = helper.e(".control-data-import").files;
// if file was added
if (fileList.length > 0) {
// validate the file
_validateJsonFile(fileList);
};
};
var _validateJsonFile = function(fileList) { var _validateJsonFile = function(fileList) {
// make new file reader // make new file reader
var reader = new FileReader(); var reader = new FileReader();
@ -191,7 +171,7 @@ var data = (function() {
if (JSON.parse(event.target.result).nighttab) { if (JSON.parse(event.target.result).nighttab) {
render.feedback.clear(); render.feedback.clear();
render.feedback.success(fileList[0].name, function() { render.feedback.success(fileList[0].name, function() {
restore(JSON.parse(event.target.result)); mod.restore(JSON.parse(event.target.result));
render.reload(); render.reload();
}); });
render.input.clear(); render.input.clear();
@ -212,23 +192,36 @@ var data = (function() {
reader.readAsText(fileList.item(0)); reader.readAsText(fileList.item(0));
}; };
var save = function() {
mod.set(_saveName, JSON.stringify({
nighttab: true,
version: version.get(),
state: state.get(),
bookmarks: bookmarks.get()
}));
};
var load = function() {
return JSON.parse(mod.get(_saveName));
};
var wipe = function() {
mod.remove(_saveName);
render.reload();
};
var init = function() { var init = function() {
restore(data.load()); mod.restore(data.load());
render.feedback.empty(); render.feedback.empty();
}; };
return { return {
init: init, init: init,
mod: mod,
render: render,
save: save, save: save,
remove: remove,
set: set,
get: get,
load: load, load: load,
wipe: wipe, wipe: wipe
restore: restore,
importData: importData,
exportData: exportData,
clearData: clearData
}; };
})(); })();

View File

@ -1,6 +1,6 @@
var version = (function() { var version = (function() {
var current = "3.43.0"; var current = "3.44.0";
var compare = function(a, b) { var compare = function(a, b) {
var pa = a.split("."); var pa = a.split(".");

View File

@ -2,7 +2,7 @@
"name": "nightTab", "name": "nightTab",
"short_name": "nightTab", "short_name": "nightTab",
"description": "A neutral new tab page accented with a chosen colour. Customise the layout, style, background and bookmarks in nightTab.", "description": "A neutral new tab page accented with a chosen colour. Customise the layout, style, background and bookmarks in nightTab.",
"version": "3.43.0", "version": "3.44.0",
"manifest_version": 2, "manifest_version": 2,
"chrome_url_overrides": { "chrome_url_overrides": {
"newtab": "index.html" "newtab": "index.html"