blob: 47cecf6f22a9d25c86aaecd2230c33c368c5ad3e [file] [log] [blame]
avm999638e030612020-08-20 02:54:17 +02001var savedSuccessfullyTimeout = null;
2
avm99963ad65e752020-09-01 00:13:59 +02003const exclusiveOptions = [['thread', 'threadall']];
4
5function save(e) {
avm99963122dc9b2019-03-30 18:44:18 +01006 var options = defaultOptions;
7
avm99963ad65e752020-09-01 00:13:59 +02008 // Validation checks before saving
9 var months = document.getElementById('profileindicatoralt_months');
10 if (!months.checkValidity()) {
11 console.warn(months.validationMessage);
12 return;
13 }
14
15 e.preventDefault();
16
17 // Save
avm99963b69eb3d2020-08-20 02:03:44 +020018 Object.keys(options).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020019 if (deprecatedOptions.includes(opt)) return;
avm99963ad65e752020-09-01 00:13:59 +020020
21 if (specialOptions.includes(opt)) {
22 switch (opt) {
23 case 'profileindicatoralt_months':
24 options[opt] = document.getElementById(opt).value || 12;
25 break;
26
27 default:
28 console.warn('Unrecognized option: ' + opt);
29 break;
30 }
31 return;
32 }
33
34 options[opt] = document.getElementById(opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010035 });
36
37 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010038 window.close();
avm999638e030612020-08-20 02:54:17 +020039
40 // In browsers like Firefox window.close is not supported:
41 if (savedSuccessfullyTimeout !== null)
42 window.clearTimeout(savedSuccessfullyTimeout);
43
44 document.getElementById('save-indicator').innerText =
45 '✓ ' + chrome.i18n.getMessage('options_saved');
46 savedSuccessfullyTimeout = window.setTimeout(_ => {
47 document.getElementById('save-indicator').innerText = '';
48 }, 3699);
avm99963cbea3142019-03-28 00:48:15 +010049 });
50}
51
avm99963a3d1ef32019-03-30 23:33:29 +010052function i18n() {
avm99963b69eb3d2020-08-20 02:03:44 +020053 document.querySelectorAll('[data-i18n]')
54 .forEach(
55 el => el.innerHTML = chrome.i18n.getMessage(
56 'options_' + el.getAttribute('data-i18n')));
avm99963a3d1ef32019-03-30 23:33:29 +010057}
58
avm99963b69eb3d2020-08-20 02:03:44 +020059window.addEventListener('load', function() {
avm99963a3d1ef32019-03-30 23:33:29 +010060 i18n();
61
avm99963cbea3142019-03-28 00:48:15 +010062 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010063 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010064
avm99963122dc9b2019-03-30 18:44:18 +010065 Object.keys(defaultOptions).forEach(function(opt) {
avm99963ad65e752020-09-01 00:13:59 +020066 if (deprecatedOptions.includes(opt)) return;
67
68 if (specialOptions.includes(opt)) {
69 switch (opt) {
70 case 'profileindicatoralt_months':
71 var input = document.createElement('input');
72 input.type = 'number';
73 input.id = 'profileindicatoralt_months';
74 input.max = '12';
75 input.min = '1';
76 input.value = items[opt];
77 input.required = true;
78 document.getElementById('profileindicatoralt_months--container')
79 .appendChild(input);
80 break;
81
82 default:
83 console.warn('Unrecognized option: ' + opt);
84 break;
85 }
86 return;
avm99963122dc9b2019-03-30 18:44:18 +010087 }
avm99963ad65e752020-09-01 00:13:59 +020088
89 if (items[opt] === true) document.getElementById(opt).checked = true;
avm99963122dc9b2019-03-30 18:44:18 +010090 });
avm99963cbea3142019-03-28 00:48:15 +010091
avm99963ad65e752020-09-01 00:13:59 +020092 exclusiveOptions.forEach(exclusive => {
93 exclusive.forEach(
94 el => document.getElementById(el).addEventListener('change', e => {
95 if (document.getElementById(exclusive[0]).checked &&
96 document.getElementById(exclusive[1]).checked) {
97 document
98 .getElementById(
99 exclusive[(e.currentTarget.id == exclusive[0] ? 1 : 0)])
100 .checked = false;
101 }
102 }));
103 });
avm99963b69eb3d2020-08-20 02:03:44 +0200104 document.querySelector('#save').addEventListener('click', save);
avm99963cbea3142019-03-28 00:48:15 +0100105 });
106});