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