Fix bulk updates

This commit is contained in:
Rob Garrison 2019-01-01 20:10:38 -06:00
parent a0ba63bb19
commit 403049692c
7 changed files with 55 additions and 34 deletions

View File

@ -31,7 +31,7 @@
const retrying = new Set();
API_METHODS.updateCheckAll = checkAllStyles;
API_METHODS.updateCheckBulk = checkBulkStyles;
API_METHODS.updateCheck = checkStyle;
API_METHODS.getUpdaterStates = () => STATES;
@ -39,18 +39,22 @@
schedule();
chrome.alarms.onAlarm.addListener(onAlarm);
return {checkAllStyles, checkStyle, STATES};
return {checkBulkStyles, checkStyle, STATES};
function checkAllStyles({
function checkBulkStyles({
save = true,
ignoreDigest,
observe,
styleIds = [],
} = {}) {
resetInterval();
checkingAll = true;
retrying.clear();
const port = observe && chrome.runtime.connect({name: 'updater'});
return styleManager.getAllStyles().then(styles => {
if (styleIds.length) {
styles = styles.filter(style => styleIds.includes(style.id));
}
styles = styles.filter(style => style.updateUrl);
if (port) port.postMessage({count: styles.length});
log('');
@ -247,7 +251,7 @@
}
function onAlarm({name}) {
if (name === ALARM_NAME) checkAllStyles();
if (name === ALARM_NAME) checkBulkStyles();
}
function resetInterval() {

View File

@ -427,7 +427,7 @@
<!-- Bulk update -->
<span data-bulk="update">
<button id="apply-all-updates" class="hidden" i18n-text="applyAllUpdates"></button>
<span id="update-all-no-updates" class="hidden" i18n-text="updateAllCheckSucceededNoUpdate"></span>
<span id="update-all-no-updates" class="tt-e hidden" i18n-text="updateAllCheckSucceededNoUpdate"></span>
<button id="check-all-updates-force" class="hidden" i18n-text="checkAllUpdatesForce"></button>
</span>

View File

@ -1,4 +1,4 @@
/* global $ $$ API t prefs handleEvent installed exportToFile checkUpdate exportDropbox
/* global $ $$ API t prefs handleEvent installed exportToFile checkUpdateBulk exportDropbox
messageBox */
/* exported bulk */
'use strict';
@ -11,6 +11,16 @@ const bulk = {
$('#bulk-actions-apply').onclick = bulk.handleApply;
},
checkApply: () => {
const checkedEntries = $$('.entry-filter-toggle').filter(entry => entry.checked);
if (checkedEntries.length > 0 && $('#bulk-actions-select').value !== '') {
$('#bulk-actions-apply').removeAttribute('disabled');
} else {
$('#bulk-actions-apply').setAttribute('disabled', true);
}
$('#bulk-filter-count').textContent = checkedEntries.length || '';
},
handleSelect: event => {
event.preventDefault();
$$('[data-bulk]').forEach(el => el.classList.add('hidden'));
@ -60,22 +70,25 @@ const bulk = {
return exportToFile(styles);
}
case 'update':
styles = entries.map(entry => entry.styleMeta);
checkUpdate(styles); // TO DO: don't check all styles
checkUpdateBulk();
break;
// case 'reset':
// break;
case 'delete':
case 'delete': {
styles = entries.reduce((acc, entry) => {
const style = entry.styleMeta;
acc[style.name] = style.id;
acc[style.id] = style.name;
return acc;
}, {});
bulk.deleteBulk(event, styles);
$('#toggle-all-filters').checked = false;
const toggle = $('#toggle-all-filters');
toggle.checked = false;
toggle.indeterminate = false;
break;
}
}
$('#bulk-actions-select').value = '';
$('#bulk-actions-apply').setAttribute('disabled', true);
},
updateBulkFilters: ({target}) => {
@ -102,27 +115,20 @@ const bulk = {
}
}
}
const count = $$('.entry-filter-toggle').filter(entry => entry.checked).length;
$('#bulk-filter-count').textContent = count || '';
if (count > 0 && $('#bulk-actions-select').value !== '') {
$('#bulk-actions-apply').removeAttribute('disabled');
} else {
$('#bulk-actions-apply').setAttribute('disabled', true);
}
bulk.checkApply();
}
},
deleteBulk: (event, styles) => {
messageBox({
title: t('deleteStyleConfirm'),
contents: Object.keys(styles).join(', '),
contents: Object.values(styles).join(', '),
className: 'danger center',
buttons: [t('confirmDelete'), t('confirmCancel')],
})
.then(({button}) => {
if (button === 0) {
Object.values(styles).forEach(id => API.deleteStyle(id));
Object.keys(styles).forEach(id => API.deleteStyle(Number(id)));
installed.dataset.total -= Object.keys(styles).length;
bulk.updateBulkFilters({target: $('#toggle-all-filters')});
}

View File

@ -8,7 +8,7 @@ global messageBox getStyleWithNoCode
URLS enforceInputRange t
getOwnTab getActiveTab openURL animateElement sessionStorageHash debounce
scrollElementIntoView FIREFOX
UI
UI bulk
*/
/* exported updateInjectionOrder */
'use strict';
@ -305,7 +305,7 @@ Object.assign(handleEvent, {
});
}
}).then(() => {
const box = $('#message-box')
const box = $('#message-box');
box.removeEventListener('change', handleEvent.manageFavicons);
box.removeEventListener('input', handleEvent.manageFavicons);
});
@ -339,6 +339,8 @@ function handleUpdate(style, {reason, method} = {}) {
}
entry = entry || UI.createStyleElement({style});
if (oldEntry) {
// Make sure to update the filter checkbox since it's state isn't saved to the style
$('.entry-filter-toggle', entry).checked = $('.entry-filter-toggle', oldEntry).checked;
if (oldEntry.styleNameLowerCase === entry.styleNameLowerCase) {
installed.replaceChild(entry, oldEntry);
} else {
@ -349,6 +351,7 @@ function handleUpdate(style, {reason, method} = {}) {
handleUpdateInstalled(entry, reason);
}
filterAndAppend({entry}).then(sorter.update);
if (!entry.matches('.hidden') && reason !== 'import') {
animateElement(entry);
requestAnimationFrame(() => scrollElementIntoView(entry));
@ -421,7 +424,6 @@ function switchUI({styleOnly} = {}) {
for (const targets of $$('.entry .targets')) {
const items = $$('.target', targets);
const extra = $('.applies-to-extra', targets);
const x = items.length === 54;
items.splice(0, UI.targets).forEach(el => {
if (!el.parentElement.classList.contains('targets')) {
targets.insertBefore(el, extra);

View File

@ -32,13 +32,13 @@ const UI = {
$('.ext-version').textContent = `v${chrome.runtime.getManifest().version}`;
// translate CSS manually
// #update-all-no-updates[data-skipped-edited="true"]::after {
// content: " ${t('updateAllCheckSucceededSomeEdited')}";
// }
document.head.appendChild($create('style', `
body.all-styles-hidden-by-filters #installed:after {
content: "${t('filteredStylesAllHidden')}";
}
#update-all-no-updates[data-skipped-edited="true"]::after {
content: " ${t('updateAllCheckSucceededSomeEdited')}";
}
`));
},

View File

@ -3,9 +3,10 @@
/* exported handleUpdateInstalled */
'use strict';
let updateTimer;
onDOMready().then(() => {
// $('#check-all-updates').onclick = checkUpdateAll;
$('#check-all-updates-force').onclick = checkUpdateAll;
$('#check-all-updates-force').onclick = checkUpdateBulk;
$('#apply-all-updates').onclick = applyUpdateAll;
$('#update-history').onclick = showUpdateHistory;
});
@ -27,17 +28,18 @@ function applyUpdateAll() {
}
function checkUpdateAll() {
function checkUpdateBulk() {
clearTimeout(updateTimer);
document.body.classList.add('update-in-progress');
// const btnCheck = $('#check-all-updates');
const btnCheckForce = $('#check-all-updates-force');
const btnApply = $('#apply-all-updates');
const noUpdates = $('#update-all-no-updates');
const styleIds = $$('.entry-filter-toggle:checked').map(el => el.closest('.entry').styleMeta.id);
// btnCheck.disabled = true;
btnCheckForce.classList.add('hidden');
btnApply.classList.add('hidden');
noUpdates.classList.add('hidden');
const ignoreDigest = this && this.id === 'check-all-updates-force';
$$('.updatable:not(.can-update)' + (ignoreDigest ? '' : ':not(.update-problem)'))
.map(checkUpdate);
@ -53,10 +55,11 @@ function checkUpdateAll() {
chrome.runtime.onConnect.removeListener(onConnect);
});
API.updateCheckAll({
API.updateCheckBulk({
save: false,
observe: true,
ignoreDigest,
styleIds,
});
function observer(info, port) {
@ -86,9 +89,15 @@ function checkUpdateAll() {
btnApply.disabled = false;
renderUpdatesOnlyFilter({check: updated + skippedEdited > 0});
if (!updated) {
noUpdates.dataset.skippedEdited = skippedEdited > 0;
if (skippedEdited > 0) {
noUpdates.dataset.title = t('updateAllCheckSucceededSomeEdited');
}
noUpdates.classList.remove('hidden');
btnCheckForce.classList.toggle('hidden', skippedEdited === 0);
updateTimer = setTimeout(() => {
noUpdates.classList.add('hidden');
noUpdates.dataset.title = '';
}, 1e4);
}
}
}

View File

@ -187,7 +187,7 @@ function checkUpdates() {
chrome.runtime.onConnect.removeListener(onConnect);
});
API.updateCheckAll({observe: true});
API.updateCheckBulk({observe: true});
function observer(info) {
if ('count' in info) {