2018-12-26 08:45:53 +01:00
|
|
|
var data = (function() {
|
|
|
|
|
2019-06-20 14:00:19 +02:00
|
|
|
var _saveName = "nitghTab";
|
2018-12-26 08:45:53 +01:00
|
|
|
|
|
|
|
var set = function(key, data) {
|
|
|
|
localStorage.setItem(key, data);
|
|
|
|
};
|
|
|
|
|
|
|
|
var get = function(key) {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
};
|
|
|
|
|
2019-01-13 23:54:48 +01:00
|
|
|
var remove = function(key) {
|
2018-12-26 08:45:53 +01:00
|
|
|
localStorage.removeItem(key);
|
|
|
|
};
|
|
|
|
|
|
|
|
var save = function() {
|
2019-06-17 11:23:57 +02:00
|
|
|
var data = {
|
2019-06-20 14:00:19 +02:00
|
|
|
nighttab: true,
|
2019-06-17 11:23:57 +02:00
|
|
|
version: version.get(),
|
|
|
|
state: state.get(),
|
|
|
|
bookmarks: bookmarks.get()
|
2019-06-17 01:08:35 +02:00
|
|
|
};
|
2019-06-20 14:00:19 +02:00
|
|
|
set(_saveName, JSON.stringify(data));
|
2018-12-26 08:45:53 +01:00
|
|
|
};
|
|
|
|
|
2019-01-13 23:54:48 +01:00
|
|
|
var wipe = function() {
|
2019-06-20 14:00:19 +02:00
|
|
|
remove(_saveName);
|
2019-01-13 23:54:48 +01:00
|
|
|
};
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
var load = function() {
|
2019-06-20 14:00:19 +02:00
|
|
|
return JSON.parse(get(_saveName));
|
2018-12-26 08:45:53 +01:00
|
|
|
};
|
|
|
|
|
2019-06-20 14:00:19 +02:00
|
|
|
var restore = function(data) {
|
2019-01-06 16:47:08 +01:00
|
|
|
if (data) {
|
2019-06-17 11:23:57 +02:00
|
|
|
if (!("version" in data) || data.version != version.get()) {
|
|
|
|
console.log("data version " + data.version + " found less than current");
|
|
|
|
data = update.run(data);
|
2019-06-20 14:00:19 +02:00
|
|
|
set(_saveName, JSON.stringify(data));
|
2019-06-17 11:23:57 +02:00
|
|
|
} else {
|
|
|
|
console.log("data version " + version.get() + " no need to run update");
|
2019-06-20 14:00:19 +02:00
|
|
|
set(_saveName, JSON.stringify(data));
|
2019-01-06 16:47:08 +01:00
|
|
|
};
|
2019-06-17 11:23:57 +02:00
|
|
|
} else {
|
|
|
|
console.log("no data found to load");
|
2019-01-06 16:47:08 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-06-20 14:00:19 +02:00
|
|
|
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 exportData = function() {
|
|
|
|
var tempAchor = helper.node("a");
|
|
|
|
var timeStamp = helper.getDateTime();
|
|
|
|
var _timeStampPrefix = function(value) {
|
|
|
|
if (value < 10) {
|
|
|
|
value = "0" + value;
|
|
|
|
};
|
|
|
|
return value
|
|
|
|
};
|
|
|
|
timeStamp.hours = _timeStampPrefix(timeStamp.hours);
|
|
|
|
timeStamp.minutes = _timeStampPrefix(timeStamp.minutes);
|
|
|
|
timeStamp.seconds = _timeStampPrefix(timeStamp.seconds);
|
|
|
|
timeStamp.date = _timeStampPrefix(timeStamp.date);
|
|
|
|
timeStamp.month = _timeStampPrefix(timeStamp.month + 1);
|
|
|
|
timeStamp.year = _timeStampPrefix(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 exportData = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(load()));
|
|
|
|
tempAchor.setAttribute("href", exportData);
|
|
|
|
tempAchor.setAttribute("download", fileName);
|
|
|
|
helper.e("html").appendChild(tempAchor);
|
|
|
|
tempAchor.click();
|
|
|
|
tempAchor.remove();
|
|
|
|
};
|
|
|
|
|
|
|
|
var clearData = 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 _validateJsonFile = function(fileList) {
|
|
|
|
var controlDataImportFeedback = helper.e(".control-data-import-feedback");
|
|
|
|
var feedbackMessage = {
|
|
|
|
success: "Success! Restoring nightTab Bookmarks and Settings.",
|
|
|
|
fail: {
|
|
|
|
notNightTabJson: "Not the right kind of JSON file. Make sure the selected file came from nightTab.",
|
|
|
|
notJson: "Not a JSON file. Make sure the selected file came from nightTab."
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var _feedback = function(message, animationClass) {
|
|
|
|
while (controlDataImportFeedback.lastChild) {
|
|
|
|
controlDataImportFeedback.removeChild(controlDataImportFeedback.lastChild);
|
|
|
|
};
|
|
|
|
var name = helper.node("p:" + fileList[0].name);
|
|
|
|
var messageText = helper.node("p:" + message + "|class:muted small");
|
|
|
|
controlDataImportFeedback.appendChild(name);
|
|
|
|
controlDataImportFeedback.appendChild(messageText);
|
|
|
|
controlDataImportFeedback.addEventListener("animationend", _resetFeedback, false);
|
|
|
|
helper.removeClass(controlDataImportFeedback, "is-hidden");
|
|
|
|
helper.addClass(controlDataImportFeedback, animationClass);
|
|
|
|
};
|
|
|
|
var _resetFeedback = function() {
|
|
|
|
helper.removeClass(controlDataImportFeedback, "is-shake");
|
|
|
|
helper.removeClass(controlDataImportFeedback, "is-pop");
|
|
|
|
controlDataImportFeedback.removeEventListener("animationend", _resetFeedback, false);
|
|
|
|
};
|
|
|
|
// make new file reader
|
|
|
|
var reader = new FileReader();
|
|
|
|
// define the on load event for the reader
|
|
|
|
reader.onload = function(event) {
|
|
|
|
// is this a JSON file
|
|
|
|
if (helper.isJsonString(event.target.result)) {
|
|
|
|
// is this a nightTab JSON
|
|
|
|
if (JSON.parse(event.target.result).nighttab) {
|
|
|
|
// console.log("is a JSON and a nightTab file");
|
|
|
|
_feedback(feedbackMessage.success, "is-pop");
|
|
|
|
controlDataImportFeedback.addEventListener("animationend", function() {
|
|
|
|
restore(JSON.parse(event.target.result));
|
|
|
|
location.reload();
|
|
|
|
}, false);
|
|
|
|
} else {
|
|
|
|
// console.log("is a JSON file but not a nightTab file");
|
|
|
|
_feedback(feedbackMessage.fail.notNightTabJson, "is-shake");
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// console.log("not a JSON file");
|
|
|
|
_feedback(feedbackMessage.fail.notJson, "is-shake");
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// invoke the reader
|
|
|
|
reader.readAsText(fileList.item(0));
|
|
|
|
};
|
|
|
|
|
2019-01-06 16:47:08 +01:00
|
|
|
var init = function() {
|
2019-06-20 14:00:19 +02:00
|
|
|
restore(data.load());
|
2019-01-06 16:47:08 +01:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:53 +01:00
|
|
|
return {
|
2019-01-06 16:47:08 +01:00
|
|
|
init: init,
|
2018-12-26 08:45:53 +01:00
|
|
|
save: save,
|
2019-01-13 23:54:48 +01:00
|
|
|
remove: remove,
|
2018-12-26 08:45:53 +01:00
|
|
|
set: set,
|
|
|
|
get: get,
|
2019-01-06 16:47:08 +01:00
|
|
|
load: load,
|
2019-05-23 20:15:14 +02:00
|
|
|
wipe: wipe,
|
2019-06-20 14:00:19 +02:00
|
|
|
restore: restore,
|
|
|
|
importData: importData,
|
|
|
|
exportData: exportData,
|
|
|
|
clearData: clearData
|
2018-12-26 08:45:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|