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