2017-02-14 15:35:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2017-06-28 10:49:04 +00:00
|
|
|
setupRadioButtons();
|
2017-04-05 13:14:59 +00:00
|
|
|
enforceInputRange($('#popupWidth'));
|
2017-12-12 00:27:28 +00:00
|
|
|
setTimeout(splitLongTooltips);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-09-09 14:21:17 +00:00
|
|
|
if (!FIREFOX && !OPERA) {
|
|
|
|
const block = $('#advanced');
|
2017-11-15 03:25:30 +00:00
|
|
|
block.classList.add('collapsible', 'collapsed');
|
2017-09-09 14:21:17 +00:00
|
|
|
block.onclick = event => {
|
|
|
|
if (block.classList.contains('collapsed') || event.target.closest('h1')) {
|
|
|
|
block.classList.toggle('collapsed');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-14 15:35:53 +00:00
|
|
|
// actions
|
2017-03-25 06:30:34 +00:00
|
|
|
document.onclick = e => {
|
2017-04-14 15:30:09 +00:00
|
|
|
const target = e.target.closest('[data-cmd]');
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// prevent double-triggering in case a sub-element was clicked
|
|
|
|
e.stopPropagation();
|
2017-02-14 15:35:53 +00:00
|
|
|
|
2017-04-14 15:30:09 +00:00
|
|
|
switch (target.dataset.cmd) {
|
2017-03-26 02:30:59 +00:00
|
|
|
case 'open-manage':
|
2017-07-12 18:52:44 +00:00
|
|
|
openURL({url: 'manage.html'});
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'check-updates':
|
2017-04-20 18:27:10 +00:00
|
|
|
checkUpdates();
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'open-keyboard':
|
2017-04-29 17:58:53 +00:00
|
|
|
openURL({url: URLS.configureCommands});
|
2017-04-09 06:43:51 +00:00
|
|
|
e.preventDefault();
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
2017-03-25 06:30:34 +00:00
|
|
|
|
2017-04-04 17:21:03 +00:00
|
|
|
case 'reset':
|
|
|
|
$$('input')
|
|
|
|
.filter(input => input.id in prefs.readOnlyValues)
|
|
|
|
.forEach(input => prefs.reset(input.id));
|
|
|
|
break;
|
2017-12-12 00:27:28 +00:00
|
|
|
|
|
|
|
case 'note': {
|
|
|
|
const tooltip = (target.closest('[title]') || {}).title;
|
|
|
|
if (tooltip && 'ontouchstart' in document) {
|
|
|
|
e.preventDefault();
|
|
|
|
target.parentNode.replaceChild($create('.expanded-note', tooltip), target);
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 05:48:47 +00:00
|
|
|
}
|
2017-03-25 06:30:34 +00:00
|
|
|
};
|
2017-04-20 18:27:10 +00:00
|
|
|
|
|
|
|
function checkUpdates() {
|
|
|
|
let total = 0;
|
|
|
|
let checked = 0;
|
|
|
|
let updated = 0;
|
2017-04-24 13:29:48 +00:00
|
|
|
const maxWidth = $('#update-progress').parentElement.clientWidth;
|
|
|
|
BG.updater.checkAllStyles({observer});
|
|
|
|
|
|
|
|
function observer(state, value) {
|
2017-04-20 18:27:10 +00:00
|
|
|
switch (state) {
|
|
|
|
case BG.updater.COUNT:
|
|
|
|
total = value;
|
2017-04-24 13:29:48 +00:00
|
|
|
document.body.classList.add('update-in-progress');
|
2017-04-20 18:27:10 +00:00
|
|
|
break;
|
|
|
|
case BG.updater.UPDATED:
|
|
|
|
updated++;
|
|
|
|
// fallthrough
|
|
|
|
case BG.updater.SKIPPED:
|
|
|
|
checked++;
|
|
|
|
break;
|
2017-04-24 13:29:48 +00:00
|
|
|
case BG.updater.DONE:
|
|
|
|
document.body.classList.remove('update-in-progress');
|
|
|
|
return;
|
2017-04-20 18:27:10 +00:00
|
|
|
}
|
2017-04-24 13:29:48 +00:00
|
|
|
$('#update-progress').style.width = Math.round(checked / total * maxWidth) + 'px';
|
|
|
|
$('#updates-installed').dataset.value = updated || '';
|
|
|
|
}
|
2017-04-20 18:27:10 +00:00
|
|
|
}
|
2017-06-28 10:49:04 +00:00
|
|
|
|
|
|
|
function setupRadioButtons() {
|
|
|
|
const sets = {};
|
2017-07-16 16:49:31 +00:00
|
|
|
const onChange = function () {
|
2017-06-28 10:49:04 +00:00
|
|
|
const newValue = sets[this.name].indexOf(this);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (newValue >= 0 && prefs.get(this.name) !== newValue) {
|
2017-06-28 10:49:04 +00:00
|
|
|
prefs.set(this.name, newValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// group all radio-inputs by name="prefName" attribute
|
|
|
|
for (const el of $$('input[type="radio"][name]')) {
|
|
|
|
(sets[el.name] = sets[el.name] || []).push(el);
|
|
|
|
el.addEventListener('change', onChange);
|
|
|
|
}
|
|
|
|
// select the input corresponding to the actual pref value
|
|
|
|
for (const name in sets) {
|
|
|
|
sets[name][prefs.get(name)].checked = true;
|
|
|
|
}
|
|
|
|
// listen to pref changes and update the values
|
2017-09-03 17:06:20 +00:00
|
|
|
prefs.subscribe(Object.keys(sets), (key, value) => {
|
2017-06-28 10:49:04 +00:00
|
|
|
sets[key][value].checked = true;
|
2017-09-03 17:06:20 +00:00
|
|
|
});
|
2017-06-28 10:49:04 +00:00
|
|
|
}
|
2017-12-12 00:27:28 +00:00
|
|
|
|
|
|
|
function splitLongTooltips() {
|
|
|
|
for (const el of $$('[title]')) {
|
|
|
|
if (el.title.length < 50) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const newTitle = el.title
|
|
|
|
.split('\n')
|
|
|
|
.map(s => s.replace(/([^.][.。?!]|.{50,60},)\s+/g, '$1\n'))
|
|
|
|
.map(s => s.replace(/(.{50,80}(?=.{40,}))\s+/g, '$1\n'))
|
|
|
|
.join('\n');
|
|
|
|
if (newTitle !== el.title) {
|
|
|
|
el.title = newTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|