Update popup style sort after toggle. Closes #619 (#624)

* Update popup style  sort after toggle. Closes #619

* Add popup auto resort option

* Switch autoResort to true by default

* Refactor sorting

* Fix: simplify sortStyles function

* Change: autoResort=false
This commit is contained in:
Rob Garrison 2018-12-31 23:11:45 -06:00 committed by GitHub
parent f5db0a5ab0
commit 1fbbeae9b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 21 deletions

View File

@ -1056,6 +1056,10 @@
"message": "Styles before commands", "message": "Styles before commands",
"description": "Label for the checkbox controlling section order in the popup." "description": "Label for the checkbox controlling section order in the popup."
}, },
"popupAutoResort": {
"message": "Resort styles in popup after toggling",
"description": "Label for the checkbox controlling popup resorting."
},
"prefShowBadge": { "prefShowBadge": {
"message": "Number of styles active for the current site", "message": "Number of styles active for the current site",
"description": "Label for the checkbox controlling toolbar badge text." "description": "Label for the checkbox controlling toolbar badge text."

View File

@ -18,6 +18,7 @@ const prefs = (() => {
'popup.breadcrumbs.usePath': false, // use URL path for 'this URL' 'popup.breadcrumbs.usePath': false, // use URL path for 'this URL'
'popup.enabledFirst': true, // display enabled styles before disabled styles 'popup.enabledFirst': true, // display enabled styles before disabled styles
'popup.stylesFirst': true, // display enabled styles before disabled styles 'popup.stylesFirst': true, // display enabled styles before disabled styles
'popup.autoResort': false, // auto resort styles after toggling
'popup.borders': false, // add white borders on the sides 'popup.borders': false, // add white borders on the sides
'popup.findStylesInline': true, // use the inline style search 'popup.findStylesInline': true, // use the inline style search

View File

@ -101,6 +101,13 @@
<span></span> <span></span>
</span> </span>
</label> </label>
<label>
<span i18n-text="popupAutoResort"></span>
<span class="onoffswitch">
<input type="checkbox" id="popup.autoResort" class="slider">
<span></span>
</span>
</label>
<label class="chromium-only"> <label class="chromium-only">
<span i18n-text="popupBorders" i18n-title="popupBordersTooltip"></span> <span i18n-text="popupBorders" i18n-title="popupBordersTooltip"></span>
<span class="onoffswitch"> <span class="onoffswitch">

View File

@ -218,6 +218,15 @@ function initPopup() {
} }
function sortStyles(entries) {
const enabledFirst = prefs.get('popup.enabledFirst');
entries.sort((a, b) =>
enabledFirst && a.styleMeta.enabled !== b.styleMeta.enabled ?
(a.styleMeta.enabled ? -1 : 1) :
a.styleMeta.name.localeCompare(b.styleMeta.name)
);
}
function showStyles(styles) { function showStyles(styles) {
if (!styles) { if (!styles) {
return; return;
@ -227,25 +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 enabledFirst = prefs.get('popup.enabledFirst'); sortStyles(entries);
styles.sort((a, b) => ( entries.forEach(e => installed.appendChild(e));
enabledFirst && a.enabled !== b.enabled
? !(a.enabled < b.enabled) ? -1 : 1
: a.name.localeCompare(b.name)
));
const container = document.createDocumentFragment();
styles.forEach(style => createStyleElement({style, container}));
installed.appendChild(container);
window.dispatchEvent(new Event('showStyles:done')); window.dispatchEvent(new Event('showStyles:done'));
} }
function sortStylesInPlace() {
if (!prefs.get('popup.autoResort')) {
return;
}
const entries = $$('.entry', installed);
if (!entries.length) {
return;
}
sortStyles(entries);
entries.forEach(e => installed.appendChild(e));
}
function createStyleElement({
style, function createStyleElement(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);
@ -319,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);
}
} }
@ -343,7 +351,9 @@ Object.assign(handleEvent, {
toggle(event) { toggle(event) {
// when fired on checkbox, prevent the parent label from seeing the event, see #501 // when fired on checkbox, prevent the parent label from seeing the event, see #501
event.stopPropagation(); event.stopPropagation();
API.toggleStyle(handleEvent.getClickedStyleId(event), this.checked); API
.toggleStyle(handleEvent.getClickedStyleId(event), this.checked)
.then(sortStylesInPlace);
}, },
delete(event) { delete(event) {
@ -475,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);