2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 WebInspector.SettingsUI = {}
34 * @param {string} name
35 * @param {!WebInspector.Setting} setting
36 * @param {boolean=} omitParagraphElement
37 * @param {!Element=} inputElement
38 * @param {string=} tooltip
41 WebInspector.SettingsUI.createSettingCheckbox = function(name, setting, omitParagraphElement, inputElement, tooltip)
43 var input = inputElement || document.createElement("input");
44 input.type = "checkbox";
46 WebInspector.SettingsUI.bindCheckbox(input, setting);
48 var label = document.createElement("label");
49 label.appendChild(input);
50 label.createTextChild(name);
52 label.title = tooltip;
54 if (omitParagraphElement)
57 var p = document.createElement("p");
63 * @param {!Element} input
64 * @param {!WebInspector.Setting} setting
66 WebInspector.SettingsUI.bindCheckbox = function(input, setting)
68 function settingChanged()
70 if (input.checked !== setting.get())
71 input.checked = setting.get();
73 setting.addChangeListener(settingChanged);
76 function inputChanged()
78 if (setting.get() !== input.checked)
79 setting.set(input.checked);
81 input.addEventListener("change", inputChanged, false);
85 * @param {string} label
86 * @param {!WebInspector.Setting} setting
87 * @param {boolean} numeric
88 * @param {number=} maxLength
89 * @param {string=} width
90 * @param {function(string):?string=} validatorCallback
91 * @param {boolean=} instant
92 * @param {boolean=} clearForZero
93 * @param {string=} placeholder
96 WebInspector.SettingsUI.createSettingInputField = function(label, setting, numeric, maxLength, width, validatorCallback, instant, clearForZero, placeholder)
98 var p = document.createElement("p");
99 var labelElement = p.createChild("label");
100 labelElement.textContent = label;
101 var inputElement = p.createChild("input");
102 inputElement.type = "text";
104 inputElement.className = "numeric";
106 inputElement.maxLength = maxLength;
108 inputElement.style.width = width;
109 inputElement.placeholder = placeholder || "";
111 if (validatorCallback || instant) {
112 inputElement.addEventListener("change", onInput, false);
113 inputElement.addEventListener("input", onInput, false);
115 inputElement.addEventListener("keydown", onKeyDown, false);
117 var errorMessageLabel;
118 if (validatorCallback) {
119 errorMessageLabel = p.createChild("div");
120 errorMessageLabel.classList.add("field-error-message");
126 if (validatorCallback)
132 function onKeyDown(event)
134 if (isEnterKey(event))
140 var error = validatorCallback(inputElement.value);
143 inputElement.classList.toggle("error-input", !!error);
144 errorMessageLabel.textContent = error;
148 inputElement.addEventListener("blur", apply, false);
152 if (validatorCallback && validatorCallback(inputElement.value))
154 setting.removeChangeListener(onSettingChange);
155 setting.set(numeric ? Number(inputElement.value) : inputElement.value);
156 setting.addChangeListener(onSettingChange);
159 setting.addChangeListener(onSettingChange);
161 function onSettingChange()
163 var value = setting.get();
164 if (clearForZero && !value)
166 inputElement.value = value;
174 * @param {string} name
175 * @param {!Element} element
178 WebInspector.SettingsUI.createCustomSetting = function(name, element)
180 var p = document.createElement("p");
181 var fieldsetElement = document.createElement("fieldset");
182 fieldsetElement.createChild("label").textContent = name;
183 fieldsetElement.appendChild(element);
184 p.appendChild(fieldsetElement);
189 * @param {!WebInspector.Setting} setting
192 WebInspector.SettingsUI.createSettingFieldset = function(setting)
194 var fieldset = document.createElement("fieldset");
195 fieldset.disabled = !setting.get();
196 setting.addChangeListener(settingChanged);
199 function settingChanged()
201 fieldset.disabled = !setting.get();
206 * @param {string} text
209 WebInspector.SettingsUI.regexValidator = function(text)
213 regex = new RegExp(text);
216 return regex ? null : WebInspector.UIString("Invalid pattern");
220 * Creates an input element under the parentElement with the given id and defaultText.
221 * @param {!Element} parentElement
223 * @param {string} defaultText
224 * @param {function(*)} eventListener
225 * @param {boolean=} numeric
226 * @param {string=} size
227 * @return {!Element} element
229 WebInspector.SettingsUI.createInput = function(parentElement, id, defaultText, eventListener, numeric, size)
231 var element = parentElement.createChild("input");
233 element.type = "text";
234 element.maxLength = 12;
235 element.style.width = size || "80px";
236 element.value = defaultText;
237 element.align = "right";
239 element.className = "numeric";
240 element.addEventListener("input", eventListener, false);
241 element.addEventListener("keydown", keyDownListener, false);
242 function keyDownListener(event)
244 if (isEnterKey(event))
245 eventListener(event);
253 WebInspector.UISettingDelegate = function()
257 WebInspector.UISettingDelegate.prototype = {
261 settingElement: function()