scheduler implementation part/4; using messageBox instead of window.alert; allow users to give up setting a new schedule or modifying existing one; reset the schedule UI on delete event

This commit is contained in:
Jeremy Schomery 2017-05-06 19:46:38 +04:30
parent 52ae781960
commit ec4e0a69f2
5 changed files with 66 additions and 18 deletions

View File

@ -713,10 +713,30 @@
"scheduleButtonActive": { "scheduleButtonActive": {
"message": "Scheduled" "message": "Scheduled"
}, },
"scheduleTitle": {
"message": "Add/Edit this schedule",
"description": "Title for the messageBox"
},
"scheduleButtonRetry": {
"message": "Retry",
"description": "First button for the messageBox"
},
"scheduleButtonGiveUp": {
"message": "I give up ",
"description": "Second button for the messageBox"
},
"scheduleOneEntry": { "scheduleOneEntry": {
"message": "Either clear both start and end values to disable the schedule or set both to enable it" "message": "Either clear both start and end values to disable the schedule or set both values to enable it",
"description": "Description for the messageBox when only one time value is present"
}, },
"scheduleEqualEntries": { "scheduleEqualEntries": {
"message": "Start and end times cannot be equal" "message": "Start and end time values cannot be equal",
"description": "Description for the messageBox when both start and end time values are equal"
},
"scheduleFrom": {
"message": "From:"
},
"scheduleTo": {
"message": "To:"
} }
} }

View File

@ -279,7 +279,9 @@ function onRuntimeMessage(request, sender, sendResponse) {
.catch(() => sendResponse(null)); .catch(() => sendResponse(null));
return KEEP_CHANNEL_OPEN; return KEEP_CHANNEL_OPEN;
case 'schedule': case 'schedule':
schedule.entry(request); schedule.entry(request)
.then(() => sendResponse(true))
.catch(() => sendResponse(false));
return KEEP_CHANNEL_OPEN; return KEEP_CHANNEL_OPEN;
} }
} }

View File

@ -159,6 +159,9 @@ a:hover {
.schedule>input { .schedule>input {
margin-right: 5px; margin-right: 5px;
} }
.schedule span {
margin: 0 5px;
}
.schedule>div { .schedule>div {
position: absolute; position: absolute;
right: 10px; right: 10px;

View File

@ -60,8 +60,9 @@
<div class="schedule" data-edit="false"> <div class="schedule" data-edit="false">
<input type="button" i18n-value="scheduleButton" data-cmd="schedule"> <input type="button" i18n-value="scheduleButton" data-cmd="schedule">
<div> <div>
<span i18n-text="scheduleFrom"></span>
<input type="time"> <input type="time">
- <span i18n-text="scheduleTo"></span>
<input type="time"> <input type="time">
</div> </div>
</div> </div>

View File

@ -1,4 +1,4 @@
/* global t, schedule, $, $$ */ /* global t, schedule, $, $$, messageBox */
'use strict'; 'use strict';
schedule.ui = {}; schedule.ui = {};
@ -8,10 +8,11 @@ schedule.ui.inputs = (parent) => $$('input[type=time]', parent);
/* updating schedule section of a single style */ /* updating schedule section of a single style */
schedule.ui.update = (request) => { schedule.ui.update = (request) => {
console.log('updating schedule ui', request);
const parent = $(`[id="style-${request.id}"] .schedule`); const parent = $(`[id="style-${request.id}"] .schedule`);
if (parent) { if (parent) {
parent.dataset.active = true; parent.dataset.active = request.enabled;
$('input[type=button]', parent).value = t('scheduleButtonActive'); $('input[type=button]', parent).value = t(request.enabled ? 'scheduleButtonActive' : 'scheduleButton');
const [start, end] = schedule.ui.inputs(parent); const [start, end] = schedule.ui.inputs(parent);
start.value = request.start; start.value = request.start;
end.value = request.end; end.value = request.end;
@ -27,30 +28,51 @@ document.addEventListener('click', e => {
if (!parent.contains(e.target)) { if (!parent.contains(e.target)) {
const [start, end] = schedule.ui.inputs(parent); const [start, end] = schedule.ui.inputs(parent);
const id = target.closest('.entry').id.replace('style-', ''); const id = target.closest('.entry').id.replace('style-', '');
switch ([start.value, end.value].filter(v => v).length) { let len = [start.value, end.value].filter(v => v).length;
case 0: len = len === 2 && start.value === end.value ? 3 : len;
switch (len) {
case 0: // reset schedule for this id
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
method: 'schedule', method: 'schedule',
enabled: false, enabled: false,
id id
}, () => {
schedule.ui.update({ // reset UI
enabled: false,
id,
start: '',
end: ''
});
}); });
break; break;
case 1: // when only start or end value is set; display an alert case 3: // when both start and end have equal values
window.alert(t('scheduleOneEntry')); case 1: // when only start or end value is set
[start, end].filter(o => !o.value).forEach(o => o.focus()); return messageBox({
return; 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();
}
});
default: default:
if (start.value === end.value) {
window.alert(t('scheduleEqualEntries'));
return start.focus();
}
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
method: 'schedule', method: 'schedule',
enabled: true, enabled: true,
id, id,
start: start.value, start: start.value,
end: end.value end: end.value
}); }, () => {});
} }
document.removeEventListener('click', observe); document.removeEventListener('click', observe);