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