Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / ui / SettingsUI.js
1 /*
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  */
30
31 WebInspector.SettingsUI = {}
32
33 /**
34  * @param {string} name
35  * @param {!WebInspector.Setting} setting
36  * @param {boolean=} omitParagraphElement
37  * @param {!Element=} inputElement
38  * @param {string=} tooltip
39  * @return {!Element}
40  */
41 WebInspector.SettingsUI.createSettingCheckbox = function(name, setting, omitParagraphElement, inputElement, tooltip)
42 {
43     var input = inputElement || document.createElement("input");
44     input.type = "checkbox";
45     input.name = name;
46     WebInspector.SettingsUI.bindCheckbox(input, setting);
47
48     var label = document.createElement("label");
49     label.appendChild(input);
50     label.createTextChild(name);
51     if (tooltip)
52         label.title = tooltip;
53
54     if (omitParagraphElement)
55         return label;
56
57     var p = document.createElement("p");
58     p.appendChild(label);
59     return p;
60 }
61
62 /**
63  * @param {!Element} input
64  * @param {!WebInspector.Setting} setting
65  */
66 WebInspector.SettingsUI.bindCheckbox = function(input, setting)
67 {
68     function settingChanged()
69     {
70         if (input.checked !== setting.get())
71             input.checked = setting.get();
72     }
73     setting.addChangeListener(settingChanged);
74     settingChanged();
75
76     function inputChanged()
77     {
78         if (setting.get() !== input.checked)
79             setting.set(input.checked);
80     }
81     input.addEventListener("change", inputChanged, false);
82 }
83
84 /**
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  */
92 WebInspector.SettingsUI.createSettingInputField = function(label, setting, numeric, maxLength, width, validatorCallback)
93 {
94     var p = document.createElement("p");
95     var labelElement = p.createChild("label");
96     labelElement.textContent = label;
97     var inputElement = p.createChild("input");
98     inputElement.value = setting.get();
99     inputElement.type = "text";
100     if (numeric)
101         inputElement.className = "numeric";
102     if (maxLength)
103         inputElement.maxLength = maxLength;
104     if (width)
105         inputElement.style.width = width;
106
107     var errorMessageLabel;
108     if (validatorCallback) {
109         errorMessageLabel = p.createChild("div");
110         errorMessageLabel.classList.add("field-error-message");
111         inputElement.oninput = onInput;
112         onInput();
113     }
114
115     function onInput()
116     {
117         var error = validatorCallback(inputElement.value);
118         if (!error)
119             error = "";
120         errorMessageLabel.textContent = error;
121     }
122
123     function onBlur()
124     {
125         setting.set(numeric ? Number(inputElement.value) : inputElement.value);
126     }
127     inputElement.addEventListener("blur", onBlur, false);
128
129     return p;
130 }
131
132 WebInspector.SettingsUI.createCustomSetting = function(name, element)
133 {
134     var p = document.createElement("p");
135     var fieldsetElement = document.createElement("fieldset");
136     fieldsetElement.createChild("label").textContent = name;
137     fieldsetElement.appendChild(element);
138     p.appendChild(fieldsetElement);
139     return p;
140 }
141
142 /**
143  * @param {!WebInspector.Setting} setting
144  * @return {!Element}
145  */
146 WebInspector.SettingsUI.createSettingFieldset = function(setting)
147 {
148     var fieldset = document.createElement("fieldset");
149     fieldset.disabled = !setting.get();
150     setting.addChangeListener(settingChanged);
151     return fieldset;
152
153     function settingChanged()
154     {
155         fieldset.disabled = !setting.get();
156     }
157 }
158
159 /**
160  * @param {string} text
161  * @return {?string}
162  */
163 WebInspector.SettingsUI.regexValidator = function(text)
164 {
165     var regex;
166     try {
167         regex = new RegExp(text);
168     } catch (e) {
169     }
170     return regex ? null : WebInspector.UIString("Invalid pattern");
171 }
172
173 /**
174  * @constructor
175  */
176 WebInspector.UISettingDelegate = function()
177 {
178 }
179
180 WebInspector.UISettingDelegate.prototype = {
181     /**
182      * @return {?Element}
183      */
184     settingElement: function()
185     {
186         return null;
187     }
188 }