options.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. setupLivePrefs();
  3. setupRadioButtons();
  4. enforceInputRange($('#popupWidth'));
  5. if (!FIREFOX && !OPERA) {
  6. const block = $('#advanced');
  7. block.classList.add('collapsible', 'collapsed');
  8. block.onclick = event => {
  9. if (block.classList.contains('collapsed') || event.target.closest('h1')) {
  10. block.classList.toggle('collapsed');
  11. }
  12. };
  13. }
  14. // actions
  15. document.onclick = e => {
  16. const target = e.target.closest('[data-cmd]');
  17. if (!target) {
  18. return;
  19. }
  20. // prevent double-triggering in case a sub-element was clicked
  21. e.stopPropagation();
  22. switch (target.dataset.cmd) {
  23. case 'open-manage':
  24. openURL({url: 'manage.html'});
  25. break;
  26. case 'check-updates':
  27. checkUpdates();
  28. break;
  29. case 'open-keyboard':
  30. openURL({url: URLS.configureCommands});
  31. e.preventDefault();
  32. break;
  33. case 'reset':
  34. $$('input')
  35. .filter(input => input.id in prefs.readOnlyValues)
  36. .forEach(input => prefs.reset(input.id));
  37. break;
  38. }
  39. };
  40. function checkUpdates() {
  41. let total = 0;
  42. let checked = 0;
  43. let updated = 0;
  44. const maxWidth = $('#update-progress').parentElement.clientWidth;
  45. BG.updater.checkAllStyles({observer});
  46. function observer(state, value) {
  47. switch (state) {
  48. case BG.updater.COUNT:
  49. total = value;
  50. document.body.classList.add('update-in-progress');
  51. break;
  52. case BG.updater.UPDATED:
  53. updated++;
  54. // fallthrough
  55. case BG.updater.SKIPPED:
  56. checked++;
  57. break;
  58. case BG.updater.DONE:
  59. document.body.classList.remove('update-in-progress');
  60. return;
  61. }
  62. $('#update-progress').style.width = Math.round(checked / total * maxWidth) + 'px';
  63. $('#updates-installed').dataset.value = updated || '';
  64. }
  65. }
  66. function setupRadioButtons() {
  67. const sets = {};
  68. const onChange = function () {
  69. const newValue = sets[this.name].indexOf(this);
  70. if (newValue >= 0 && prefs.get(this.name) !== newValue) {
  71. prefs.set(this.name, newValue);
  72. }
  73. };
  74. // group all radio-inputs by name="prefName" attribute
  75. for (const el of $$('input[type="radio"][name]')) {
  76. (sets[el.name] = sets[el.name] || []).push(el);
  77. el.addEventListener('change', onChange);
  78. }
  79. // select the input corresponding to the actual pref value
  80. for (const name in sets) {
  81. sets[name][prefs.get(name)].checked = true;
  82. }
  83. // listen to pref changes and update the values
  84. prefs.subscribe(Object.keys(sets), (key, value) => {
  85. sets[key][value].checked = true;
  86. });
  87. }