2017-03-16 13:36:33 +00:00
|
|
|
/* globals configureCommands */
|
2017-02-14 15:35:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-25 06:30:34 +00:00
|
|
|
|
2017-02-14 15:35:53 +00:00
|
|
|
function restore () {
|
|
|
|
chrome.runtime.getBackgroundPage(bg => {
|
2017-03-25 06:30:34 +00:00
|
|
|
$('#badgeDisabled').value = bg.prefs.get('badgeDisabled');
|
|
|
|
$('#badgeNormal').value = bg.prefs.get('badgeNormal');
|
|
|
|
$('#popupWidth').value = localStorage.getItem('popupWidth') || '246';
|
|
|
|
$('#updateInterval').value = bg.prefs.get('updateInterval');
|
2017-03-21 01:32:38 +00:00
|
|
|
enforceValueRange('popupWidth');
|
2017-02-14 15:35:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-25 06:30:34 +00:00
|
|
|
|
2017-02-14 15:35:53 +00:00
|
|
|
function save () {
|
|
|
|
chrome.runtime.getBackgroundPage(bg => {
|
2017-03-25 06:30:34 +00:00
|
|
|
bg.prefs.set('badgeDisabled', $('#badgeDisabled').value);
|
|
|
|
bg.prefs.set('badgeNormal', $('#badgeNormal').value);
|
2017-03-21 01:32:38 +00:00
|
|
|
localStorage.setItem('popupWidth', enforceValueRange('popupWidth'));
|
2017-02-14 15:35:53 +00:00
|
|
|
bg.prefs.set(
|
|
|
|
'updateInterval',
|
2017-03-25 06:30:34 +00:00
|
|
|
Math.max(0, +$('#updateInterval').value)
|
2017-02-14 15:35:53 +00:00
|
|
|
);
|
|
|
|
// display notification
|
2017-03-25 06:30:34 +00:00
|
|
|
let status = $('#status');
|
2017-02-14 15:35:53 +00:00
|
|
|
status.textContent = 'Options saved.';
|
|
|
|
setTimeout(() => status.textContent = '', 750);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-25 06:30:34 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
function enforceValueRange(id) {
|
|
|
|
let element = document.getElementById(id);
|
|
|
|
let value = Number(element.value);
|
|
|
|
const min = Number(element.min);
|
|
|
|
const max = Number(element.max);
|
|
|
|
if (value < min || value > max) {
|
|
|
|
value = Math.max(min, Math.min(max, value));
|
|
|
|
element.value = value;
|
|
|
|
}
|
|
|
|
element.onchange = element.onchange || (() => enforceValueRange(id));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-03-25 06:30:34 +00:00
|
|
|
|
|
|
|
onDOMready().then(restore);
|
|
|
|
$('#save').onclick = save;
|
|
|
|
|
|
|
|
// overwrite the default URL if browser is Opera
|
|
|
|
$('[data-cmd="open-keyboard"]').textContent =
|
|
|
|
configureCommands.url;
|
2017-02-14 15:35:53 +00:00
|
|
|
|
|
|
|
// actions
|
2017-03-25 06:30:34 +00:00
|
|
|
document.onclick = e => {
|
2017-02-14 15:35:53 +00:00
|
|
|
let cmd = e.target.dataset.cmd;
|
|
|
|
let total = 0, updated = 0;
|
|
|
|
|
|
|
|
function update () {
|
2017-03-25 06:30:34 +00:00
|
|
|
$('#update-counter').textContent = `${updated}/${total}`;
|
2017-02-14 15:35:53 +00:00
|
|
|
}
|
|
|
|
function done (target) {
|
|
|
|
target.disabled = false;
|
|
|
|
window.setTimeout(() => {
|
2017-03-25 06:30:34 +00:00
|
|
|
$('#update-counter').textContent = '';
|
2017-02-14 15:35:53 +00:00
|
|
|
}, 750);
|
|
|
|
}
|
|
|
|
|
2017-03-25 06:30:34 +00:00
|
|
|
switch (cmd) {
|
|
|
|
|
|
|
|
case 'open-manage':
|
|
|
|
openURL({url: '/manage.html'});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case'check-updates':
|
2017-02-14 15:35:53 +00:00
|
|
|
e.target.disabled = true;
|
|
|
|
chrome.runtime.getBackgroundPage(bg => {
|
|
|
|
bg.update.perform((cmd, value) => {
|
|
|
|
if (cmd === 'count') {
|
|
|
|
total = value;
|
|
|
|
if (!total) {
|
|
|
|
done(e.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd === 'single-updated' || cmd === 'single-skipped') {
|
|
|
|
updated += 1;
|
|
|
|
if (total && updated === total) {
|
|
|
|
done(e.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// notify the automatic updater to reset the next automatic update accordingly
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
method: 'resetInterval'
|
|
|
|
});
|
2017-03-25 06:30:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'open-keyboard':
|
2017-03-16 13:36:33 +00:00
|
|
|
configureCommands.open();
|
2017-03-25 06:30:34 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-15 05:48:47 +00:00
|
|
|
}
|
2017-03-25 06:30:34 +00:00
|
|
|
};
|