Clean up sort function & variable names

This commit is contained in:
Rob Garrison 2017-12-23 22:45:20 -06:00
parent 45231bf8dc
commit d800f072f9
4 changed files with 23 additions and 23 deletions

View File

@ -155,7 +155,7 @@ function filterOnChange({target: el, forceRefilter}) {
}); });
if (installed) { if (installed) {
reapplyFilter(); reapplyFilter();
sorter().updateSort(); sorter().update();
} }
} }

View File

@ -85,7 +85,7 @@ function initGlobalEvents() {
// N.B. triggers existing onchange listeners // N.B. triggers existing onchange listeners
setupLivePrefs(); setupLivePrefs();
sorter().sortInit(); sorter().init();
$$('[id^="manage.newUI"]') $$('[id^="manage.newUI"]')
.forEach(el => (el.oninput = (el.onchange = switchUI))); .forEach(el => (el.oninput = (el.onchange = switchUI)));
@ -108,7 +108,7 @@ function initGlobalEvents() {
function showStyles(styles = []) { function showStyles(styles = []) {
const sorted = sorter().sortStyles({ const sorted = sorter().sort({
parser: 'style', parser: 'style',
styles: styles.map(style => ({ styles: styles.map(style => ({
style, style,
@ -459,7 +459,7 @@ function handleUpdate(style, {reason, method} = {}) {
handleUpdateInstalled(entry, reason); handleUpdateInstalled(entry, reason);
} }
filterAndAppend({entry}); filterAndAppend({entry});
sorter().updateSort(); sorter().update();
if (!entry.matches('.hidden') && reason !== 'import') { if (!entry.matches('.hidden') && reason !== 'import') {
animateElement(entry); animateElement(entry);
scrollElementIntoView(entry); scrollElementIntoView(entry);

View File

@ -27,7 +27,7 @@ const sorter = (() => {
sorter: sorterType.number sorter: sorterType.number
}, },
disabled: { disabled: {
text: '', // added as either "enabled" or "disabled" by the addSortOptions function text: '', // added as either "enabled" or "disabled" by the addOptions function
parse: { parse: {
style: ({style}) => (style.enabled ? 1 : 0), style: ({style}) => (style.enabled ? 1 : 0),
entry: entry => (entry.classList.contains('enabled') ? 1 : 0) entry: entry => (entry.classList.contains('enabled') ? 1 : 0)
@ -54,7 +54,7 @@ const sorter = (() => {
// Adding (assumed) most commonly used ('title,asc' should always be first) // Adding (assumed) most commonly used ('title,asc' should always be first)
// whitespace before & after the comma is ignored // whitespace before & after the comma is ignored
const sortSelectOptions = [ const selectOptions = [
'{groupAsc}', '{groupAsc}',
'title,asc', 'title,asc',
'dateInstalled,desc, title,asc', 'dateInstalled,desc, title,asc',
@ -74,9 +74,9 @@ const sorter = (() => {
'disabled,desc, usercss,asc, title,desc' 'disabled,desc, usercss,asc, title,desc'
]; ];
const sortByRegex = /\s*,\s*/; const splitRegex = /\s*,\s*/;
function addSortOptions() { function addOptions() {
let container; let container;
const select = $('#sort-select'); const select = $('#sort-select');
const renderBin = document.createDocumentFragment(); const renderBin = document.createDocumentFragment();
@ -92,7 +92,7 @@ const sorter = (() => {
groupDesc: t('sortLabelTitleDesc') groupDesc: t('sortLabelTitleDesc')
}; };
const optgroupRegex = /\{\w+\}/; const optgroupRegex = /\{\w+\}/;
sortSelectOptions.forEach(sort => { selectOptions.forEach(sort => {
if (optgroupRegex.test(sort)) { if (optgroupRegex.test(sort)) {
if (container) { if (container) {
renderBin.appendChild(container); renderBin.appendChild(container);
@ -103,7 +103,7 @@ const sorter = (() => {
} }
let lastTag = ''; let lastTag = '';
const opt = option.cloneNode(); const opt = option.cloneNode();
opt.textContent = sort.split(sortByRegex).reduce((acc, val) => { opt.textContent = sort.split(splitRegex).reduce((acc, val) => {
if (tagData[val]) { if (tagData[val]) {
lastTag = val; lastTag = val;
return acc + (acc !== '' ? ' + ' : '') + tagData[val].text; return acc + (acc !== '' ? ' + ' : '') + tagData[val].text;
@ -120,14 +120,14 @@ const sorter = (() => {
select.value = prefs.get('manage.newUI.sort'); select.value = prefs.get('manage.newUI.sort');
} }
function sortStyles({styles, parser}) { function sort({styles, parser}) {
if (!styles) { if (!styles) {
styles = [...installed.children]; styles = [...installed.children];
parser = 'entry'; parser = 'entry';
} else { } else {
parser = 'style'; parser = 'style';
} }
const sortBy = prefs.get('manage.newUI.sort').split(sortByRegex); // 'title,asc' const sortBy = prefs.get('manage.newUI.sort').split(splitRegex); // 'title,asc'
const len = sortBy.length; const len = sortBy.length;
return styles.sort((a, b) => { return styles.sort((a, b) => {
let types, direction; let types, direction;
@ -143,10 +143,10 @@ const sorter = (() => {
}); });
} }
function updateSort() { function update() {
if (!installed) return; if (!installed) return;
const current = [...installed.children]; const current = [...installed.children];
const sorted = sortStyles({ const sorted = sort({
styles: current.map(entry => ({ styles: current.map(entry => ({
entry, entry,
name: entry.styleNameLowerCase, name: entry.styleNameLowerCase,
@ -163,13 +163,13 @@ const sorter = (() => {
} }
} }
function manageSort(event) { function manageChange(event) {
event.preventDefault(); event.preventDefault();
prefs.set('manage.newUI.sort', this.value); prefs.set('manage.newUI.sort', this.value);
updateSort(); update();
} }
function showSortHelp(event) { function showHelp(event) {
event.preventDefault(); event.preventDefault();
messageBox({ messageBox({
className: 'help-text', className: 'help-text',
@ -182,10 +182,10 @@ const sorter = (() => {
}); });
} }
function sortInit() { function init() {
$('#sort-select').addEventListener('change', manageSort); $('#sort-select').addEventListener('change', manageChange);
$('#sorter-help').onclick = showSortHelp; $('#sorter-help').onclick = showHelp;
addSortOptions(); addOptions();
} }
function updateStripes() { function updateStripes() {
@ -199,5 +199,5 @@ const sorter = (() => {
}); });
} }
return {sortInit, updateSort, sortStyles, updateStripes}; return {init, update, sort, updateStripes};
}); });

View File

@ -144,7 +144,7 @@ function reportUpdateState(state, style, details) {
} }
if (filtersSelector.hide) { if (filtersSelector.hide) {
filterAndAppend({entry}); filterAndAppend({entry});
sorter().updateSort(); sorter().update();
} }
} }