OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('editSearchEngineDialog', function() { | 5 cr.define('editSearchEngineDialog', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * Flag indicating if we are in the process of validating input. While | |
10 * validating, the validity of the inputs is indeterminate. | |
11 * @type {boolean} | |
12 * @private | |
13 */ | |
14 var isValidating_ = false; | |
15 | |
16 /** | |
17 * Accessor for an entry field in the search engine dialog. | |
18 * @param {string} baseName Name of the field, which serves as a base name | |
19 * for the text input field and icon. | |
20 * @constructor | |
21 */ | |
22 function SearchEngineDialogEntryField(baseName) { | |
23 this.name_ = baseName; | |
24 this.text_ = $(baseName + '-text'); | |
25 this.icon_ = $(baseName + '-icon'); | |
26 this.text_.oninput = validate; | |
27 return this; | |
28 } | |
29 | |
30 SearchEngineDialogEntryField.prototype = { | |
31 | |
James Hawkins
2011/12/01 17:07:34
Remove blank line.
kevers
2011/12/01 17:57:27
Done.
| |
32 /* | |
33 * Getter for the name of the field. | |
34 * @type {string} Descriptive name of the field. | |
35 */ | |
36 get name() { | |
37 return this.name_; | |
38 }, | |
39 | |
40 /* | |
41 * Getter for the content of the input field. | |
42 * @type {string} Text content in the input field. | |
43 */ | |
44 get value() { | |
45 return this.text_.value; | |
46 }, | |
47 | |
48 /** | |
49 * Setter for the content of the input field. The validity of the input is | |
50 * not automatically revalidated. | |
51 * @type {string} New content for the input field. | |
52 */ | |
53 set value(text) { | |
54 this.text_.value = text; | |
55 // Validity is in an indeterminate state until validate is called. Clear | |
56 // the class name associated with the icon. | |
57 this.icon_.classList = ''; | |
James Hawkins
2011/12/01 17:07:34
s/classList/className/
kevers
2011/12/01 17:57:27
Done.
| |
58 }, | |
59 | |
60 /** | |
61 * Getter for the validity of an input field. | |
62 * @type {boolean} True if the text input is valid, otherwise false. | |
63 */ | |
64 get valid() { | |
65 return this.icon_.className == 'valid'; | |
66 }, | |
67 | |
68 /** | |
69 * Setter for the input field validily. | |
70 * @type {boolean} True if the input field is valid, false for invalid. | |
71 */ | |
72 set valid(state) { | |
73 this.icon_.className = state ? 'valid' : 'invalid'; | |
74 }, | |
75 | |
76 /** | |
77 * Creates a text representation of the class containing the name, | |
78 * text field contents and validity. | |
79 * @return {string} Text representation. | |
80 */ | |
81 toString: function() { | |
82 return this.name_ + ': \'' + this.text_.value + '\' (' + | |
83 this.icon_.className + ')'; | |
84 } | |
85 }; | |
86 | |
87 /** | |
88 * Accessors for entry fields in the search engine dialog. Initialized after | |
89 * content is loaded. | |
90 * @type{Object.<string,SearchEngineDialogEntryField>} | |
91 */ | |
92 var inputFields = {}; | |
93 | |
94 /** | |
9 * Disables the controls while the dialog is busy. | 95 * Disables the controls while the dialog is busy. |
10 */ | 96 */ |
11 function disableControls() { | 97 function disableControls() { |
12 $('cancel').disabled = true; | 98 $('cancel').disabled = true; |
13 $('save').disabled = true; | 99 $('save').disabled = true; |
14 } | 100 } |
15 | 101 |
16 /** | 102 /** |
17 * Close the dialog and pass a result value to the dialog close handler. | 103 * Close the dialog and pass a result value to the dialog close handler. |
18 * @param {{description: string, details: string, url: string}=} opt_result | 104 * @param {{description: string, details: string, url: string}=} opt_result |
19 * The value to pass to the dialog close handler. | 105 * The value to pass to the dialog close handler. |
20 */ | 106 */ |
21 function closeWithResult(opt_result) { | 107 function closeWithResult(opt_result) { |
22 disableControls(); | 108 disableControls(); |
23 var json = JSON.stringify(opt_result ? [opt_result] : []); | 109 var json = JSON.stringify(opt_result ? [opt_result] : []); |
24 chrome.send('DialogClose', [json]); | 110 chrome.send('DialogClose', [json]); |
25 } | 111 } |
26 | 112 |
27 /** | 113 /** |
28 * Sets the text of the dialog's editable text boxes. | 114 * Sets the text of the dialog's editable text boxes. |
29 * @param {{description: string, details: string, url: string}} details Values | 115 * @param {{description: string, details: string, url: string}} details Values |
30 * for corresponding text fields. | 116 * for corresponding text fields. |
31 */ | 117 */ |
32 function setDetails(details) { | 118 function setDetails(details) { |
33 $('description-text').value = details.description; | 119 inputFields.description.value = details.description; |
34 $('keyword-text').value = details.keyword; | 120 inputFields.keyword.value = details.keyword; |
35 $('url-text').value = details.url; | 121 inputFields.url.value = details.url; |
36 validate(); | 122 validate(); |
37 } | 123 } |
38 | 124 |
39 /** | 125 /** |
40 * Updates the validity icon element by changing its style. | |
41 * @param {Object} element The element to change. | |
42 * @param {boolean} valid True if the data is valid. | |
43 */ | |
44 function setValidImage(element, valid) { | |
45 element.className = valid ? 'valid' : 'invalid'; | |
46 } | |
47 | |
48 /** | |
49 * Sends all strings to Chrome for validation. Chrome is expected to respond | 126 * Sends all strings to Chrome for validation. Chrome is expected to respond |
50 * by calling setValidation. | 127 * by calling setValidation. |
51 */ | 128 */ |
52 function validate() { | 129 function validate() { |
53 chrome.send('requestValidation', [$('description-text').value, | 130 isValidating_ = true; |
54 $('keyword-text').value, $('url-text').value]); | 131 chrome.send('requestValidation', [inputFields.description.value, |
132 inputFields.keyword.value, inputFields.url.value]); | |
55 } | 133 } |
56 | 134 |
57 /** | 135 /** |
58 * Sets dialog state given the results of the validation of input by Chrome. | 136 * Sets dialog state given the results of the validation of input by Chrome. |
59 * @param {{description: boolean, details: boolean, url: boolean, ok:boolean}} | 137 * @param {{description: boolean, |
60 * details A dictionary of booleans indicating the validation results of | 138 keyword: boolean, |
61 * various parts of the UI. |description|, |details| and |url| indicate | 139 url: boolean, |
62 * the validity of the respective text fields, and |ok| indicates whether | 140 ok: boolean}} details |
141 * A dictionary of booleans indicating the validation results of various | |
142 * parts of the UI. |description|, |keyword| and |url| indicate the | |
143 * validity of the respective text fields, and |ok| indicates whether | |
63 * the OK/Save button can be pressed. | 144 * the OK/Save button can be pressed. |
64 */ | 145 */ |
65 function setValidation(details) { | 146 function setValidation(details) { |
66 setValidImage($('description-icon'), details.description); | 147 inputFields.description.valid = details.description; |
67 setValidImage($('keyword-icon'), details.keyword); | 148 inputFields.keyword.valid = details.keyword; |
68 setValidImage($('url-icon'), details.url); | 149 inputFields.url.valid = details.url; |
69 $('save').disabled = !details.ok; | 150 $('save').disabled = !details.ok; |
151 isValidating_ = false; | |
70 } | 152 } |
71 | 153 |
72 /** | 154 /** |
73 * Reverses the order of child nodes. | 155 * Reverses the order of child nodes. |
74 * @param {HTMLElement} parent The parent node whose children are to be | 156 * @param {HTMLElement} parent The parent node whose children are to be |
75 * reversed. | 157 * reversed. |
76 */ | 158 */ |
77 function reverseChildren(parent) { | 159 function reverseChildren(parent) { |
78 var childNodes = parent.childNodes; | 160 var childNodes = parent.childNodes; |
79 for (var i = childNodes.length - 1; i >= 0; i--) | 161 for (var i = childNodes.length - 1; i >= 0; i--) |
80 parent.appendChild(childNodes[i]); | 162 parent.appendChild(childNodes[i]); |
81 }; | 163 } |
82 | 164 |
83 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); | 165 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
84 | 166 |
85 /** | 167 /** |
86 * Inserts translated strings on loading. | 168 * Inserts translated strings on loading. |
87 */ | 169 */ |
88 function initialize() { | 170 function initialize() { |
89 i18nTemplate.process(document, templateData); | 171 i18nTemplate.process(document, templateData); |
90 | 172 |
173 inputFields.description = new SearchEngineDialogEntryField('description'); | |
174 inputFields.keyword = new SearchEngineDialogEntryField('keyword'); | |
175 inputFields.url = new SearchEngineDialogEntryField('url'); | |
176 | |
91 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : | 177 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : |
92 templateData.titleEdit; | 178 templateData.titleEdit; |
93 | 179 |
94 $('cancel').onclick = function() { | 180 $('cancel').onclick = function() { |
95 closeWithResult(); | 181 closeWithResult(); |
96 } | 182 }; |
97 | 183 |
98 $('save').onclick = function() { | 184 $('save').onclick = function() { |
99 closeWithResult({description: $('description-text').value, | 185 closeWithResult({description: inputFields.description.value, |
100 keyword: $('keyword-text').value, | 186 keyword: inputFields.keyword.value, |
101 url: $('url-text').value}); | 187 url: inputFields.url.value}); |
102 } | 188 }; |
103 | |
104 $('description-text').oninput = validate; | |
105 $('keyword-text').oninput = validate; | |
106 $('url-text').oninput = validate; | |
107 | 189 |
108 setValidation({description: false, keyword: false, url: false}); | 190 setValidation({description: false, keyword: false, url: false}); |
109 if (cr.isViews) | 191 if (cr.isViews) |
110 forEach(document.querySelectorAll('.button-strip'), reverseChildren); | 192 forEach(document.querySelectorAll('.button-strip'), reverseChildren); |
111 chrome.send('requestDetails') | 193 // Mark that we are in the process of validating, since the 'send' call |
194 // is asynchronous. Until the next call to 'setValidation' complete, the | |
195 // validity of the inputs is in an indeterminate state. | |
196 isValidating_ = true; | |
197 | |
198 // TODO(kevers): Should be a cleaner way to implement without requiring | |
199 // multiple callback to C++. The call to |requestDetails| | |
James Hawkins
2011/12/01 17:07:34
Remove this comment indentation.
kevers
2011/12/01 17:57:27
Done.
| |
200 // fetches the content to insert into the input fields | |
201 // without inidicating if the inputs are valid. A separate | |
202 // callback is then required to determine if the inputs are | |
203 // OK. In fact, it should be possible to pass in the details | |
204 // when the dialog is created rather than using a callback. | |
205 chrome.send('requestDetails'); | |
206 } | |
207 | |
208 /** | |
209 * Indicates if we are in the process of validating input. | |
210 * @return {boolean} True if validation is in progress. | |
211 */ | |
212 function isValidating() { | |
213 return isValidating_; | |
214 } | |
215 | |
216 /** | |
217 * Retrieves the save button element. | |
218 * @return {Element} | |
219 */ | |
220 function getSave() { | |
221 return $('save'); | |
112 } | 222 } |
113 | 223 |
114 document.addEventListener('DOMContentLoaded', initialize); | 224 document.addEventListener('DOMContentLoaded', initialize); |
115 | 225 |
116 return { | 226 return { |
227 inputFields: inputFields, | |
228 isValidating: isValidating, | |
229 getSave: getSave, | |
117 setDetails: setDetails, | 230 setDetails: setDetails, |
118 setValidation: setValidation, | 231 setValidation: setValidation, |
232 validate: validate | |
119 }; | 233 }; |
120 }); | 234 }); |
121 | 235 |
122 | 236 |
OLD | NEW |