2017-05-06 15:16:38 +00:00
|
|
|
/* global t, schedule, $, $$, messageBox */
|
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) => {
|
2017-05-06 15:16:38 +00:00
|
|
|
console.log('updating schedule ui', request);
|
2017-05-03 14:49:40 +00:00
|
|
|
const parent = $(`[id="style-${request.id}"] .schedule`);
|
|
|
|
if (parent) {
|
2017-05-06 15:16:38 +00:00
|
|
|
parent.dataset.active = request.enabled;
|
|
|
|
$('input[type=button]', parent).value = t(request.enabled ? 'scheduleButtonActive' : 'scheduleButton');
|
2017-05-03 14:49:40 +00:00
|
|
|
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-', '');
|
2017-05-06 15:16:38 +00:00
|
|
|
let len = [start.value, end.value].filter(v => v).length;
|
|
|
|
len = len === 2 && start.value === end.value ? 3 : len;
|
|
|
|
switch (len) {
|
|
|
|
case 0: // reset schedule for this id
|
2017-05-03 12:21:15 +00:00
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
method: 'schedule',
|
|
|
|
enabled: false,
|
|
|
|
id
|
2017-05-06 15:16:38 +00:00
|
|
|
}, () => {
|
|
|
|
schedule.ui.update({ // reset UI
|
|
|
|
enabled: false,
|
|
|
|
id,
|
|
|
|
start: '',
|
|
|
|
end: ''
|
|
|
|
});
|
2017-05-03 12:21:15 +00:00
|
|
|
});
|
|
|
|
break;
|
2017-05-06 15:16:38 +00:00
|
|
|
case 3: // when both start and end have equal values
|
|
|
|
case 1: // when only start or end value is set
|
|
|
|
return messageBox({
|
|
|
|
title: t('scheduleTitle'),
|
|
|
|
contents: t(len === 1 ? 'scheduleOneEntry' : 'scheduleEqualEntries'),
|
|
|
|
buttons: [t('scheduleButtonGiveUp'), t('scheduleButtonRetry')],
|
|
|
|
onshow: e => e.addEventListener('click', e => e.stopPropagation())
|
|
|
|
}).then((r) => {
|
|
|
|
if (r.button === 1 && len === 1) { // retry
|
|
|
|
[start, end].filter(o => !o.value).forEach(o => o.focus());
|
|
|
|
}
|
|
|
|
else if (r.button === 1 && len === 3) {
|
|
|
|
start.focus();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// clear and hide UI
|
|
|
|
start.value = end.value = '';
|
|
|
|
document.body.click();
|
|
|
|
}
|
|
|
|
});
|
2017-05-03 12:21:15 +00:00
|
|
|
default:
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
method: 'schedule',
|
|
|
|
enabled: true,
|
|
|
|
id,
|
|
|
|
start: start.value,
|
|
|
|
end: end.value
|
2017-05-06 15:16:38 +00:00
|
|
|
}, () => {});
|
2017-05-03 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|