avm99963 | 8e03061 | 2020-08-20 02:54:17 +0200 | [diff] [blame] | 1 | var savedSuccessfullyTimeout = null; |
| 2 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 3 | const exclusiveOptions = [['thread', 'threadall']]; |
| 4 | |
| 5 | function save(e) { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 6 | var options = defaultOptions; |
| 7 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 8 | // 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 |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 18 | Object.keys(options).forEach(function(opt) { |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 19 | if (deprecatedOptions.includes(opt)) return; |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 20 | |
| 21 | if (specialOptions.includes(opt)) { |
| 22 | switch (opt) { |
| 23 | case 'profileindicatoralt_months': |
| 24 | options[opt] = document.getElementById(opt).value || 12; |
| 25 | break; |
| 26 | |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 27 | 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 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 35 | default: |
| 36 | console.warn('Unrecognized option: ' + opt); |
| 37 | break; |
| 38 | } |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | options[opt] = document.getElementById(opt).checked || false; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 43 | }); |
| 44 | |
| 45 | chrome.storage.sync.set(options, function() { |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 46 | window.close(); |
avm99963 | 8e03061 | 2020-08-20 02:54:17 +0200 | [diff] [blame] | 47 | |
| 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); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 57 | }); |
| 58 | } |
| 59 | |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 60 | function i18n() { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 61 | document.querySelectorAll('[data-i18n]') |
| 62 | .forEach( |
| 63 | el => el.innerHTML = chrome.i18n.getMessage( |
| 64 | 'options_' + el.getAttribute('data-i18n'))); |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 65 | } |
| 66 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 67 | window.addEventListener('load', function() { |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 68 | i18n(); |
| 69 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 70 | chrome.storage.sync.get(null, function(items) { |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 71 | items = cleanUpOptions(items); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 72 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 73 | Object.keys(defaultOptions).forEach(function(opt) { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 74 | 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 | |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 90 | 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 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 112 | default: |
| 113 | console.warn('Unrecognized option: ' + opt); |
| 114 | break; |
| 115 | } |
| 116 | return; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 117 | } |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 118 | |
| 119 | if (items[opt] === true) document.getElementById(opt).checked = true; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 120 | }); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 121 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 122 | 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 | }); |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 134 | document.querySelector('#save').addEventListener('click', save); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 135 | }); |
| 136 | }); |