blob: 48a6b656c50a670d28f93cbfe2d009d63d8140be [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
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 Lippe0830b3d2019-10-03 13:20:0730
Paul Lewis17e384e2020-01-08 15:46:5131import * as Common from '../common/common.js';
Paul Lewis9950e182019-12-16 16:06:0732import {CheckboxLabel} from './UIUtils.js';
Blink Reformat4c46d092018-04-07 15:32:3733
34/**
35 * @param {string} name
Paul Lewis17e384e2020-01-08 15:46:5136 * @param {!Common.Settings.Setting} setting
Blink Reformat4c46d092018-04-07 15:32:3737 * @param {boolean=} omitParagraphElement
38 * @param {string=} tooltip
39 * @return {!Element}
40 */
Tim van der Lippe0830b3d2019-10-03 13:20:0741export const createSettingCheckbox = function(name, setting, omitParagraphElement, tooltip) {
Paul Lewis9950e182019-12-16 16:06:0742 const label = CheckboxLabel.create(name);
Tim van der Lippe1d6e57a2019-09-30 11:55:3443 if (tooltip) {
Blink Reformat4c46d092018-04-07 15:32:3744 label.title = tooltip;
Tim van der Lippe1d6e57a2019-09-30 11:55:3445 }
Blink Reformat4c46d092018-04-07 15:32:3746
47 const input = label.checkboxElement;
48 input.name = name;
Tim van der Lippe0830b3d2019-10-03 13:20:0749 bindCheckbox(input, setting);
Blink Reformat4c46d092018-04-07 15:32:3750
Tim van der Lippe1d6e57a2019-09-30 11:55:3451 if (omitParagraphElement) {
Blink Reformat4c46d092018-04-07 15:32:3752 return label;
Tim van der Lippe1d6e57a2019-09-30 11:55:3453 }
Blink Reformat4c46d092018-04-07 15:32:3754
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 Lewis17e384e2020-01-08 15:46:5163 * @param {!Common.Settings.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:1564 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:3765 * @return {!Element}
66 */
Tim van der Lippec96ccd92019-11-29 16:23:5467const createSettingSelect = function(name, options, setting, subtitle) {
Michael Liao (WPT)be2a1f62019-09-06 18:19:1568 const settingSelectElement = createElement('p');
69 const label = settingSelectElement.createChild('label');
70 const select = settingSelectElement.createChild('select', 'chrome-select');
Chandani Shrestha83bd7c92019-06-11 21:21:5971 label.textContent = name;
Michael Liao (WPT)be2a1f62019-09-06 18:19:1572 if (subtitle) {
73 settingSelectElement.classList.add('chrome-select-label');
74 label.createChild('p').textContent = subtitle;
75 }
Chandani Shrestha83bd7c92019-06-11 21:21:5976 UI.ARIAUtils.bindLabelToControl(label, select);
Blink Reformat4c46d092018-04-07 15:32:3777
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 Lewis17e384e2020-01-08 15:46:5181 const optionName = option.raw ? option.text : Common.UIString.UIString(option.text);
Blink Reformat4c46d092018-04-07 15:32:3782 select.add(new Option(optionName, option.value));
83 }
84
85 setting.addChangeListener(settingChanged);
86 settingChanged();
87 select.addEventListener('change', selectChanged, false);
Michael Liao (WPT)be2a1f62019-09-06 18:19:1588 return settingSelectElement;
Blink Reformat4c46d092018-04-07 15:32:3789
90 function settingChanged() {
91 const newValue = setting.get();
92 for (let i = 0; i < options.length; i++) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3493 if (options[i].value === newValue) {
Blink Reformat4c46d092018-04-07 15:32:3794 select.selectedIndex = i;
Tim van der Lippe1d6e57a2019-09-30 11:55:3495 }
Blink Reformat4c46d092018-04-07 15:32:3796 }
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 Lewis17e384e2020-01-08 15:46:51107 * @param {!Common.Settings.Setting} setting
Blink Reformat4c46d092018-04-07 15:32:37108 */
Tim van der Lippe0830b3d2019-10-03 13:20:07109export const bindCheckbox = function(input, setting) {
Blink Reformat4c46d092018-04-07 15:32:37110 function settingChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34111 if (input.checked !== setting.get()) {
Blink Reformat4c46d092018-04-07 15:32:37112 input.checked = setting.get();
Tim van der Lippe1d6e57a2019-09-30 11:55:34113 }
Blink Reformat4c46d092018-04-07 15:32:37114 }
115 setting.addChangeListener(settingChanged);
116 settingChanged();
117
118 function inputChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34119 if (setting.get() !== input.checked) {
Blink Reformat4c46d092018-04-07 15:32:37120 setting.set(input.checked);
Tim van der Lippe1d6e57a2019-09-30 11:55:34121 }
Blink Reformat4c46d092018-04-07 15:32:37122 }
123 input.addEventListener('change', inputChanged, false);
124};
125
126/**
127 * @param {string} name
128 * @param {!Element} element
129 * @return {!Element}
130 */
Tim van der Lippe0830b3d2019-10-03 13:20:07131export const createCustomSetting = function(name, element) {
Blink Reformat4c46d092018-04-07 15:32:37132 const p = createElement('p');
133 const fieldsetElement = p.createChild('fieldset');
Chandani Shrestha83bd7c92019-06-11 21:21:59134 const label = fieldsetElement.createChild('label');
135 label.textContent = name;
136 UI.ARIAUtils.bindLabelToControl(label, element);
Blink Reformat4c46d092018-04-07 15:32:37137 fieldsetElement.appendChild(element);
138 return p;
139};
140
141/**
Paul Lewis17e384e2020-01-08 15:46:51142 * @param {!Common.Settings.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:15143 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:37144 * @return {?Element}
145 */
Tim van der Lippe0830b3d2019-10-03 13:20:07146export const createControlForSetting = function(setting, subtitle) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34147 if (!setting.extension()) {
Blink Reformat4c46d092018-04-07 15:32:37148 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34149 }
Blink Reformat4c46d092018-04-07 15:32:37150 const descriptor = setting.extension().descriptor();
Paul Lewis17e384e2020-01-08 15:46:51151 const uiTitle = Common.UIString.UIString(setting.title() || '');
Blink Reformat4c46d092018-04-07 15:32:37152 switch (descriptor['settingType']) {
153 case 'boolean':
Tim van der Lippe0830b3d2019-10-03 13:20:07154 return createSettingCheckbox(uiTitle, setting);
Blink Reformat4c46d092018-04-07 15:32:37155 case 'enum':
Tim van der Lippe1d6e57a2019-09-30 11:55:34156 if (Array.isArray(descriptor['options'])) {
Tim van der Lippe0830b3d2019-10-03 13:20:07157 return createSettingSelect(uiTitle, descriptor['options'], setting, subtitle);
Tim van der Lippe1d6e57a2019-09-30 11:55:34158 }
Blink Reformat4c46d092018-04-07 15:32:37159 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 Lippe0830b3d2019-10-03 13:20:07170export class SettingUI {
Blink Reformat4c46d092018-04-07 15:32:37171 /**
172 * @return {?Element}
173 */
174 settingElement() {}
Tim van der Lippe0830b3d2019-10-03 13:20:07175}