blob: 22a5550be686b494bda3009de8b44058400122cf [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';
Tim van der Lippeaa76aa22020-02-14 14:38:2432
33import * as ARIAUtils from './ARIAUtils.js';
Paul Lewis9950e182019-12-16 16:06:0734import {CheckboxLabel} from './UIUtils.js';
Blink Reformat4c46d092018-04-07 15:32:3735
36/**
37 * @param {string} name
Paul Lewis17e384e2020-01-08 15:46:5138 * @param {!Common.Settings.Setting} setting
Blink Reformat4c46d092018-04-07 15:32:3739 * @param {boolean=} omitParagraphElement
40 * @param {string=} tooltip
41 * @return {!Element}
42 */
Tim van der Lippe0830b3d2019-10-03 13:20:0743export const createSettingCheckbox = function(name, setting, omitParagraphElement, tooltip) {
Paul Lewis9950e182019-12-16 16:06:0744 const label = CheckboxLabel.create(name);
Tim van der Lippe1d6e57a2019-09-30 11:55:3445 if (tooltip) {
Blink Reformat4c46d092018-04-07 15:32:3746 label.title = tooltip;
Tim van der Lippe1d6e57a2019-09-30 11:55:3447 }
Blink Reformat4c46d092018-04-07 15:32:3748
49 const input = label.checkboxElement;
50 input.name = name;
Tim van der Lippe0830b3d2019-10-03 13:20:0751 bindCheckbox(input, setting);
Blink Reformat4c46d092018-04-07 15:32:3752
Tim van der Lippe1d6e57a2019-09-30 11:55:3453 if (omitParagraphElement) {
Blink Reformat4c46d092018-04-07 15:32:3754 return label;
Tim van der Lippe1d6e57a2019-09-30 11:55:3455 }
Blink Reformat4c46d092018-04-07 15:32:3756
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 Lewis17e384e2020-01-08 15:46:5165 * @param {!Common.Settings.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:1566 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:3767 * @return {!Element}
68 */
Tim van der Lippec96ccd92019-11-29 16:23:5469const createSettingSelect = function(name, options, setting, subtitle) {
Michael Liao (WPT)be2a1f62019-09-06 18:19:1570 const settingSelectElement = createElement('p');
71 const label = settingSelectElement.createChild('label');
72 const select = settingSelectElement.createChild('select', 'chrome-select');
Chandani Shrestha83bd7c92019-06-11 21:21:5973 label.textContent = name;
Michael Liao (WPT)be2a1f62019-09-06 18:19:1574 if (subtitle) {
75 settingSelectElement.classList.add('chrome-select-label');
76 label.createChild('p').textContent = subtitle;
77 }
Tim van der Lippeaa76aa22020-02-14 14:38:2478 ARIAUtils.bindLabelToControl(label, select);
Blink Reformat4c46d092018-04-07 15:32:3779
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 Lewis17e384e2020-01-08 15:46:5183 const optionName = option.raw ? option.text : Common.UIString.UIString(option.text);
Blink Reformat4c46d092018-04-07 15:32:3784 select.add(new Option(optionName, option.value));
85 }
86
87 setting.addChangeListener(settingChanged);
88 settingChanged();
89 select.addEventListener('change', selectChanged, false);
Michael Liao (WPT)be2a1f62019-09-06 18:19:1590 return settingSelectElement;
Blink Reformat4c46d092018-04-07 15:32:3791
92 function settingChanged() {
93 const newValue = setting.get();
94 for (let i = 0; i < options.length; i++) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3495 if (options[i].value === newValue) {
Blink Reformat4c46d092018-04-07 15:32:3796 select.selectedIndex = i;
Tim van der Lippe1d6e57a2019-09-30 11:55:3497 }
Blink Reformat4c46d092018-04-07 15:32:3798 }
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 Lewis17e384e2020-01-08 15:46:51109 * @param {!Common.Settings.Setting} setting
Blink Reformat4c46d092018-04-07 15:32:37110 */
Tim van der Lippe0830b3d2019-10-03 13:20:07111export const bindCheckbox = function(input, setting) {
Blink Reformat4c46d092018-04-07 15:32:37112 function settingChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34113 if (input.checked !== setting.get()) {
Blink Reformat4c46d092018-04-07 15:32:37114 input.checked = setting.get();
Tim van der Lippe1d6e57a2019-09-30 11:55:34115 }
Blink Reformat4c46d092018-04-07 15:32:37116 }
117 setting.addChangeListener(settingChanged);
118 settingChanged();
119
120 function inputChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34121 if (setting.get() !== input.checked) {
Blink Reformat4c46d092018-04-07 15:32:37122 setting.set(input.checked);
Tim van der Lippe1d6e57a2019-09-30 11:55:34123 }
Blink Reformat4c46d092018-04-07 15:32:37124 }
125 input.addEventListener('change', inputChanged, false);
126};
127
128/**
129 * @param {string} name
130 * @param {!Element} element
131 * @return {!Element}
132 */
Tim van der Lippe0830b3d2019-10-03 13:20:07133export const createCustomSetting = function(name, element) {
Blink Reformat4c46d092018-04-07 15:32:37134 const p = createElement('p');
135 const fieldsetElement = p.createChild('fieldset');
Chandani Shrestha83bd7c92019-06-11 21:21:59136 const label = fieldsetElement.createChild('label');
137 label.textContent = name;
Tim van der Lippeaa76aa22020-02-14 14:38:24138 ARIAUtils.bindLabelToControl(label, element);
Blink Reformat4c46d092018-04-07 15:32:37139 fieldsetElement.appendChild(element);
140 return p;
141};
142
143/**
Paul Lewis17e384e2020-01-08 15:46:51144 * @param {!Common.Settings.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:15145 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:37146 * @return {?Element}
147 */
Tim van der Lippe0830b3d2019-10-03 13:20:07148export const createControlForSetting = function(setting, subtitle) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34149 if (!setting.extension()) {
Blink Reformat4c46d092018-04-07 15:32:37150 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34151 }
Blink Reformat4c46d092018-04-07 15:32:37152 const descriptor = setting.extension().descriptor();
Paul Lewis17e384e2020-01-08 15:46:51153 const uiTitle = Common.UIString.UIString(setting.title() || '');
Blink Reformat4c46d092018-04-07 15:32:37154 switch (descriptor['settingType']) {
155 case 'boolean':
Tim van der Lippe0830b3d2019-10-03 13:20:07156 return createSettingCheckbox(uiTitle, setting);
Blink Reformat4c46d092018-04-07 15:32:37157 case 'enum':
Tim van der Lippe1d6e57a2019-09-30 11:55:34158 if (Array.isArray(descriptor['options'])) {
Tim van der Lippe0830b3d2019-10-03 13:20:07159 return createSettingSelect(uiTitle, descriptor['options'], setting, subtitle);
Tim van der Lippe1d6e57a2019-09-30 11:55:34160 }
Blink Reformat4c46d092018-04-07 15:32:37161 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 Lippe0830b3d2019-10-03 13:20:07172export class SettingUI {
Blink Reformat4c46d092018-04-07 15:32:37173 /**
174 * @return {?Element}
175 */
176 settingElement() {}
Tim van der Lippe0830b3d2019-10-03 13:20:07177}