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 | 8e0c100 | 2020-12-03 16:54:20 +0100 | [diff] [blame] | 35 | case 'ccdragndropfix': |
| 36 | options[opt] = document.getElementById(opt).checked || false; |
| 37 | break; |
| 38 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 39 | default: |
| 40 | console.warn('Unrecognized option: ' + opt); |
| 41 | break; |
| 42 | } |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | options[opt] = document.getElementById(opt).checked || false; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 47 | }); |
| 48 | |
| 49 | chrome.storage.sync.set(options, function() { |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 50 | window.close(); |
avm99963 | 8e03061 | 2020-08-20 02:54:17 +0200 | [diff] [blame] | 51 | |
| 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); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 61 | }); |
| 62 | } |
| 63 | |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 64 | function i18n() { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 65 | document.querySelectorAll('[data-i18n]') |
| 66 | .forEach( |
| 67 | el => el.innerHTML = chrome.i18n.getMessage( |
| 68 | 'options_' + el.getAttribute('data-i18n'))); |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 69 | } |
| 70 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 71 | window.addEventListener('load', function() { |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 72 | i18n(); |
| 73 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 74 | chrome.storage.sync.get(null, function(items) { |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 75 | items = cleanUpOptions(items); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 76 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 77 | Object.keys(defaultOptions).forEach(function(opt) { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 78 | 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 | |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 94 | 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 | |
avm99963 | 8e0c100 | 2020-12-03 16:54:20 +0100 | [diff] [blame] | 116 | // 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 | } |
avm99963 | 41498ee | 2020-12-07 11:22:04 +0100 | [diff] [blame] | 127 | break; |
avm99963 | 8e0c100 | 2020-12-03 16:54:20 +0100 | [diff] [blame] | 128 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 129 | default: |
| 130 | console.warn('Unrecognized option: ' + opt); |
| 131 | break; |
| 132 | } |
| 133 | return; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 134 | } |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 135 | |
| 136 | if (items[opt] === true) document.getElementById(opt).checked = true; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 137 | }); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 138 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 139 | 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 | }); |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 151 | document.querySelector('#save').addEventListener('click', save); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 152 | }); |
| 153 | }); |