stylus/manage/bulk-actions.js

146 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2019-01-02 02:10:38 +00:00
/* global $ $$ API t prefs handleEvent installed exportToFile checkUpdateBulk exportDropbox
2019-01-01 12:59:59 +00:00
messageBox */
2018-12-31 05:45:14 +00:00
/* exported bulk */
'use strict';
const bulk = {
init: () => {
document.addEventListener('change', bulk.updateBulkFilters);
$('#bulk-actions-select').onchange = bulk.handleSelect;
$('#bulk-actions-apply').onclick = bulk.handleApply;
},
// Update all button in header
updateAll: () => {
const toggle = $('#toggle-all-filters');
toggle.checked = false; // ensure click will check all styles
toggle.click();
checkUpdateBulk();
},
2019-01-02 02:10:38 +00:00
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 || '';
},
2018-12-31 05:45:14 +00:00
handleSelect: event => {
event.preventDefault();
$$('[data-bulk]').forEach(el => el.classList.add('hidden'));
2019-01-01 12:59:59 +00:00
2018-12-31 05:45:14 +00:00
switch (event.target.value) {
case 'enable':
break;
case 'disable':
break;
case 'export':
$('[data-bulk="export"]').classList.remove('hidden');
break;
case 'update':
$('[data-bulk="update"]').classList.remove('hidden');
break;
2019-01-01 14:05:19 +00:00
// case 'reset':
// break;
2018-12-31 05:45:14 +00:00
case 'delete':
break;
}
},
handleApply: event => {
event.preventDefault();
let styles;
const action = $('#bulk-actions-select').value;
const entries = $$('.entry-filter-toggle:checked').map(el => el.closest('.entry'));
switch (action) {
case 'enable':
case 'disable': {
const isEnabled = action === 'enable';
entries.forEach(entry => {
const box = $('.entry-state-toggle', entry);
entry.classList.toggle('enable', isEnabled);
box.checked = isEnabled;
handleEvent.toggle.call(box, event, entry);
});
break;
}
2019-01-01 12:59:59 +00:00
case 'export': {
2018-12-31 05:45:14 +00:00
styles = entries.map(entry => entry.styleMeta);
2019-01-01 12:59:59 +00:00
const destination = prefs.get('manage.export.destination');
if (destination === 'dropbox') {
return exportDropbox(styles);
}
return exportToFile(styles);
}
2018-12-31 05:45:14 +00:00
case 'update':
2019-01-02 02:10:38 +00:00
checkUpdateBulk();
2018-12-31 05:45:14 +00:00
break;
2019-01-01 14:05:19 +00:00
// case 'reset':
// break;
2019-01-02 02:10:38 +00:00
case 'delete': {
2018-12-31 05:45:14 +00:00
styles = entries.reduce((acc, entry) => {
const style = entry.styleMeta;
2019-01-02 02:10:38 +00:00
acc[style.id] = style.name;
2018-12-31 05:45:14 +00:00
return acc;
}, {});
bulk.deleteBulk(event, styles);
2019-01-02 02:10:38 +00:00
const toggle = $('#toggle-all-filters');
toggle.checked = false;
toggle.indeterminate = false;
2018-12-31 05:45:14 +00:00
break;
2019-01-02 02:10:38 +00:00
}
2018-12-31 05:45:14 +00:00
}
$('#bulk-actions-select').value = '';
2019-01-02 02:10:38 +00:00
$('#bulk-actions-apply').setAttribute('disabled', true);
2018-12-31 05:45:14 +00:00
},
updateBulkFilters: ({target}) => {
// total is undefined until initialized
if (installed.dataset.total) {
// ignore filter checkboxes
if (target.type === 'checkbox' && target.closest('.toggle-all, .entry-filter')) {
2018-12-31 05:45:14 +00:00
const bulk = $('#toggle-all-filters');
const state = target.checked;
const visibleEntries = $$('.entry-filter-toggle')
.filter(entry => !entry.closest('.entry').classList.contains('hidden'));
bulk.indeterminate = false;
if (target === bulk) {
visibleEntries.forEach(entry => {
entry.checked = state;
});
} else {
if (visibleEntries.length === visibleEntries.filter(entry => entry.checked === state).length) {
bulk.checked = state;
} else {
bulk.checked = false;
bulk.indeterminate = true;
}
}
}
2019-01-02 02:10:38 +00:00
bulk.checkApply();
2018-12-31 05:45:14 +00:00
}
},
deleteBulk: (event, styles) => {
messageBox({
title: t('deleteStyleConfirm'),
2019-01-02 02:10:38 +00:00
contents: Object.values(styles).join(', '),
2018-12-31 05:45:14 +00:00
className: 'danger center',
buttons: [t('confirmDelete'), t('confirmCancel')],
})
.then(({button}) => {
if (button === 0) {
2019-01-02 02:10:38 +00:00
Object.keys(styles).forEach(id => API.deleteStyle(Number(id)));
2018-12-31 05:45:14 +00:00
installed.dataset.total -= Object.keys(styles).length;
bulk.updateBulkFilters({target: $('#toggle-all-filters')});
}
});
}
2019-01-01 12:59:59 +00:00
};