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); |
| 41 | if (tooltip) |
| 42 | label.title = tooltip; |
| 43 | |
| 44 | const input = label.checkboxElement; |
| 45 | input.name = name; |
| 46 | UI.SettingsUI.bindCheckbox(input, setting); |
| 47 | |
| 48 | if (omitParagraphElement) |
| 49 | return label; |
| 50 | |
| 51 | const p = createElement('p'); |
| 52 | p.appendChild(label); |
| 53 | return p; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * @param {string} name |
| 58 | * @param {!Array<!{text: string, value: *, raw: (boolean|undefined)}>} options |
| 59 | * @param {!Common.Setting} setting |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame^] | 60 | * @param {string=} subtitle |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 61 | * @return {!Element} |
| 62 | */ |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame^] | 63 | UI.SettingsUI.createSettingSelect = function(name, options, setting, subtitle) { |
| 64 | const settingSelectElement = createElement('p'); |
| 65 | const label = settingSelectElement.createChild('label'); |
| 66 | const select = settingSelectElement.createChild('select', 'chrome-select'); |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 67 | label.textContent = name; |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame^] | 68 | if (subtitle) { |
| 69 | settingSelectElement.classList.add('chrome-select-label'); |
| 70 | label.createChild('p').textContent = subtitle; |
| 71 | } |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 72 | UI.ARIAUtils.bindLabelToControl(label, select); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | |
| 74 | for (let i = 0; i < options.length; ++i) { |
| 75 | // The "raw" flag indicates text is non-i18n-izable. |
| 76 | const option = options[i]; |
| 77 | const optionName = option.raw ? option.text : Common.UIString(option.text); |
| 78 | select.add(new Option(optionName, option.value)); |
| 79 | } |
| 80 | |
| 81 | setting.addChangeListener(settingChanged); |
| 82 | settingChanged(); |
| 83 | select.addEventListener('change', selectChanged, false); |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame^] | 84 | return settingSelectElement; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | |
| 86 | function settingChanged() { |
| 87 | const newValue = setting.get(); |
| 88 | for (let i = 0; i < options.length; i++) { |
| 89 | if (options[i].value === newValue) |
| 90 | select.selectedIndex = i; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | function selectChanged() { |
| 95 | // Don't use event.target.value to avoid conversion of the value to string. |
| 96 | setting.set(options[select.selectedIndex].value); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | /** |
| 101 | * @param {!Element} input |
| 102 | * @param {!Common.Setting} setting |
| 103 | */ |
| 104 | UI.SettingsUI.bindCheckbox = function(input, setting) { |
| 105 | function settingChanged() { |
| 106 | if (input.checked !== setting.get()) |
| 107 | input.checked = setting.get(); |
| 108 | } |
| 109 | setting.addChangeListener(settingChanged); |
| 110 | settingChanged(); |
| 111 | |
| 112 | function inputChanged() { |
| 113 | if (setting.get() !== input.checked) |
| 114 | setting.set(input.checked); |
| 115 | } |
| 116 | input.addEventListener('change', inputChanged, false); |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * @param {string} name |
| 121 | * @param {!Element} element |
| 122 | * @return {!Element} |
| 123 | */ |
| 124 | UI.SettingsUI.createCustomSetting = function(name, element) { |
| 125 | const p = createElement('p'); |
| 126 | const fieldsetElement = p.createChild('fieldset'); |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 127 | const label = fieldsetElement.createChild('label'); |
| 128 | label.textContent = name; |
| 129 | UI.ARIAUtils.bindLabelToControl(label, element); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 130 | fieldsetElement.appendChild(element); |
| 131 | return p; |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * @param {!Common.Setting} setting |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame^] | 136 | * @param {string=} subtitle |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 137 | * @return {?Element} |
| 138 | */ |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame^] | 139 | UI.SettingsUI.createControlForSetting = function(setting, subtitle) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 140 | if (!setting.extension()) |
| 141 | return null; |
| 142 | const descriptor = setting.extension().descriptor(); |
| 143 | const uiTitle = Common.UIString(setting.title() || ''); |
| 144 | switch (descriptor['settingType']) { |
| 145 | case 'boolean': |
| 146 | return UI.SettingsUI.createSettingCheckbox(uiTitle, setting); |
| 147 | case 'enum': |
| 148 | if (Array.isArray(descriptor['options'])) |
Michael Liao (WPT) | be2a1f6 | 2019-09-06 18:19:15 | [diff] [blame^] | 149 | return UI.SettingsUI.createSettingSelect(uiTitle, descriptor['options'], setting, subtitle); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 150 | console.error('Enum setting defined without options'); |
| 151 | return null; |
| 152 | default: |
| 153 | console.error('Invalid setting type: ' + descriptor['settingType']); |
| 154 | return null; |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * @interface |
| 160 | */ |
| 161 | UI.SettingUI = function() {}; |
| 162 | |
| 163 | UI.SettingUI.prototype = { |
| 164 | /** |
| 165 | * @return {?Element} |
| 166 | */ |
| 167 | settingElement() {} |
| 168 | }; |