blob: 46913d4d099695dbdce48db539ad0e2a6df02be8 [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
avm999630bc113a2020-09-07 13:02:11 +020027 case 'ccdarktheme_mode':
28 options[opt] = document.getElementById(opt).value || 'switch';
29 break;
30
31 // This option is controlled directly in the Community Console.
32 case 'ccdarktheme_switch_enabled':
33 break;
34
avm999638e0c1002020-12-03 16:54:20 +010035 case 'ccdragndropfix':
36 options[opt] = document.getElementById(opt).checked || false;
37 break;
38
avm99963ad65e752020-09-01 00:13:59 +020039 default:
40 console.warn('Unrecognized option: ' + opt);
41 break;
42 }
43 return;
44 }
45
46 options[opt] = document.getElementById(opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010047 });
48
49 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010050 window.close();
avm999638e030612020-08-20 02:54:17 +020051
52 // In browsers like Firefox window.close is not supported:
53 if (savedSuccessfullyTimeout !== null)
54 window.clearTimeout(savedSuccessfullyTimeout);
55
56 document.getElementById('save-indicator').innerText =
57 '✓ ' + chrome.i18n.getMessage('options_saved');
58 savedSuccessfullyTimeout = window.setTimeout(_ => {
59 document.getElementById('save-indicator').innerText = '';
60 }, 3699);
avm99963cbea3142019-03-28 00:48:15 +010061 });
62}
63
avm99963a3d1ef32019-03-30 23:33:29 +010064function i18n() {
avm99963b69eb3d2020-08-20 02:03:44 +020065 document.querySelectorAll('[data-i18n]')
66 .forEach(
67 el => el.innerHTML = chrome.i18n.getMessage(
68 'options_' + el.getAttribute('data-i18n')));
avm99963a3d1ef32019-03-30 23:33:29 +010069}
70
avm99963b69eb3d2020-08-20 02:03:44 +020071window.addEventListener('load', function() {
avm99963a3d1ef32019-03-30 23:33:29 +010072 i18n();
73
avm99963cbea3142019-03-28 00:48:15 +010074 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010075 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010076
avm99963122dc9b2019-03-30 18:44:18 +010077 Object.keys(defaultOptions).forEach(function(opt) {
avm99963ad65e752020-09-01 00:13:59 +020078 if (deprecatedOptions.includes(opt)) return;
79
80 if (specialOptions.includes(opt)) {
81 switch (opt) {
82 case 'profileindicatoralt_months':
83 var input = document.createElement('input');
84 input.type = 'number';
85 input.id = 'profileindicatoralt_months';
86 input.max = '12';
87 input.min = '1';
88 input.value = items[opt];
89 input.required = true;
90 document.getElementById('profileindicatoralt_months--container')
91 .appendChild(input);
92 break;
93
avm999630bc113a2020-09-07 13:02:11 +020094 case 'ccdarktheme_mode':
95 var select = document.createElement('select');
96 select.id = 'ccdarktheme_mode';
97
98 const modes = ['switch', 'system'];
99 for (const mode of modes) {
100 var modeOption = document.createElement('option');
101 modeOption.value = mode;
102 modeOption.textContent =
103 chrome.i18n.getMessage('options_ccdarktheme_mode_' + mode);
104 if (items.ccdarktheme_mode == mode) modeOption.selected = true;
105 select.appendChild(modeOption);
106 }
107
108 document.getElementById('ccdarktheme_mode--container')
109 .appendChild(select);
110 break;
111
112 // This option is controlled directly in the Community Console.
113 case 'ccdarktheme_switch_enabled':
114 break;
115
avm999638e0c1002020-12-03 16:54:20 +0100116 // Firefox doesn't support drag and dropping bookmarks into the text
117 // editor while preserving the bookmark title.
118 case 'ccdragndropfix':
119 var showOption = !isFirefox();
120 if (showOption) {
121 document.getElementById('dragndrop-wrapper')
122 .removeAttribute('hidden');
123
124 if (items[opt] === true)
125 document.getElementById(opt).checked = true;
126 }
avm9996341498ee2020-12-07 11:22:04 +0100127 break;
avm999638e0c1002020-12-03 16:54:20 +0100128
avm99963ad65e752020-09-01 00:13:59 +0200129 default:
130 console.warn('Unrecognized option: ' + opt);
131 break;
132 }
133 return;
avm99963122dc9b2019-03-30 18:44:18 +0100134 }
avm99963ad65e752020-09-01 00:13:59 +0200135
136 if (items[opt] === true) document.getElementById(opt).checked = true;
avm99963122dc9b2019-03-30 18:44:18 +0100137 });
avm99963cbea3142019-03-28 00:48:15 +0100138
avm99963ad65e752020-09-01 00:13:59 +0200139 exclusiveOptions.forEach(exclusive => {
140 exclusive.forEach(
141 el => document.getElementById(el).addEventListener('change', e => {
142 if (document.getElementById(exclusive[0]).checked &&
143 document.getElementById(exclusive[1]).checked) {
144 document
145 .getElementById(
146 exclusive[(e.currentTarget.id == exclusive[0] ? 1 : 0)])
147 .checked = false;
148 }
149 }));
150 });
avm99963b69eb3d2020-08-20 02:03:44 +0200151 document.querySelector('#save').addEventListener('click', save);
avm99963cbea3142019-03-28 00:48:15 +0100152 });
153});