stylus/schedule/ui.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-05-03 14:49:40 +00:00
/* global t, schedule, $, $$ */
2017-05-03 12:21:15 +00:00
'use strict';
2017-05-03 14:49:40 +00:00
schedule.ui = {};
/* get start and end inputs */
schedule.ui.inputs = (parent) => $$('input[type=time]', parent);
/* updating schedule section of a single style */
schedule.ui.update = (request) => {
const parent = $(`[id="style-${request.id}"] .schedule`);
if (parent) {
parent.dataset.active = true;
$('input[type=button]', parent).value = t('scheduleButtonActive');
const [start, end] = schedule.ui.inputs(parent);
start.value = request.start;
end.value = request.end;
}
};
/* display schedule panel and hide it when user selects outside area */
2017-05-03 12:21:15 +00:00
document.addEventListener('click', e => {
const target = e.target;
let parent;
// hide schedule panel
function observe (e) {
if (!parent.contains(e.target)) {
2017-05-03 14:49:40 +00:00
const [start, end] = schedule.ui.inputs(parent);
2017-05-03 12:21:15 +00:00
const id = target.closest('.entry').id.replace('style-', '');
switch ([start.value, end.value].filter(v => v).length) {
case 0:
chrome.runtime.sendMessage({
method: 'schedule',
enabled: false,
id
});
break;
case 1: // when only start or end value is set; display an alert
window.alert(t('scheduleOneEntry'));
2017-05-03 12:21:15 +00:00
[start, end].filter(o => !o.value).forEach(o => o.focus());
return;
default:
if (start.value === end.value) {
window.alert(t('scheduleEqualEntries'));
return start.focus();
}
2017-05-03 12:21:15 +00:00
chrome.runtime.sendMessage({
method: 'schedule',
enabled: true,
id,
start: start.value,
end: end.value
});
}
document.removeEventListener('click', observe);
parent.dataset.edit = false;
}
}
// display schedule panel
if (target.dataset.cmd === 'schedule') {
parent = target.closest('div');
parent.dataset.edit = true;
document.addEventListener('click', observe);
}
});
2017-05-03 14:49:40 +00:00
/* update schedule section on styles ready */
document.addEventListener('styles-ready', () => {
console.log('"styles-ready" is called');
2017-05-03 12:21:15 +00:00
schedule.prefs.getAll(prefs => {
2017-05-03 14:49:40 +00:00
prefs.forEach(([name, pref]) => schedule.ui.update(pref));
2017-05-03 12:21:15 +00:00
});
2017-05-03 14:49:40 +00:00
});
/* update schedule section on style change */
document.addEventListener('style-edited', e => {
console.log('"style-edited" is called');
const id = e.detail.id;
const name = schedule.prefs.name(id);
schedule.prefs.get(name, prefs => {
const pref = prefs[name];
if (pref) {
schedule.ui.update(pref);
}
});
});