blob: 4d37a194e2527f1de984e50eb2749ea72d824a1b [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
avm99963ad65e752020-09-01 00:13:59 +020035 default:
36 console.warn('Unrecognized option: ' + opt);
37 break;
38 }
39 return;
40 }
41
42 options[opt] = document.getElementById(opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010043 });
44
45 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010046 window.close();
avm999638e030612020-08-20 02:54:17 +020047
48 // In browsers like Firefox window.close is not supported:
49 if (savedSuccessfullyTimeout !== null)
50 window.clearTimeout(savedSuccessfullyTimeout);
51
52 document.getElementById('save-indicator').innerText =
53 '✓ ' + chrome.i18n.getMessage('options_saved');
54 savedSuccessfullyTimeout = window.setTimeout(_ => {
55 document.getElementById('save-indicator').innerText = '';
56 }, 3699);
avm99963cbea3142019-03-28 00:48:15 +010057 });
58}
59
avm99963a3d1ef32019-03-30 23:33:29 +010060function i18n() {
avm99963b69eb3d2020-08-20 02:03:44 +020061 document.querySelectorAll('[data-i18n]')
62 .forEach(
63 el => el.innerHTML = chrome.i18n.getMessage(
64 'options_' + el.getAttribute('data-i18n')));
avm99963a3d1ef32019-03-30 23:33:29 +010065}
66
avm99963b69eb3d2020-08-20 02:03:44 +020067window.addEventListener('load', function() {
avm99963a3d1ef32019-03-30 23:33:29 +010068 i18n();
69
avm99963cbea3142019-03-28 00:48:15 +010070 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010071 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010072
avm99963122dc9b2019-03-30 18:44:18 +010073 Object.keys(defaultOptions).forEach(function(opt) {
avm99963ad65e752020-09-01 00:13:59 +020074 if (deprecatedOptions.includes(opt)) return;
75
76 if (specialOptions.includes(opt)) {
77 switch (opt) {
78 case 'profileindicatoralt_months':
79 var input = document.createElement('input');
80 input.type = 'number';
81 input.id = 'profileindicatoralt_months';
82 input.max = '12';
83 input.min = '1';
84 input.value = items[opt];
85 input.required = true;
86 document.getElementById('profileindicatoralt_months--container')
87 .appendChild(input);
88 break;
89
avm999630bc113a2020-09-07 13:02:11 +020090 case 'ccdarktheme_mode':
91 var select = document.createElement('select');
92 select.id = 'ccdarktheme_mode';
93
94 const modes = ['switch', 'system'];
95 for (const mode of modes) {
96 var modeOption = document.createElement('option');
97 modeOption.value = mode;
98 modeOption.textContent =
99 chrome.i18n.getMessage('options_ccdarktheme_mode_' + mode);
100 if (items.ccdarktheme_mode == mode) modeOption.selected = true;
101 select.appendChild(modeOption);
102 }
103
104 document.getElementById('ccdarktheme_mode--container')
105 .appendChild(select);
106 break;
107
108 // This option is controlled directly in the Community Console.
109 case 'ccdarktheme_switch_enabled':
110 break;
111
avm99963ad65e752020-09-01 00:13:59 +0200112 default:
113 console.warn('Unrecognized option: ' + opt);
114 break;
115 }
116 return;
avm99963122dc9b2019-03-30 18:44:18 +0100117 }
avm99963ad65e752020-09-01 00:13:59 +0200118
119 if (items[opt] === true) document.getElementById(opt).checked = true;
avm99963122dc9b2019-03-30 18:44:18 +0100120 });
avm99963cbea3142019-03-28 00:48:15 +0100121
avm99963ad65e752020-09-01 00:13:59 +0200122 exclusiveOptions.forEach(exclusive => {
123 exclusive.forEach(
124 el => document.getElementById(el).addEventListener('change', e => {
125 if (document.getElementById(exclusive[0]).checked &&
126 document.getElementById(exclusive[1]).checked) {
127 document
128 .getElementById(
129 exclusive[(e.currentTarget.id == exclusive[0] ? 1 : 0)])
130 .checked = false;
131 }
132 }));
133 });
avm99963b69eb3d2020-08-20 02:03:44 +0200134 document.querySelector('#save').addEventListener('click', save);
avm99963cbea3142019-03-28 00:48:15 +0100135 });
136});