From 294914f380e44229e2e0b65eedf4b6cc9305f0ee Mon Sep 17 00:00:00 2001 From: zombieFox Date: Fri, 12 Jun 2020 19:19:48 +0100 Subject: [PATCH] [refactor] improve string handling --- src/js/background.js | 2 +- src/js/bookmarks.js | 58 +++++++++++++++++++++----------------------- src/js/clock.js | 7 +++--- src/js/control.js | 19 ++++++++++++++- src/js/date.js | 9 +++---- src/js/greeting.js | 4 +-- src/js/header.js | 4 +-- src/js/helper.js | 23 +++++++++++++++++- src/js/layout.js | 4 +-- src/js/link.js | 28 +++++++++++---------- src/js/search.js | 2 +- src/js/theme.js | 39 ++++++++++++++--------------- 12 files changed, 115 insertions(+), 84 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index b0a3b2bb..61cb2e72 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -172,7 +172,7 @@ var background = (function() { render.feedback = { init: function() { - if (state.get.current().background.image.file.name != "") { + if (helper.checkValueString(state.get.current().background.image.file.name)) { render.feedback.current(); } else { render.feedback.empty(); diff --git a/src/js/bookmarks.js b/src/js/bookmarks.js index ac49346f..6c5873b9 100644 --- a/src/js/bookmarks.js +++ b/src/js/bookmarks.js @@ -11,7 +11,7 @@ var bookmarks = (function() { show: true }, items: [{ - visual:{ + visual: { display: "icon", letter: "AS", image: null, @@ -53,7 +53,7 @@ var bookmarks = (function() { searchMatch: false, timeStamp: 1546453104010 }, { - visual:{ + visual: { display: "letter", letter: "AZ", image: null, @@ -95,7 +95,7 @@ var bookmarks = (function() { searchMatch: false, timeStamp: 1546453107633 }, { - visual:{ + visual: { display: "letter", letter: "GM", image: null, @@ -137,7 +137,7 @@ var bookmarks = (function() { searchMatch: false, timeStamp: 1546453110265 }, { - visual:{ + visual: { display: "icon", letter: "R", image: null, @@ -179,7 +179,7 @@ var bookmarks = (function() { searchMatch: false, timeStamp: 1546453111491 }, { - visual:{ + visual: { display: "icon", letter: "N", image: null, @@ -221,7 +221,7 @@ var bookmarks = (function() { searchMatch: false, timeStamp: 1546453104460 }, { - visual:{ + visual: { display: "letter", letter: "DR", image: null, @@ -272,7 +272,7 @@ var bookmarks = (function() { show: true }, items: [{ - visual:{ + visual: { display: "icon", letter: "DEV", image: null, @@ -314,7 +314,7 @@ var bookmarks = (function() { searchMatch: false, timeStamp: 1546453101749 }, { - visual:{ + visual: { display: "icon", letter: "GIT", image: null, @@ -387,34 +387,30 @@ var bookmarks = (function() { mod.add = { link: function(data) { - if (data.position.group.new) { - mod.add.group({ - position: { - origin: null, - destination: data.position.destination.group - }, - group: { - name: { - text: data.position.group.name.text, - show: data.position.group.name.show + if (data) { + if (data.position.group.new) { + mod.add.group({ + position: { + origin: null, + destination: data.position.destination.group }, - openAll: { - show: data.position.group.openAll.show - }, - items: [] - } - }); + group: { + name: { + text: data.position.group.name.text, + show: data.position.group.name.show + }, + openAll: { + show: data.position.group.openAll.show + }, + items: [] + } + }); + }; + mod.all[data.position.destination.group].items.splice(data.position.destination.item, 0, data.link); }; - mod.all[data.position.destination.group].items.splice(data.position.destination.item, 0, data.link); }, group: function(data) { if (data) { - if (data.group.name.text == null) { - data.group.name.text = ""; - }; - if (typeof data.group.name.text == "string") { - data.group.name.text = data.group.name.text.trim(); - }; mod.all.splice(data.position.destination, 0, data.group); }; } diff --git a/src/js/clock.js b/src/js/clock.js index 5b398ab5..aa00832c 100644 --- a/src/js/clock.js +++ b/src/js/clock.js @@ -112,10 +112,9 @@ var clock = (function() { }; if (state.get.current().header.clock.separator.show) { var separatorCharacter; - if (typeof state.get.current().header.clock.separator.text == "string" && state.get.current().header.clock.separator.text != "") { - separatorCharacter = state.get.current().header.clock.separator.text.trim().replace(/\s\s+/g, " "); - }; - if (separatorCharacter == "" || separatorCharacter == " " || separatorCharacter == undefined) { + if (helper.checkValueString(state.get.current().header.clock.separator.text)) { + separatorCharacter = helper.trimString(state.get.current().header.clock.separator.text); + } else { separatorCharacter = ":"; }; var parts = clock.querySelectorAll("span"); diff --git a/src/js/control.js b/src/js/control.js index 06484a0d..1f59e3c9 100644 --- a/src/js/control.js +++ b/src/js/control.js @@ -2225,8 +2225,25 @@ var control = (function() { path: "header.search.engine.custom.queryName", type: "text", func: function() { - console.log("hit"); + header.render.item.clear(); + header.render.item.all(); + header.render.search.width.size(); + greeting.render.clear(); + greeting.render.all(); + clock.render.clear(); + clock.render.all(); + transitional.render.clear(); + transitional.render.all(); + date.render.clear(); + date.render.all(); + render.class(); + render.dependents(); + render.update.control.header(); + bind.control.header(); search.render.engine(); + search.bind.input(); + search.bind.clear(); + dropdown.bind.editAdd(); } }, { element: ".control-header-search-text-justify-left", diff --git a/src/js/date.js b/src/js/date.js index 9a4f4127..2469e900 100644 --- a/src/js/date.js +++ b/src/js/date.js @@ -129,11 +129,10 @@ var date = (function() { }; if (state.get.current().header.date.separator.show) { var separatorCharacter; - if (typeof state.get.current().header.date.separator.text == "string" && state.get.current().header.date.separator.text != "") { - separatorCharacter = state.get.current().header.date.separator.text.trim().replace(/\s\s+/g, " "); - }; - if (separatorCharacter == "" || separatorCharacter == " " || separatorCharacter == undefined) { - separatorCharacter = "/"; + if (helper.checkValueString(state.get.current().header.date.separator.text)) { + separatorCharacter = helper.trimString(state.get.current().header.date.separator.text); + } else { + separatorCharacter = ":"; }; var parts = date.querySelectorAll("span"); if (parts.length > 1) { diff --git a/src/js/greeting.js b/src/js/greeting.js index 680110ec..150b23e6 100644 --- a/src/js/greeting.js +++ b/src/js/greeting.js @@ -37,8 +37,8 @@ var greeting = (function() { } }; var string = message[state.get.current().header.greeting.type](); - if (state.get.current().header.greeting.name != "" && state.get.current().header.greeting.name != undefined) { - string = string + ", " + state.get.current().header.greeting.name; + if (helper.checkValueString(state.get.current().header.greeting.name)) { + string = string + ", " + helper.trimString(state.get.current().header.greeting.name) }; var greetingItem = helper.node("span|class:greeting-item"); var greetingItemText = helper.node("span:" + string + "|class:greeting-item-text"); diff --git a/src/js/header.js b/src/js/header.js index 0c7b7832..8768daff 100644 --- a/src/js/header.js +++ b/src/js/header.js @@ -373,8 +373,8 @@ var header = (function() { var headerSearchBody = helper.node("div|class:search-wrapper"); var form = helper.node("form|class:search,action,method:get"); var searchInput; - if (typeof state.get.current().header.search.engine.custom.queryName == "string" && state.get.current().header.search.engine.custom.queryName != "") { - searchInput = helper.node("input|class:search-input,type:search,placeholder:Find or Search,name:" + state.get.current().header.search.engine.custom.queryName.trim() + ",autocomplete:off,autocorrect:off,autocapitalize:off,spellcheck:false,tabindex:1"); + if (helper.checkValueString(state.get.current().header.search.engine.custom.queryName)) { + searchInput = helper.node("input|class:search-input,type:search,placeholder:Find or Search,name:" + helper.trimString(state.get.current().header.search.engine.custom.queryName) + ",autocomplete:off,autocorrect:off,autocapitalize:off,spellcheck:false,tabindex:1"); } else { searchInput = helper.node("input|class:search-input,type:search,placeholder:Find or Search,name:q,autocomplete:off,autocorrect:off,autocapitalize:off,spellcheck:false,tabindex:1"); }; diff --git a/src/js/helper.js b/src/js/helper.js index 28450dc3..e67dfad0 100644 --- a/src/js/helper.js +++ b/src/js/helper.js @@ -767,6 +767,25 @@ var helper = (function() { }; }; + var checkValueString = function(value) { + var result = false; + if (typeof value == "string") { + value = value.trim().replace(/\s/g, ""); + if (value != "") { + result = true; + }; + }; + return result; + }; + + var trimString = function(value) { + if (typeof value == "string") { + return value.trim().replace(/\s\s+/g, " "); + } else { + return value; + }; + }; + // exposed methods return { e: e, @@ -792,7 +811,9 @@ var helper = (function() { ordinalNumber: ordinalNumber, isJsonString: isJsonString, isHexNumber: isHexNumber, - convertColor: convertColor + convertColor: convertColor, + checkValueString: checkValueString, + trimString: trimString }; })(); diff --git a/src/js/layout.js b/src/js/layout.js index ca6b6900..80195eba 100644 --- a/src/js/layout.js +++ b/src/js/layout.js @@ -24,8 +24,8 @@ var layout = (function() { render.title = function() { var title = helper.e("title"); - if (typeof state.get.current().layout.title == "string" && state.get.current().layout.title != "") { - title.textContent = state.get.current().layout.title.trim().replace(/\s\s+/g, " "); + if (helper.checkValueString(state.get.current().layout.title)) { + title.textContent = helper.trimString(state.get.current().layout.title); } else { title.textContent = "New tab"; }; diff --git a/src/js/link.js b/src/js/link.js index a427cb57..5504c6ea 100644 --- a/src/js/link.js +++ b/src/js/link.js @@ -709,7 +709,7 @@ var link = (function() { }); groupHeader.appendChild(groupHeaderItemControl); - if (stagedGroup.group.name.show && stagedGroup.group.name.text.replace(/\s/g, "") != "") { + if (stagedGroup.group.name.show && helper.checkValueString(stagedGroup.group.name.text)) { helper.addClass(groupHeader, "group-header-name"); groupHeader.appendChild(groupHeaderItemName); }; @@ -916,7 +916,7 @@ var link = (function() { value: "link-item" }] }; - if (stagedLink.link.accent.by == "custom" || stagedLink.link.color.by == "custom" || (stagedLink.link.image != null && typeof stagedLink.link.image == "string" && stagedLink.link.image != "")) { + if (stagedLink.link.accent.by == "custom" || stagedLink.link.color.by == "custom" || helper.checkValueString(stagedLink.link.image)) { linkItemOptions.attr.push({ key: "style", value: "" @@ -940,8 +940,8 @@ var link = (function() { "--link-item-color: " + stagedLink.link.color.rgb.r + ", " + stagedLink.link.color.rgb.g + ", " + stagedLink.link.color.rgb.b + ";" + "--link-item-color-focus-hover: " + stagedLink.link.color.rgb.r + ", " + stagedLink.link.color.rgb.g + ", " + stagedLink.link.color.rgb.b + ";"; }; - if (stagedLink.link.image != null && stagedLink.link.image != "") { - linkItemOptions.attr[1].value = linkItemOptions.attr[1].value + "--link-image-url: url(" + stagedLink.link.image.trim() + ");" + if (helper.checkValueString(stagedLink.link.image)) { + linkItemOptions.attr[1].value = linkItemOptions.attr[1].value + "--link-image-url: url(" + helper.trimString(stagedLink.link.image) + ");" }; }; var linkItem = helper.makeNode(linkItemOptions); @@ -993,15 +993,17 @@ var link = (function() { value: "link-display-image" }, { key: "style", - value: "--link-display-image-url: url(" + stagedLink.link.visual.image.trim() + ")" + value: "--link-display-image-url: url(" + helper.trimString(stagedLink.link.visual.image) + ")" }] }); }; - var nameText = ""; - if (typeof stagedLink.link.name == "string" && stagedLink.link.name != "") { - nameText = stagedLink.link.name.trim().replace(/\s\s+/g, " "); + + var linkDisplayName; + if (helper.checkValueString(stagedLink.link.name)) { + linkDisplayName = helper.node("p:" + helper.trimString(stagedLink.link.name) + "|class:link-display-name"); + } else { + linkDisplayName = helper.node("p|class:link-display-name"); }; - var linkDisplayName = helper.node("p:" + nameText + "|class:link-display-name"); var linkUrl = helper.node("div|class:link-url"); var url = ""; @@ -1043,11 +1045,11 @@ var link = (function() { linkDisplay.appendChild(linkDisplayVisual); }; - if (stagedLink.link.name != null && stagedLink.link.name != "") { + if (helper.checkValueString(stagedLink.link.name)) { linkDisplay.appendChild(linkDisplayName); }; - if (stagedLink.link.image != null && stagedLink.link.image != "") { + if (helper.checkValueString(stagedLink.link.image)) { linkPanelFront.appendChild(linkImage); }; linkPanelFront.appendChild(linkDisplay); @@ -2898,7 +2900,7 @@ var link = (function() { useStagedLink: true }); var heading; - if (stagedLink.link.name != null && stagedLink.link.name != "") { + if (helper.checkValueString(stagedLink.link.name)) { heading = "Edit " + stagedLink.link.name; } else { heading = "Edit unnamed bookmark"; @@ -3003,7 +3005,7 @@ var link = (function() { stagedLink.link = JSON.parse(JSON.stringify(copyStagedLink.link)); stagedLink.position = JSON.parse(JSON.stringify(copyStagedLink.position)); var heading; - if (stagedLink.link.name != null && stagedLink.link.name != "") { + if (helper.checkValueString(stagedLink.link.name)) { heading = "Remove " + stagedLink.link.name; } else { heading = "Remove unnamed bookmark"; diff --git a/src/js/search.js b/src/js/search.js index 80ecc337..077c27c4 100644 --- a/src/js/search.js +++ b/src/js/search.js @@ -144,7 +144,7 @@ var search = (function() { render.check = function() { var searchInput = helper.e(".search-input"); - if (searchInput.value.replace(/\s/g, "") != "") { + if (helper.checkValueString(searchInput.value)) { mod.searching.open(); } else { mod.searching.close(); diff --git a/src/js/theme.js b/src/js/theme.js index c7b448d8..4a5fe4f5 100644 --- a/src/js/theme.js +++ b/src/js/theme.js @@ -2298,11 +2298,11 @@ var theme = (function() { mod.preset.all.forEach(function(arrayItem, index) { var displayFont = arrayItem.font.display.name + ":" + arrayItem.font.display.weight; var uiFont = arrayItem.font.ui.name + ":" + arrayItem.font.ui.weight; - if (arrayItem.font.display.name != "" && !allPresetFonts.includes(displayFont)) { + if (helper.checkValueString(arrayItem.font.display.name) && !allPresetFonts.includes(displayFont)) { mod.font.loaded.push(arrayItem.font.display.name); allPresetFonts.push(displayFont); }; - if (arrayItem.font.ui.name != "" && !allPresetFonts.includes(uiFont)) { + if (helper.checkValueString(arrayItem.font.ui.name) && !allPresetFonts.includes(uiFont)) { mod.font.loaded.push(arrayItem.font.ui.name); allPresetFonts.push(uiFont); }; @@ -2315,24 +2315,24 @@ var theme = (function() { }, custom: { display: function() { - var displayFont = state.get.current().theme.font.display.name.trim().replace(/\s\s+/g, " "); - if (!mod.font.loaded.includes(displayFont) && displayFont != "") { + var displayFont = helper.trimString(state.get.current().theme.font.display.name); + if (!mod.font.loaded.includes(displayFont) && helper.checkValueString(displayFont)) { mod.font.loaded.push(displayFont); WebFont.load({ google: { - families: [state.get.current().theme.font.display.name.trim().replace(/\s\s+/g, " ") + ":100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i"] + families: [helper.trimString(state.get.current().theme.font.display.name) + ":100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i"] } }); }; render.font.display.name(); }, ui: function() { - var uiFont = state.get.current().theme.font.ui.name.trim().replace(/\s\s+/g, " "); - if (!mod.font.loaded.includes(uiFont) && uiFont != "") { + var uiFont = helper.trimString(state.get.current().theme.font.ui.name); + if (!mod.font.loaded.includes(uiFont) && helper.checkValueString(uiFont)) { mod.font.loaded.push(uiFont); WebFont.load({ google: { - families: [state.get.current().theme.font.ui.name.trim().replace(/\s\s+/g, " ") + ":100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i"] + families: [helper.trimString(state.get.current().theme.font.ui.name) + ":100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i"] } }); }; @@ -2353,9 +2353,8 @@ var theme = (function() { display: { name: function() { var html = helper.e("html"); - var name = state.get.current().theme.font.display.name.trim().replace(/\s\s+/g, " "); - if (name != "") { - html.style.setProperty("--theme-font-display-name", "\"" + name + "\"" + ", \"Fjalla One\", sans-serif"); + if (helper.checkValueString(state.get.current().theme.font.display.name)) { + html.style.setProperty("--theme-font-display-name", "\"" + helper.trimString(state.get.current().theme.font.display.name) + "\", \"Fjalla One\", sans-serif"); } else { html.style.removeProperty("--theme-font-display-name"); }; @@ -2373,9 +2372,8 @@ var theme = (function() { ui: { name: function() { var html = helper.e("html"); - var name = state.get.current().theme.font.ui.name.trim().replace(/\s\s+/g, " "); - if (name != "") { - html.style.setProperty("--theme-font-ui-name", "\"" + name + "\"" + ", \"Open Sans\", sans-serif"); + if (helper.checkValueString(state.get.current().theme.font.ui.name)) { + html.style.setProperty("--theme-font-ui-name", "\"" + helper.trimString(state.get.current().theme.font.ui.name) + "\", \"Open Sans\", sans-serif"); } else { html.style.removeProperty("--theme-font-ui-name"); }; @@ -2457,7 +2455,8 @@ var theme = (function() { var themePresetAccent = helper.node("span|class:theme-preset-accent"); themePresetPreview.style.setProperty("--theme-preset-accent", arrayItem.accent.rgb.r + ", " + arrayItem.accent.rgb.g + ", " + arrayItem.accent.rgb.b); themePresetPreview.appendChild(themePresetAccent); - if (arrayItem.name != null && arrayItem.name != "") { + + if (helper.checkValueString(arrayItem.name)) { var themePresetName = helper.node("span:" + arrayItem.name + "|class:theme-preset-name"); themePresetPreview.appendChild(themePresetName); }; @@ -2554,11 +2553,9 @@ var theme = (function() { var themeCustomAccent = helper.node("span|class:theme-custom-accent"); themeCustomPreview.style.setProperty("--theme-custom-accent", arrayItem.accent.rgb.r + ", " + arrayItem.accent.rgb.g + ", " + arrayItem.accent.rgb.b); themeCustomPreview.appendChild(themeCustomAccent); - if (arrayItem.name != null && arrayItem.name != "") { - var themeCustomName = helper.node("span:" + arrayItem.name + "|class:theme-custom-name"); - themeCustomPreview.appendChild(themeCustomName); + if (helper.checkValueString(arrayItem.name)) { + themeCustomPreview.appendChild(helper.node("span:" + helper.trimString(arrayItem.name) + "|class:theme-custom-name")); }; - themeCustomButton.appendChild(themeCustomPreview); themeCustomEdit.appendChild(themeCustomEditIcon); themeCustomRemove.appendChild(themeCustomRemoveIcon); @@ -2713,7 +2710,7 @@ var theme = (function() { useStagedTheme: true }); var heading; - if (stagedThemeCustom.theme.name != null && stagedThemeCustom.theme.name != "") { + if (helper.checkValueString(stagedThemeCustom.theme.name)) { heading = "Edit " + stagedThemeCustom.theme.name; } else { heading = "Edit unnamed theme"; @@ -2753,7 +2750,7 @@ var theme = (function() { stagedThemeCustom.position.index = JSON.parse(JSON.stringify(copyStagedThemeCustom.position.index)); stagedThemeCustom.theme = JSON.parse(JSON.stringify(copyStagedThemeCustom.theme)); var heading; - if (stagedThemeCustom.theme.name != null && stagedThemeCustom.theme.name != "") { + if (helper.checkValueString(stagedThemeCustom.theme.name)) { heading = "Remove " + stagedThemeCustom.theme.name; } else { heading = "Remove unnamed theme";