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 {string=} tooltip
40 WebInspector
.SettingsUI
.createSettingCheckbox = function(name
, setting
, omitParagraphElement
, tooltip
)
42 var label
= createCheckboxLabel(name
);
44 label
.title
= tooltip
;
46 var input
= label
.checkboxElement
;
48 WebInspector
.SettingsUI
.bindCheckbox(input
, setting
);
50 if (omitParagraphElement
)
53 var p
= createElement("p");
59 * @param {!Element} input
60 * @param {!WebInspector.Setting} setting
62 WebInspector
.SettingsUI
.bindCheckbox = function(input
, setting
)
64 function settingChanged()
66 if (input
.checked
!== setting
.get())
67 input
.checked
= setting
.get();
69 setting
.addChangeListener(settingChanged
);
72 function inputChanged()
74 if (setting
.get() !== input
.checked
)
75 setting
.set(input
.checked
);
77 input
.addEventListener("change", inputChanged
, false);
81 * @param {string} label
82 * @param {!WebInspector.Setting} setting
83 * @param {boolean} numeric
84 * @param {number=} maxLength
85 * @param {string=} width
86 * @param {function(string):?string=} validatorCallback
87 * @param {boolean=} instant
88 * @param {boolean=} clearForZero
89 * @param {string=} placeholder
92 WebInspector
.SettingsUI
.createSettingInputField = function(label
, setting
, numeric
, maxLength
, width
, validatorCallback
, instant
, clearForZero
, placeholder
)
94 var p
= createElement("p");
95 var labelElement
= p
.createChild("label");
96 labelElement
.textContent
= label
;
97 var inputElement
= p
.createChild("input");
98 inputElement
.type
= "text";
100 inputElement
.className
= "numeric";
102 inputElement
.maxLength
= maxLength
;
104 inputElement
.style
.width
= width
;
105 inputElement
.placeholder
= placeholder
|| "";
107 if (validatorCallback
|| instant
) {
108 inputElement
.addEventListener("change", onInput
, false);
109 inputElement
.addEventListener("input", onInput
, false);
111 inputElement
.addEventListener("keydown", onKeyDown
, false);
113 var errorMessageLabel
;
114 if (validatorCallback
)
115 errorMessageLabel
= p
.createChild("div", "field-error-message");
119 if (validatorCallback
)
125 function onKeyDown(event
)
127 if (isEnterKey(event
))
129 incrementForArrows(event
);
132 function incrementForArrows(event
)
137 var increment
= event
.keyIdentifier
=== "Up" ? 1 : event
.keyIdentifier
=== "Down" ? -1 : 0;
143 var value
= inputElement
.value
;
144 if (validatorCallback
&& validatorCallback(value
))
146 value
= Number(value
);
147 if (clearForZero
&& !value
)
150 if (clearForZero
&& !value
)
152 value
= String(value
);
153 if (validatorCallback
&& validatorCallback(value
))
156 inputElement
.value
= value
;
158 event
.preventDefault();
163 var error
= validatorCallback(inputElement
.value
);
166 inputElement
.classList
.toggle("error-input", !!error
);
167 errorMessageLabel
.textContent
= error
;
171 inputElement
.addEventListener("blur", apply
, false);
175 if (validatorCallback
&& validatorCallback(inputElement
.value
))
177 setting
.removeChangeListener(onSettingChange
);
178 setting
.set(numeric
? Number(inputElement
.value
) : inputElement
.value
);
179 setting
.addChangeListener(onSettingChange
);
182 setting
.addChangeListener(onSettingChange
);
184 function onSettingChange()
186 var value
= setting
.get();
187 if (clearForZero
&& !value
)
189 inputElement
.value
= value
;
193 if (validatorCallback
)
200 * @param {string} name
201 * @param {!Element} element
204 WebInspector
.SettingsUI
.createCustomSetting = function(name
, element
)
206 var p
= createElement("p");
207 var fieldsetElement
= p
.createChild("fieldset");
208 fieldsetElement
.createChild("label").textContent
= name
;
209 fieldsetElement
.appendChild(element
);
214 * @param {!WebInspector.Setting} setting
217 WebInspector
.SettingsUI
.createSettingFieldset = function(setting
)
219 var fieldset
= createElement("fieldset");
220 fieldset
.disabled
= !setting
.get();
221 setting
.addChangeListener(settingChanged
);
224 function settingChanged()
226 fieldset
.disabled
= !setting
.get();
231 * @param {string} text
234 WebInspector
.SettingsUI
.regexValidator = function(text
)
238 regex
= new RegExp(text
);
241 return regex
? null : WebInspector
.UIString("Invalid pattern");
245 * Creates an input element under the parentElement with the given id and defaultText.
246 * @param {!Element} parentElement
248 * @param {string} defaultText
249 * @param {function(*)} eventListener
250 * @param {boolean=} numeric
251 * @param {string=} size
252 * @return {!Element} element
254 WebInspector
.SettingsUI
.createInput = function(parentElement
, id
, defaultText
, eventListener
, numeric
, size
)
256 var element
= parentElement
.createChild("input");
258 element
.type
= "text";
259 element
.maxLength
= 12;
260 element
.style
.width
= size
|| "80px";
261 element
.value
= defaultText
;
262 element
.align
= "right";
264 element
.className
= "numeric";
265 element
.addEventListener("input", eventListener
, false);
266 element
.addEventListener("keydown", keyDownListener
, false);
267 function keyDownListener(event
)
269 if (isEnterKey(event
))
270 eventListener(event
);
278 WebInspector
.SettingUI = function()
282 WebInspector
.SettingUI
.prototype = {
286 settingElement: function() { }