Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | UI.SettingsUI = {}; |
| 31 | |
| 32 | /** |
| 33 | * @param {string} name |
| 34 | * @param {!Common.Setting} setting |
| 35 | * @param {boolean=} omitParagraphElement |
| 36 | * @param {string=} tooltip |
| 37 | * @return {!Element} |
| 38 | */ |
| 39 | UI.SettingsUI.createSettingCheckbox = function(name, setting, omitParagraphElement, tooltip) { |
| 40 | const label = UI.CheckboxLabel.create(name); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 41 | if (tooltip) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | label.title = tooltip; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 43 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | |
| 45 | const input = label.checkboxElement; |
| 46 | input.name = name; |
| 47 | UI.SettingsUI.bindCheckbox(input, setting); |
| 48 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 49 | if (omitParagraphElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 50 | return label; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 51 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 52 | |
| 53 | const p = createElement('p'); |
| 54 | p.appendChild(label); |
| 55 | return p; |
| 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * @param {string} name |
| 60 | * @param {!Array<!{text: string, value: *, raw: (boolean|undefined)}>} options |
| 61 | * @param {!Common.Setting} setting |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame] | 62 | * @param {string=} subtitle |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 63 | * @return {!Element} |
| 64 | */ |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame] | 65 | UI.SettingsUI.createSettingSelect = function(name, options, setting, subtitle) { |
| 66 | const settingSelectElement = createElement('p'); |
| 67 | const label = settingSelectElement.createChild('label'); |
| 68 | const select = settingSelectElement.createChild('select', 'chrome-select'); |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 69 | label.textContent = name; |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame] | 70 | if (subtitle) { |
| 71 | settingSelectElement.classList.add('chrome-select-label'); |
| 72 | label.createChild('p').textContent = subtitle; |
| 73 | } |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 74 | UI.ARIAUtils.bindLabelToControl(label, select); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | |
| 76 | for (let i = 0; i < options.length; ++i) { |
| 77 | // The "raw" flag indicates text is non-i18n-izable. |
| 78 | const option = options[i]; |
| 79 | const optionName = option.raw ? option.text : Common.UIString(option.text); |
| 80 | select.add(new Option(optionName, option.value)); |
| 81 | } |
| 82 | |
| 83 | setting.addChangeListener(settingChanged); |
| 84 | settingChanged(); |
| 85 | select.addEventListener('change', selectChanged, false); |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame] | 86 | return settingSelectElement; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 87 | |
| 88 | function settingChanged() { |
| 89 | const newValue = setting.get(); |
| 90 | for (let i = 0; i < options.length; i++) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 91 | if (options[i].value === newValue) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 92 | select.selectedIndex = i; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 93 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | function selectChanged() { |
| 98 | // Don't use event.target.value to avoid conversion of the value to string. |
| 99 | setting.set(options[select.selectedIndex].value); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * @param {!Element} input |
| 105 | * @param {!Common.Setting} setting |
| 106 | */ |
| 107 | UI.SettingsUI.bindCheckbox = function(input, setting) { |
| 108 | function settingChanged() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 109 | if (input.checked !== setting.get()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 110 | input.checked = setting.get(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 111 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 112 | } |
| 113 | setting.addChangeListener(settingChanged); |
| 114 | settingChanged(); |
| 115 | |
| 116 | function inputChanged() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 117 | if (setting.get() !== input.checked) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 118 | setting.set(input.checked); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 119 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | } |
| 121 | input.addEventListener('change', inputChanged, false); |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * @param {string} name |
| 126 | * @param {!Element} element |
| 127 | * @return {!Element} |
| 128 | */ |
| 129 | UI.SettingsUI.createCustomSetting = function(name, element) { |
| 130 | const p = createElement('p'); |
| 131 | const fieldsetElement = p.createChild('fieldset'); |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 132 | const label = fieldsetElement.createChild('label'); |
| 133 | label.textContent = name; |
| 134 | UI.ARIAUtils.bindLabelToControl(label, element); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 135 | fieldsetElement.appendChild(element); |
| 136 | return p; |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * @param {!Common.Setting} setting |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame] | 141 | * @param {string=} subtitle |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 142 | * @return {?Element} |
| 143 | */ |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame] | 144 | UI.SettingsUI.createControlForSetting = function(setting, subtitle) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 145 | if (!setting.extension()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 146 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 147 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | const descriptor = setting.extension().descriptor(); |
| 149 | const uiTitle = Common.UIString(setting.title() || ''); |
| 150 | switch (descriptor['settingType']) { |
| 151 | case 'boolean': |
| 152 | return UI.SettingsUI.createSettingCheckbox(uiTitle, setting); |
| 153 | case 'enum': |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 154 | if (Array.isArray(descriptor['options'])) { |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame] | 155 | return UI.SettingsUI.createSettingSelect(uiTitle, descriptor['options'], setting, subtitle); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 156 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 157 | console.error('Enum setting defined without options'); |
| 158 | return null; |
| 159 | default: |
| 160 | console.error('Invalid setting type: ' + descriptor['settingType']); |
| 161 | return null; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | /** |
| 166 | * @interface |
| 167 | */ |
| 168 | UI.SettingUI = function() {}; |
| 169 | |
| 170 | UI.SettingUI.prototype = { |
| 171 | /** |
| 172 | * @return {?Element} |
| 173 | */ |
| 174 | settingElement() {} |
| 175 | }; |