From 918debe1105eae6b61159dcf4d6b7d48c9ebc9ad Mon Sep 17 00:00:00 2001 From: tophf Date: Fri, 24 Aug 2018 14:02:31 +0300 Subject: [PATCH] fix show-more in old UI and simplify generation of targets --- manage/manage.js | 64 +++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/manage/manage.js b/manage/manage.js index 3354d4d7..4e14f62f 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -153,17 +153,16 @@ function showStyles(styles = [], matchUrlIds) { filterAndAppend({container: renderBin}).then(sorter.updateStripes); if (index < sorted.length) { requestAnimationFrame(renderStyles); - if (firstRun) setTimeout(recreateStyleTargets, 0, {styles, iconsOnly: true}); + if (firstRun) setTimeout(getFaviconImgSrc); firstRun = false; return; } - if (newUI.enabled && newUI.favicons) { - setTimeout(recreateStyleTargets, 0, {iconsOnly: true}); - } - if ('scrollY' in (history.state || {}) && !sessionStorage.justEditedStyleId) { + setTimeout(getFaviconImgSrc); + if (sessionStorage.justEditedStyleId) { + highlightEditedStyle(); + } else if ('scrollY' in (history.state || {})) { setTimeout(window.scrollTo, 0, 0, history.state.scrollY); } - highlightEditedStyle(); } } @@ -238,10 +237,10 @@ function createStyleElement({style, name}) { } -function createStyleTargetsElement({entry, style, iconsOnly}) { +function createStyleTargetsElement({entry, style}) { const parts = createStyleElement.parts; const entryTargets = $('.targets', entry); - const targets = iconsOnly ? entryTargets : parts.targets.cloneNode(true); + const targets = parts.targets.cloneNode(true); let container = targets; let numTargets = 0; const displayed = new Set(); @@ -252,23 +251,21 @@ function createStyleTargetsElement({entry, style, iconsOnly}) { continue; } displayed.add(targetValue); - const element = iconsOnly ? targets.children[numTargets] : template.appliesToTarget.cloneNode(true); + const element = template.appliesToTarget.cloneNode(true); if (!newUI.enabled) { if (numTargets === 10) { container = container.appendChild(template.extraAppliesTo.cloneNode(true)); - } else if (numTargets > 1) { + } else if (numTargets > 0) { container.appendChild(template.appliesToSeparator.cloneNode(true)); } } - if (!iconsOnly) { - element.dataset.type = type; - element.appendChild( - document.createTextNode( - (parts.decorations[type + 'Before'] || '') + - targetValue + - (parts.decorations[type + 'After'] || ''))); - container.appendChild(element); - } + element.dataset.type = type; + element.appendChild( + document.createTextNode( + (parts.decorations[type + 'Before'] || '') + + targetValue + + (parts.decorations[type + 'After'] || ''))); + container.appendChild(element); numTargets++; } } @@ -279,9 +276,7 @@ function createStyleTargetsElement({entry, style, iconsOnly}) { } } if (numTargets) { - if (!iconsOnly) { - entryTargets.parentElement.replaceChild(targets, entryTargets); - } + entryTargets.parentElement.replaceChild(targets, entryTargets); } else if (!entry.classList.contains('global') || !entryTargets.firstElementChild) { if (entryTargets.firstElementChild) { @@ -293,25 +288,8 @@ function createStyleTargetsElement({entry, style, iconsOnly}) { } -function recreateStyleTargets({styles, iconsOnly = false} = {}) { - Promise.resolve(styles || API.getStyles()).then(styles => { - for (const style of styles) { - const entry = $(ENTRY_ID_PREFIX + style.id); - if (entry) { - createStyleTargetsElement({ - entry, - style, - iconsOnly, - }); - } - } - if (newUI.enabled && newUI.favicons) { - debounce(getFaviconImgSrc); - } - }); -} - function getFaviconImgSrc(container = installed) { + if (!newUI.enabled || !newUI.favicons) return; const regexpRemoveNegativeLookAhead = /(\?!([^)]+\))|\(\?![\w(]+[^)]+[\w|)]+)/g; // replace extra characters & all but the first group entry "(abc|def|ghi)xyz" => abcxyz const regexpReplaceExtraCharacters = /[\\(]|((\|\w+)+\))/g; @@ -557,9 +535,7 @@ function handleUpdate(style, {reason, method} = {}) { animateElement(entry); requestAnimationFrame(() => scrollElementIntoView(entry)); } - if (newUI.enabled && newUI.favicons) { - getFaviconImgSrc(entry); - } + getFaviconImgSrc(entry); function handleToggledOrCodeOnly() { const newStyleMeta = getStyleWithNoCode(style); @@ -673,7 +649,7 @@ function switchUI({styleOnly} = {}) { return; } if (missingFavicons) { - recreateStyleTargets(); + debounce(getFaviconImgSrc); return; } }