[refactor] improve update module

This commit is contained in:
Kuldeep M 2019-07-01 12:39:41 +01:00
parent 11555eb306
commit 4ba85a3806

View File

@ -1,11 +1,25 @@
var update = (function() {
var _update_100 = function(data) {
data.version = 1.00;
// this associative array contains all the updates. add
// a new entry if you need to modify data.
//
// example, this assumes the previous version is less than
// 3.28.0, so 3.27.0 would be upgraded in this case.
// _update["3.28.0"] = function(data) {
// return data;
// };
//
// always add the version in increasing order so the
// most recent version is last.
var _update = {};
_update["1.0.0"] = function(data) {
data.version = "1.0.0";
return data;
};
var _update_200 = function(data) {
_update["2.0.0"] = function(data) {
// major state overhaul
data.state = {
header: {
date: {
@ -89,30 +103,28 @@ var update = (function() {
active: false
}
};
// version 1.0.0 bookmarks are not compatible so need to be reset
data.bookmarks = [];
data.version = 2.00;
return data;
};
var _update_210 = function(data) {
_update["2.1.0"] = function(data) {
data.state.layout.theme = {
current: data.state.layout.theme.current,
random: false
};
data.version = 2.10;
return data;
};
var _update_230 = function(data) {
_update["2.3.0"] = function(data) {
data.state.layout.theme.random = {
active: data.state.layout.theme.random,
style: "any"
};
data.version = 2.30;
return data;
};
var _update_240 = function(data) {
_update["2.4.0"] = function(data) {
data.state.link.show = {
active: true,
name: true,
@ -132,17 +144,15 @@ var update = (function() {
accentOpacity: 0
}
};
data.version = 2.40;
return data;
};
var _update_250 = function(data) {
_update["2.5.0"] = function(data) {
data.state.header.search.focus = false;
data.version = 2.50;
return data;
};
var _update_270 = function(data) {
_update["2.7.0"] = function(data) {
// update date character length
data.state.header.date.character = {
length: data.state.header.date.characterLength
@ -190,17 +200,15 @@ var update = (function() {
delete data.state.menu.active;
delete data.state.menu.open;
// update version
data.version = 2.70;
return data;
};
var _update_280 = function(data) {
_update["2.8.0"] = function(data) {
data.state.layout.title = "New Tab";
data.version = 2.80;
return data;
};
var _update_290 = function(data) {
_update["2.9.0"] = function(data) {
data.state.header.shade = {
show: true,
padding: 4,
@ -211,31 +219,42 @@ var update = (function() {
bottom: false
}
};
data.version = 2.90;
return data;
};
var _update_2110 = function(data) {
_update["2.10.0"] = function(data) {
data.state.header.shade = {
show: true,
padding: 4,
style: "scroll",
opacity: 0.95,
border: {
top: false,
bottom: false
}
};
return data;
};
_update["2.11.0"] = function(data) {
data.state.header.greeting = {
show: false,
type: "good",
name: ""
};
data.version = "2.11.0";
return data;
};
var _update_2110 = function(data) {
_update["2.11.0"] = function(data) {
data.state.header.greeting = {
show: false,
type: "good",
name: ""
};
data.version = "2.11.0";
return data;
};
var _update_2120 = function(data) {
_update["2.12.0"] = function(data) {
data.state.bookmarks.link = {
show: data.state.bookmarks.show.link
};
@ -255,17 +274,15 @@ var update = (function() {
style: "dark"
};
delete data.state.layout.theme;
data.version = "2.12.0";
return data;
};
var _update_2140 = function(data) {
_update["2.14.0"] = function(data) {
data.state.layout.width = 72;
data.version = "2.14.0";
return data;
};
var _update_2160 = function(data) {
_update["2.16.0"] = function(data) {
data.state.header.shade.padding = {
top: data.state.header.shade.padding,
bottom: data.state.header.shade.padding
@ -280,30 +297,26 @@ var update = (function() {
width: 1
}
};
data.version = "2.16.0";
return data;
};
var _update_2170 = function(data) {
_update["2.17.0"] = function(data) {
data.state.header.search.engine.google.name = "Google";
data.state.header.search.engine.duckduckgo.name = "Duck Duck Go";
data.state.header.search.engine.giphy.name = "Giphy";
data.version = "2.17.0";
return data;
};
var _update_2190 = function(data) {
_update["2.19.0"] = function(data) {
data.state.header.search.engine.youtube = {
url: "https://www.youtube.com/results?search_query=",
name: "YouTube"
};
data.state.header.search.engine.custom.name = "";
data.version = "2.19.0";
return data;
};
var _update_2200 = function(data) {
data.version = "2.20.0";
_update["2.20.0"] = function(data) {
data.state.header.search.width = {
style: "auto",
custom: 30
@ -315,8 +328,7 @@ var update = (function() {
return data;
};
var _update_2210 = function(data) {
data.version = "2.21.0";
_update["2.21.0"] = function(data) {
data.state.header.clock = {
hours: {
show: data.state.header.clock.show.hours,
@ -374,8 +386,7 @@ var update = (function() {
return data;
};
var _update_2220 = function(data) {
data.version = "2.22.0";
_update["2.22.0"] = function(data) {
data.bookmarks.forEach(function(item, index) {
item.accent = {
override: false,
@ -389,8 +400,7 @@ var update = (function() {
return data;
};
var _update_300 = function(data) {
data.version = "3.0.0";
_update["3.0.0"] = function(data) {
data.bookmarks.forEach(function(item, index) {
item.display = "letter";
item.icon = {
@ -402,8 +412,7 @@ var update = (function() {
return data;
};
var _update_310 = function(data) {
data.version = "3.1.0";
_update["3.1.0"] = function(data) {
data.state.header.area = {
width: 90,
alignment: {
@ -443,8 +452,7 @@ var update = (function() {
return data;
};
var _update_320 = function(data) {
data.version = "3.2.0";
_update["3.2.0"] = function(data) {
data.state.link.display = {
show: true,
alignment: {
@ -461,8 +469,7 @@ var update = (function() {
return data;
};
var _update_340 = function(data) {
data.version = "3.4.0";
_update["3.4.0"] = function(data) {
data.state.header.padding = data.state.header.shade.padding;
delete data.state.header.shade.padding;
data.state.header.border = data.state.header.shade.border;
@ -470,8 +477,7 @@ var update = (function() {
return data;
};
var _update_360 = function(data) {
data.version = "3.6.0";
_update["3.6.0"] = function(data) {
data.state.header.item = data.state.header.items;
delete data.state.header.items;
data.state.link.area.gap = 2;
@ -489,16 +495,14 @@ var update = (function() {
return data;
};
var _update_370 = function(data) {
data.version = "3.7.0";
_update["3.7.0"] = function(data) {
data.state.link.item.line = {
show: true
};
return data;
};
var _update_380 = function(data) {
data.version = "3.8.0";
_update["3.8.0"] = function(data) {
data.state.header.clock.size = 1;
data.state.header.date.size = 1;
data.state.header.greeting.size = 1;
@ -520,8 +524,7 @@ var update = (function() {
return data;
};
var _update_390 = function(data) {
data.version = "3.9.0";
_update["3.9.0"] = function(data) {
delete data.state.header.padding;
data.state.header.radius = false;
data.state.header.border = {
@ -535,57 +538,40 @@ var update = (function() {
return data;
};
var _update_3100 = function(data) {
data.version = "3.10.0";
_update["3.10.0"] = function(data) {
data.state.header.button.style = "box";
return data;
};
var _update_3110 = function(data) {
data.version = "3.11.0";
_update["3.11.0"] = function(data) {
data.state.link.item.line = data.state.link.item.line.show;
data.state.link.item.hoverScale = true;
return data;
};
var _update_3150 = function(data) {
data.version = "3.15.0";
_update["3.15.0"] = function(data) {
delete data.state.link.sort;
return data;
};
var _update_3180 = function(data) {
data.version = "3.18.0";
_update["3.18.0"] = function(data) {
data.nighttab = true;
return data;
};
var _update_3200 = function(data) {
data.version = "3.20.0";
_update["3.20.0"] = function(data) {
data.state.link.item.url = data.state.link.item.url.show;
return data;
};
var _update_3210 = function(data) {
data.version = "3.21.0";
_update["3.21.0"] = function(data) {
data.state.layout.order = "headerLink";
return data;
};
// This associative array contains all the updates. Add
// a new entry if you need to modify data.
//
// Example, this assumes the previous version is less than
// 3.28.0, so 3.27.0 would be upgraded in this case.
// versionUpdates["3.28.0"] = function(data) {
// return data;
// };
//
// Always add the version in increasing order so the
// most recent version is last.
var versionUpdates = {};
versionUpdates["3.27.0"] = function(data) {
_update["3.27.0"] = function(data) {
// swicth to single values for alignment controls
// no more horizontal or vertical keys in state object
data.state.header.area.alignment = data.state.header.area.alignment.horizontal;
data.state.header.item.alignment = data.state.header.item.alignment.horizontal;
data.state.header.search.text.alignment = data.state.header.search.text.align;
@ -596,8 +582,8 @@ var update = (function() {
return data;
};
// Add Bing as a option for the search engines.
versionUpdates["3.28.0"] = function(data) {
_update["3.28.0"] = function(data) {
// add bing search engine
data.state.header.search.engine.bing = {
url: "https://www.bing.com/search?q=",
name: "Bing"
@ -607,158 +593,26 @@ var update = (function() {
function run(data) {
if (!("version" in data)) {
console.log("\t= running update", "1.0.0");
data = _update_100(data);
// legacy update as first version of nightTab did not have a version number stored in the state
data = _update["1.0.0"](data);
};
if (typeof data.version == "number") {
if (data.version < 2.0) {
console.log("\t= running update 2.0.0");
data = _update_200(data);
// old version numbers were type of number
// change them to strings to support new version checking
data.version = data.version.toString().split(".")
data.version.push("0");
data.version = data.version.join(".");
};
if (data.version < 2.1) {
console.log("\t= running update 2.1.0");
data = _update_210(data);
};
if (data.version < 2.3) {
console.log("\t= running update 2.3.0");
data = _update_230(data);
};
if (data.version < 2.4) {
console.log("\t= running update 2.4.0");
data = _update_240(data);
};
if (data.version < 2.5) {
console.log("\t= running update 2.5.0");
data = _update_250(data);
};
if (data.version < 2.7) {
console.log("\t= running update 2.7.0");
data = _update_270(data);
};
if (data.version < 2.8) {
console.log("\t= running update 2.8.0");
data = _update_280(data);
};
if (data.version < 2.9) {
console.log("\t= running update 2.9.0");
data = _update_290(data);
};
if (data.version < 2.91) {
console.log("\t= running update 2.9.1");
data.version = 2.91;
};
if (data.version < 2.96) {
// introduction of a new semantic version check function so the version is being bumped to 2.10.0 and changed to a string
console.log("\t= running update 2.10.0");
data.version = "2.10.0";
};
};
if (typeof data.version == "string") {
if (version.compare(data.version, "2.11.0") == -1) {
console.log("\t= running update 2.11.0");
data = _update_2110(data);
};
if (version.compare(data.version, "2.12.0") == -1) {
console.log("\t= running update 2.12.0");
data = _update_2120(data);
};
if (version.compare(data.version, "2.14.0") == -1) {
console.log("\t= running update 2.14.0");
data = _update_2140(data);
};
if (version.compare(data.version, "2.16.0") == -1) {
console.log("\t= running update 2.16.0");
data = _update_2160(data);
};
if (version.compare(data.version, "2.17.0") == -1) {
console.log("\t= running update 2.17.0");
data = _update_2170(data);
};
if (version.compare(data.version, "2.19.0") == -1) {
console.log("\t= running update 2.19.0");
data = _update_2190(data);
};
if (version.compare(data.version, "2.20.0") == -1) {
console.log("\t= running update 2.20.0");
data = _update_2200(data);
};
if (version.compare(data.version, "2.21.0") == -1) {
console.log("\t= running update 2.21.0");
data = _update_2210(data);
};
if (version.compare(data.version, "2.22.0") == -1) {
console.log("\t= running update 2.22.0");
data = _update_2220(data);
};
if (version.compare(data.version, "3.0.0") == -1) {
console.log("\t= running update 3.0.0");
data = _update_300(data);
};
if (version.compare(data.version, "3.1.0") == -1) {
console.log("\t= running update 3.1.0");
data = _update_310(data);
};
if (version.compare(data.version, "3.2.0") == -1) {
console.log("\t= running update 3.2.0");
data = _update_320(data);
};
if (version.compare(data.version, "3.4.0") == -1) {
console.log("\t= running update 3.4.0");
data = _update_340(data);
};
if (version.compare(data.version, "3.6.0") == -1) {
console.log("\t= running update 3.6.0");
data = _update_360(data);
};
if (version.compare(data.version, "3.7.0") == -1) {
console.log("\t= running update 3.7.0");
data = _update_370(data);
};
if (version.compare(data.version, "3.8.0") == -1) {
console.log("\t= running update 3.8.0");
data = _update_380(data);
};
if (version.compare(data.version, "3.9.0") == -1) {
console.log("\t= running update 3.9.0");
data = _update_390(data);
};
if (version.compare(data.version, "3.10.0") == -1) {
console.log("\t= running update 3.10.0");
data = _update_3100(data);
};
if (version.compare(data.version, "3.11.0") == -1) {
console.log("\t= running update 3.11.0");
data = _update_3110(data);
};
if (version.compare(data.version, "3.15.0") == -1) {
console.log("\t= running update 3.15.0");
data = _update_3150(data);
};
if (version.compare(data.version, "3.18.0") == -1) {
console.log("\t= running update 3.18.0");
data = _update_3180(data);
};
if (version.compare(data.version, "3.20.0") == -1) {
console.log("\t= running update 3.20.0");
data = _update_3200(data);
};
if (version.compare(data.version, "3.21.0") == -1) {
console.log("\t= running update 3.21.0");
data = _update_3210(data);
};
// Shift to a associate array for the configuration update as
// there less code to maintain.
for (var key in versionUpdates) {
for (var key in _update) {
if (version.compare(data.version, key) == -1) {
console.log("\t= running update", key);
data = versionUpdates[key](data);
data = _update[key](data);
data.version = key;
};
};
};
// if no update is needed
// version bump
// if no update is needed version bump
if (version.compare(data.version, version.get()) == -1) {
console.log("\t= nothing to update, version bump to", version.get());
data.version = version.get();