From 1aa5c11b10d29a852511f9bfbe1781dc09515286 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 27 Jun 2017 23:13:11 +0300 Subject: [PATCH] code cosmetics: entry id prefix => named constant --- manage.js | 15 +++++++++------ popup.js | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/manage.js b/manage.js index 87e357c7..7589e8ff 100644 --- a/manage.js +++ b/manage.js @@ -7,6 +7,9 @@ const filtersSelector = { unhide: '', }; +const ENTRY_ID_PREFIX_RAW = 'style-'; +const ENTRY_ID_PREFIX = '#' + ENTRY_ID_PREFIX_RAW; + const newUI = { enabled: prefs.get('manage.newUI'), favicons: prefs.get('manage.newUI.favicons'), @@ -140,7 +143,7 @@ function showStyles(styles = []) { if (newUI.enabled && newUI.favicons) { debounce(handleEvent.loadFavicons, 16); } - } + } } @@ -175,7 +178,7 @@ function createStyleElement({style, name}) { parts.homepage.href = parts.homepage.title = style.url || ''; const entry = parts.entry.cloneNode(true); - entry.id = 'style-' + style.id; + entry.id = ENTRY_ID_PREFIX_RAW + style.id; entry.styleId = style.id; entry.styleNameLowerCase = name || style.name.toLocaleLowerCase(); entry.styleMeta = getStyleWithNoCode(style); @@ -400,7 +403,7 @@ Object.assign(handleEvent, { function handleUpdate(style, {reason, method} = {}) { let entry; - let oldEntry = $('#style-' + style.id); + let oldEntry = $(ENTRY_ID_PREFIX + style.id); if (oldEntry && method == 'styleUpdated') { handleToggledOrCodeOnly(); } @@ -449,7 +452,7 @@ function handleUpdate(style, {reason, method} = {}) { function handleDelete(id) { - const node = $('#style-' + id); + const node = $(ENTRY_ID_PREFIX + id); if (node) { node.remove(); if (node.matches('.can-update')) { @@ -519,7 +522,7 @@ function switchUI({styleOnly} = {}) { if (missingFavicons) { getStylesSafe().then(styles => { for (const style of styles) { - const entry = $('#style-' + style.id); + const entry = $(ENTRY_ID_PREFIX + style.id); if (entry) { createStyleTargetsElement({entry, style, postponeFavicons: true}); } @@ -621,7 +624,7 @@ function checkUpdate(entry, {single = true} = {}) { function reportUpdateState(state, style, details) { - const entry = $('#style-' + style.id); + const entry = $(ENTRY_ID_PREFIX + style.id); entry.classList.remove('checking-update'); switch (state) { case BG.updater.UPDATED: diff --git a/popup.js b/popup.js index 9c5e8e5c..f82b978f 100644 --- a/popup.js +++ b/popup.js @@ -5,6 +5,9 @@ let installed; let tabURL; const handleEvent = {}; +const ENTRY_ID_PREFIX_RAW = 'style-'; +const ENTRY_ID_PREFIX = '#' + ENTRY_ID_PREFIX_RAW; + getActiveTabRealURL().then(url => { tabURL = URLS.supported.test(url) ? url : ''; Promise.all([ @@ -215,7 +218,7 @@ function createStyleElement({ const entry = template.style.cloneNode(true); entry.setAttribute('style-id', style.id); Object.assign(entry, { - id: 'style-' + style.id, + id: ENTRY_ID_PREFIX_RAW + style.id, styleId: style.id, className: entry.className + ' ' + (style.enabled ? 'enabled' : 'disabled'), onmousedown: handleEvent.maybeEdit, @@ -223,7 +226,7 @@ function createStyleElement({ const checkbox = $('.checker', entry); Object.assign(checkbox, { - id: 'style-' + style.id, + id: ENTRY_ID_PREFIX_RAW + style.id, checked: style.enabled, onclick: handleEvent.toggle, }); @@ -236,7 +239,7 @@ function createStyleElement({ const styleName = $('.style-name', entry); Object.assign(styleName, { - htmlFor: 'style-' + style.id, + htmlFor: ENTRY_ID_PREFIX_RAW + style.id, onclick: handleEvent.name, }); styleName.checkbox = checkbox; @@ -248,7 +251,7 @@ function createStyleElement({ invokeOrPostpone(!postponeDetect, detectSloppyRegexps, {entry, style}); - const oldElement = $('#style-' + style.id); + const oldElement = $(ENTRY_ID_PREFIX + style.id); if (oldElement) { oldElement.parentNode.replaceChild(entry, oldElement); } else { @@ -368,7 +371,7 @@ Object.assign(handleEvent, { function handleUpdate(style) { - if ($('#style-' + style.id)) { + if ($(ENTRY_ID_PREFIX + style.id)) { createStyleElement({style}); return; } @@ -381,7 +384,7 @@ function handleUpdate(style) { function handleDelete(id) { - $$('#style-' + id).forEach(el => el.remove()); + $$(ENTRY_ID_PREFIX + id).forEach(el => el.remove()); }