blob: 5676b8dacd24d1bb4194e5eea45b925cdccf7e9f [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);
41 if (tooltip)
42 label.title = tooltip;
43
44 const input = label.checkboxElement;
45 input.name = name;
46 UI.SettingsUI.bindCheckbox(input, setting);
47
48 if (omitParagraphElement)
49 return label;
50
51 const p = createElement('p');
52 p.appendChild(label);
53 return p;
54};
55
56/**
57 * @param {string} name
58 * @param {!Array<!{text: string, value: *, raw: (boolean|undefined)}>} options
59 * @param {!Common.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:1560 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:3761 * @return {!Element}
62 */
Michael Liao (WPT)be2a1f62019-09-06 18:19:1563UI.SettingsUI.createSettingSelect = function(name, options, setting, subtitle) {
64 const settingSelectElement = createElement('p');
65 const label = settingSelectElement.createChild('label');
66 const select = settingSelectElement.createChild('select', 'chrome-select');
Chandani Shrestha83bd7c92019-06-11 21:21:5967 label.textContent = name;
Michael Liao (WPT)be2a1f62019-09-06 18:19:1568 if (subtitle) {
69 settingSelectElement.classList.add('chrome-select-label');
70 label.createChild('p').textContent = subtitle;
71 }
Chandani Shrestha83bd7c92019-06-11 21:21:5972 UI.ARIAUtils.bindLabelToControl(label, select);
Blink Reformat4c46d092018-04-07 15:32:3773
74 for (let i = 0; i < options.length; ++i) {
75 // The "raw" flag indicates text is non-i18n-izable.
76 const option = options[i];
77 const optionName = option.raw ? option.text : Common.UIString(option.text);
78 select.add(new Option(optionName, option.value));
79 }
80
81 setting.addChangeListener(settingChanged);
82 settingChanged();
83 select.addEventListener('change', selectChanged, false);
Michael Liao (WPT)be2a1f62019-09-06 18:19:1584 return settingSelectElement;
Blink Reformat4c46d092018-04-07 15:32:3785
86 function settingChanged() {
87 const newValue = setting.get();
88 for (let i = 0; i < options.length; i++) {
89 if (options[i].value === newValue)
90 select.selectedIndex = i;
91 }
92 }
93
94 function selectChanged() {
95 // Don't use event.target.value to avoid conversion of the value to string.
96 setting.set(options[select.selectedIndex].value);
97 }
98};
99
100/**
101 * @param {!Element} input
102 * @param {!Common.Setting} setting
103 */
104UI.SettingsUI.bindCheckbox = function(input, setting) {
105 function settingChanged() {
106 if (input.checked !== setting.get())
107 input.checked = setting.get();
108 }
109 setting.addChangeListener(settingChanged);
110 settingChanged();
111
112 function inputChanged() {
113 if (setting.get() !== input.checked)
114 setting.set(input.checked);
115 }
116 input.addEventListener('change', inputChanged, false);
117};
118
119/**
120 * @param {string} name
121 * @param {!Element} element
122 * @return {!Element}
123 */
124UI.SettingsUI.createCustomSetting = function(name, element) {
125 const p = createElement('p');
126 const fieldsetElement = p.createChild('fieldset');
Chandani Shrestha83bd7c92019-06-11 21:21:59127 const label = fieldsetElement.createChild('label');
128 label.textContent = name;
129 UI.ARIAUtils.bindLabelToControl(label, element);
Blink Reformat4c46d092018-04-07 15:32:37130 fieldsetElement.appendChild(element);
131 return p;
132};
133
134/**
135 * @param {!Common.Setting} setting
Michael Liao (WPT)be2a1f62019-09-06 18:19:15136 * @param {string=} subtitle
Blink Reformat4c46d092018-04-07 15:32:37137 * @return {?Element}
138 */
Michael Liao (WPT)be2a1f62019-09-06 18:19:15139UI.SettingsUI.createControlForSetting = function(setting, subtitle) {
Blink Reformat4c46d092018-04-07 15:32:37140 if (!setting.extension())
141 return null;
142 const descriptor = setting.extension().descriptor();
143 const uiTitle = Common.UIString(setting.title() || '');
144 switch (descriptor['settingType']) {
145 case 'boolean':
146 return UI.SettingsUI.createSettingCheckbox(uiTitle, setting);
147 case 'enum':
148 if (Array.isArray(descriptor['options']))
Michael Liao (WPT)be2a1f62019-09-06 18:19:15149 return UI.SettingsUI.createSettingSelect(uiTitle, descriptor['options'], setting, subtitle);
Blink Reformat4c46d092018-04-07 15:32:37150 console.error('Enum setting defined without options');
151 return null;
152 default:
153 console.error('Invalid setting type: ' + descriptor['settingType']);
154 return null;
155 }
156};
157
158/**
159 * @interface
160 */
161UI.SettingUI = function() {};
162
163UI.SettingUI.prototype = {
164 /**
165 * @return {?Element}
166 */
167 settingElement() {}
168};