blob: 8fc53a4bfac073723f80cd000de709fc3ca01404 [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 */
30UI.SettingsUI = {};
31
32/**
33 * @param {string} name
34 * @param {!Common.Setting} setting
35 * @param {boolean=} omitParagraphElement
36 * @param {string=} tooltip
37 * @return {!Element}
38 */
39UI.SettingsUI.createSettingCheckbox = function(name, setting, omitParagraphElement, tooltip) {
40 const label = UI.CheckboxLabel.create(name);
Tim van der Lippe1d6e57a2019-09-30 11:55:3441 if (tooltip) {
Blink Reformat4c46d092018-04-07 15:32:3742 label.title = tooltip;
Tim van der Lippe1d6e57a2019-09-30 11:55:3443 }
Blink Reformat4c46d092018-04-07 15:32:3744
45 const input = label.checkboxElement;
46 input.name = name;
47 UI.SettingsUI.bindCheckbox(input, setting);
48
Tim van der Lippe1d6e57a2019-09-30 11:55:3449 if (omitParagraphElement) {
Blink Reformat4c46d092018-04-07 15:32:3750 return label;
Tim van der Lippe1d6e57a2019-09-30 11:55:3451 }
Blink Reformat4c46d092018-04-07 15:32:3752
53 const p = createElement('p');
54 p.appendChild(label);
55 return p;
56};
57
58/**
59 * @param {string} name
60 * @param {!Array<!{text: string, value: *, raw: (boolean|undefined)}>} options
61 * @param {!Common.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:1562 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:3763 * @return {!Element}
64 */
Michael Liao (WPT)be2a1f62019-09-06 18:19:1565UI.SettingsUI.createSettingSelect = function(name, options, setting, subtitle) {
66 const settingSelectElement = createElement('p');
67 const label = settingSelectElement.createChild('label');
68 const select = settingSelectElement.createChild('select', 'chrome-select');
Chandani Shrestha83bd7c92019-06-11 21:21:5969 label.textContent = name;
Michael Liao (WPT)be2a1f62019-09-06 18:19:1570 if (subtitle) {
71 settingSelectElement.classList.add('chrome-select-label');
72 label.createChild('p').textContent = subtitle;
73 }
Chandani Shrestha83bd7c92019-06-11 21:21:5974 UI.ARIAUtils.bindLabelToControl(label, select);
Blink Reformat4c46d092018-04-07 15:32:3775
76 for (let i = 0; i < options.length; ++i) {
77 // The "raw" flag indicates text is non-i18n-izable.
78 const option = options[i];
79 const optionName = option.raw ? option.text : Common.UIString(option.text);
80 select.add(new Option(optionName, option.value));
81 }
82
83 setting.addChangeListener(settingChanged);
84 settingChanged();
85 select.addEventListener('change', selectChanged, false);
Michael Liao (WPT)be2a1f62019-09-06 18:19:1586 return settingSelectElement;
Blink Reformat4c46d092018-04-07 15:32:3787
88 function settingChanged() {
89 const newValue = setting.get();
90 for (let i = 0; i < options.length; i++) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3491 if (options[i].value === newValue) {
Blink Reformat4c46d092018-04-07 15:32:3792 select.selectedIndex = i;
Tim van der Lippe1d6e57a2019-09-30 11:55:3493 }
Blink Reformat4c46d092018-04-07 15:32:3794 }
95 }
96
97 function selectChanged() {
98 // Don't use event.target.value to avoid conversion of the value to string.
99 setting.set(options[select.selectedIndex].value);
100 }
101};
102
103/**
104 * @param {!Element} input
105 * @param {!Common.Setting} setting
106 */
107UI.SettingsUI.bindCheckbox = function(input, setting) {
108 function settingChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34109 if (input.checked !== setting.get()) {
Blink Reformat4c46d092018-04-07 15:32:37110 input.checked = setting.get();
Tim van der Lippe1d6e57a2019-09-30 11:55:34111 }
Blink Reformat4c46d092018-04-07 15:32:37112 }
113 setting.addChangeListener(settingChanged);
114 settingChanged();
115
116 function inputChanged() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34117 if (setting.get() !== input.checked) {
Blink Reformat4c46d092018-04-07 15:32:37118 setting.set(input.checked);
Tim van der Lippe1d6e57a2019-09-30 11:55:34119 }
Blink Reformat4c46d092018-04-07 15:32:37120 }
121 input.addEventListener('change', inputChanged, false);
122};
123
124/**
125 * @param {string} name
126 * @param {!Element} element
127 * @return {!Element}
128 */
129UI.SettingsUI.createCustomSetting = function(name, element) {
130 const p = createElement('p');
131 const fieldsetElement = p.createChild('fieldset');
Chandani Shrestha83bd7c92019-06-11 21:21:59132 const label = fieldsetElement.createChild('label');
133 label.textContent = name;
134 UI.ARIAUtils.bindLabelToControl(label, element);
Blink Reformat4c46d092018-04-07 15:32:37135 fieldsetElement.appendChild(element);
136 return p;
137};
138
139/**
140 * @param {!Common.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:15141 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:37142 * @return {?Element}
143 */
Michael Liao (WPT)be2a1f62019-09-06 18:19:15144UI.SettingsUI.createControlForSetting = function(setting, subtitle) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34145 if (!setting.extension()) {
Blink Reformat4c46d092018-04-07 15:32:37146 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34147 }
Blink Reformat4c46d092018-04-07 15:32:37148 const descriptor = setting.extension().descriptor();
149 const uiTitle = Common.UIString(setting.title() || '');
150 switch (descriptor['settingType']) {
151 case 'boolean':
152 return UI.SettingsUI.createSettingCheckbox(uiTitle, setting);
153 case 'enum':
Tim van der Lippe1d6e57a2019-09-30 11:55:34154 if (Array.isArray(descriptor['options'])) {
Michael Liao (WPT)be2a1f62019-09-06 18:19:15155 return UI.SettingsUI.createSettingSelect(uiTitle, descriptor['options'], setting, subtitle);
Tim van der Lippe1d6e57a2019-09-30 11:55:34156 }
Blink Reformat4c46d092018-04-07 15:32:37157 console.error('Enum setting defined without options');
158 return null;
159 default:
160 console.error('Invalid setting type: ' + descriptor['settingType']);
161 return null;
162 }
163};
164
165/**
166 * @interface
167 */
168UI.SettingUI = function() {};
169
170UI.SettingUI.prototype = {
171 /**
172 * @return {?Element}
173 */
174 settingElement() {}
175};