Fix: simplify sortStyles function

This commit is contained in:
eight 2018-12-31 18:08:24 +08:00
parent 1516f6c771
commit 869dc0d114

View File

@ -218,14 +218,13 @@ function initPopup() {
} }
function sortStyles({styles, getEnabled, getName}) { function sortStyles(entries) {
const enabledFirst = prefs.get('popup.enabledFirst'); const enabledFirst = prefs.get('popup.enabledFirst');
return styles.sort((a, b) => { entries.sort((a, b) =>
const aEnabled = getEnabled(a); enabledFirst && a.styleMeta.enabled !== b.styleMeta.enabled ?
return enabledFirst && aEnabled !== getEnabled(b) (a.styleMeta.enabled ? -1 : 1) :
? aEnabled ? -1 : 1 a.styleMeta.name.localeCompare(b.styleMeta.name)
: getName(a).localeCompare(getName(b)); );
});
} }
function showStyles(styles) { function showStyles(styles) {
@ -237,38 +236,26 @@ function showStyles(styles) {
window.dispatchEvent(new Event('showStyles:done')); window.dispatchEvent(new Event('showStyles:done'));
return; return;
} }
const entries = styles.map(createStyleElement);
const container = document.createDocumentFragment(); sortStyles(entries);
sortStyles({ entries.forEach(e => installed.appendChild(e));
styles,
getEnabled: entry => entry.enabled,
getName: entry => entry.name
})
.forEach(style => createStyleElement({style, container}));
installed.appendChild(container);
window.dispatchEvent(new Event('showStyles:done')); window.dispatchEvent(new Event('showStyles:done'));
} }
function sortStylesInPlace() { function sortStylesInPlace() {
if (prefs.get('popup.autoResort')) { if (!prefs.get('popup.autoResort')) {
const styles = $$('.entry', installed); return;
if (styles.length) {
sortStyles({
styles,
getEnabled: entry => entry.styleMeta.enabled,
getName: entry => entry.styleMeta.name
})
.forEach(style => installed.appendChild(style));
}
} }
const entries = $$('.entry', installed);
if (!entries.length) {
return;
}
sortStyles(entries);
entries.forEach(e => installed.appendChild(e));
} }
function createStyleElement({ function createStyleElement(style) {
style,
container = installed,
}) {
let entry = $(ENTRY_ID_PREFIX + style.id); let entry = $(ENTRY_ID_PREFIX + style.id);
if (!entry) { if (!entry) {
entry = template.style.cloneNode(true); entry = template.style.cloneNode(true);
@ -342,9 +329,7 @@ function createStyleElement({
entry.classList.toggle('not-applied', style.excluded || style.sloppy); entry.classList.toggle('not-applied', style.excluded || style.sloppy);
entry.classList.toggle('regexp-partial', style.sloppy); entry.classList.toggle('regexp-partial', style.sloppy);
if (entry.parentNode !== container) { return entry;
container.appendChild(entry);
}
} }
@ -500,12 +485,12 @@ function handleUpdate({style, reason}) {
return; return;
} }
if ($(ENTRY_ID_PREFIX + style.id)) { if ($(ENTRY_ID_PREFIX + style.id)) {
createStyleElement({style}); createStyleElement(style);
return; return;
} }
document.body.classList.remove('blocked'); document.body.classList.remove('blocked');
$$.remove('.blocked-info, #no-styles'); $$.remove('.blocked-info, #no-styles');
createStyleElement({style}); createStyleElement(style);
}) })
.catch(console.error); .catch(console.error);