blob: 84859fa109c197ad24b104d9c3be2e2052ddc222 [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 Lewis9950e182019-12-16 16:06:0731import {CheckboxLabel} from './UIUtils.js';
Blink Reformat4c46d092018-04-07 15:32:3732
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 Lippe0830b3d2019-10-03 13:20:0740export const createSettingCheckbox = function(name, setting, omitParagraphElement, tooltip) {
Paul Lewis9950e182019-12-16 16:06:0741 const label = CheckboxLabel.create(name);
Tim van der Lippe1d6e57a2019-09-30 11:55:3442 if (tooltip) {
Blink Reformat4c46d092018-04-07 15:32:3743 label.title = tooltip;
Tim van der Lippe1d6e57a2019-09-30 11:55:3444 }
Blink Reformat4c46d092018-04-07 15:32:3745
46 const input = label.checkboxElement;
47 input.name = name;
Tim van der Lippe0830b3d2019-10-03 13:20:0748 bindCheckbox(input, setting);
Blink Reformat4c46d092018-04-07 15:32:3749
Tim van der Lippe1d6e57a2019-09-30 11:55:3450 if (omitParagraphElement) {
Blink Reformat4c46d092018-04-07 15:32:3751 return label;
Tim van der Lippe1d6e57a2019-09-30 11:55:3452 }
Blink Reformat4c46d092018-04-07 15:32:3753
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)be2a1f62019-09-06 18:19:1563 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:3764 * @return {!Element}
65 */
Tim van der Lippec96ccd92019-11-29 16:23:5466const createSettingSelect = function(name, options, setting, subtitle) {
Michael Liao (WPT)be2a1f62019-09-06 18:19:1567 const settingSelectElement = createElement('p');
68 const label = settingSelectElement.createChild('label');
69 const select = settingSelectElement.createChild('select', 'chrome-select');
Chandani Shrestha83bd7c92019-06-11 21:21:5970 label.textContent = name;
Michael Liao (WPT)be2a1f62019-09-06 18:19:1571 if (subtitle) {
72 settingSelectElement.classList.add('chrome-select-label');
73 label.createChild('p').textContent = subtitle;
74 }
Chandani Shrestha83bd7c92019-06-11 21:21:5975 UI.ARIAUtils.bindLabelToControl(label, select);
Blink Reformat4c46d092018-04-07 15:32:3776
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)be2a1f62019-09-06 18:19:1587 return settingSelectElement;
Blink Reformat4c46d092018-04-07 15:32:3788
89 function settingChanged() {
90 const newValue = setting.get();
91 for (let i = 0; i < options.length; i++) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3492 if (options[i].value === newValue) {
Blink Reformat4c46d092018-04-07 15:32:3793 select.selectedIndex = i;
Tim van der Lippe1d6e57a2019-09-30 11:55:3494 }
Blink Reformat4c46d092018-04-07 15:32:3795 }
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 Lippe0830b3d2019-10-03 13:20:07108export const bindCheckbox = function(input, setting) {
Blink Reformat4c46d092018-04-07 15:32:37109 function settingChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34110 if (input.checked !== setting.get()) {
Blink Reformat4c46d092018-04-07 15:32:37111 input.checked = setting.get();
Tim van der Lippe1d6e57a2019-09-30 11:55:34112 }
Blink Reformat4c46d092018-04-07 15:32:37113 }
114 setting.addChangeListener(settingChanged);
115 settingChanged();
116
117 function inputChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34118 if (setting.get() !== input.checked) {
Blink Reformat4c46d092018-04-07 15:32:37119 setting.set(input.checked);
Tim van der Lippe1d6e57a2019-09-30 11:55:34120 }
Blink Reformat4c46d092018-04-07 15:32:37121 }
122 input.addEventListener('change', inputChanged, false);
123};
124
125/**
126 * @param {string} name
127 * @param {!Element} element
128 * @return {!Element}
129 */
Tim van der Lippe0830b3d2019-10-03 13:20:07130export const createCustomSetting = function(name, element) {
Blink Reformat4c46d092018-04-07 15:32:37131 const p = createElement('p');
132 const fieldsetElement = p.createChild('fieldset');
Chandani Shrestha83bd7c92019-06-11 21:21:59133 const label = fieldsetElement.createChild('label');
134 label.textContent = name;
135 UI.ARIAUtils.bindLabelToControl(label, element);
Blink Reformat4c46d092018-04-07 15:32:37136 fieldsetElement.appendChild(element);
137 return p;
138};
139
140/**
141 * @param {!Common.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:15142 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:37143 * @return {?Element}
144 */
Tim van der Lippe0830b3d2019-10-03 13:20:07145export const createControlForSetting = function(setting, subtitle) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34146 if (!setting.extension()) {
Blink Reformat4c46d092018-04-07 15:32:37147 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34148 }
Blink Reformat4c46d092018-04-07 15:32:37149 const descriptor = setting.extension().descriptor();
150 const uiTitle = Common.UIString(setting.title() || '');
151 switch (descriptor['settingType']) {
152 case 'boolean':
Tim van der Lippe0830b3d2019-10-03 13:20:07153 return createSettingCheckbox(uiTitle, setting);
Blink Reformat4c46d092018-04-07 15:32:37154 case 'enum':
Tim van der Lippe1d6e57a2019-09-30 11:55:34155 if (Array.isArray(descriptor['options'])) {
Tim van der Lippe0830b3d2019-10-03 13:20:07156 return createSettingSelect(uiTitle, descriptor['options'], setting, subtitle);
Tim van der Lippe1d6e57a2019-09-30 11:55:34157 }
Blink Reformat4c46d092018-04-07 15:32:37158 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 Lippe0830b3d2019-10-03 13:20:07169export class SettingUI {
Blink Reformat4c46d092018-04-07 15:32:37170 /**
171 * @return {?Element}
172 */
173 settingElement() {}
Tim van der Lippe0830b3d2019-10-03 13:20:07174}