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 inidicating if we are in the process of validating input. While | |
flackr
2011/11/30 16:30:24
s/inidicating/indicating
kevers
2011/11/30 21:27:29
Done.
| |
10 * validating, the validity of the inputs is indeterminate. | |
11 * @type {boolean} | |
12 * @private | |
13 */ | |
14 var isValidating_ = false; | |
15 | |
16 /** | |
17 * Accessor for in entry field in the search engine dialog. | |
flackr
2011/11/30 16:30:24
s/for in/for an
kevers
2011/11/30 21:27:29
Done.
| |
18 * @param {string} baseName Name of the field, which servers as a base name | |
flackr
2011/11/30 16:30:24
s/servers/serves
kevers
2011/11/30 21:27:29
Done.
| |
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 | |
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; | |
flackr
2011/11/30 16:30:24
This could invalidate the field, or if not setting
kevers
2011/11/30 21:27:29
Done.
| |
55 }, | |
56 | |
57 /** | |
58 * Getter for the validity of an input field. | |
59 * @type {boolean} True if the text input is valid, otherwise false. | |
60 */ | |
61 get valid() { | |
62 return this.icon_.className == 'valid'; | |
63 }, | |
64 | |
65 /** | |
66 * Setter for the input field validily. | |
67 * @type {boolean} True if the input field is valid, false for invalid. | |
68 */ | |
69 set valid(state) { | |
70 this.icon_.className = state ? 'valid' : 'invalid'; | |
71 }, | |
72 | |
73 /** | |
74 * Creates a text representation of the class containing the name, | |
75 * text field contents and validity. | |
76 * @return {string} Text representation. | |
77 */ | |
78 toString: function() { | |
79 return this.name_ + ': \'' + this.text_.value + '\' (' + | |
80 this.icon_.className + ')'; | |
81 } | |
82 }; | |
83 | |
84 /** | |
85 * Accessors for entry fields in the search engine dialog. Initialized after | |
86 * content is loaded. | |
87 * @type{Object.<string,SearchEngineDialogEntryField>} | |
88 */ | |
89 var inputFields = {}; | |
90 | |
91 /** | |
9 * Disables the controls while the dialog is busy. | 92 * Disables the controls while the dialog is busy. |
10 */ | 93 */ |
11 function disableControls() { | 94 function disableControls() { |
12 $('cancel').disabled = true; | 95 $('cancel').disabled = true; |
13 $('save').disabled = true; | 96 $('save').disabled = true; |
14 } | 97 } |
15 | 98 |
16 /** | 99 /** |
17 * Close the dialog and pass a result value to the dialog close handler. | 100 * Close the dialog and pass a result value to the dialog close handler. |
18 * @param {{description: string, details: string, url: string}=} opt_result | 101 * @param {{description: string, details: string, url: string}=} opt_result |
19 * The value to pass to the dialog close handler. | 102 * The value to pass to the dialog close handler. |
20 */ | 103 */ |
21 function closeWithResult(opt_result) { | 104 function closeWithResult(opt_result) { |
22 disableControls(); | 105 disableControls(); |
23 var json = JSON.stringify(opt_result ? [opt_result] : []); | 106 var json = JSON.stringify(opt_result ? [opt_result] : []); |
24 chrome.send('DialogClose', [json]); | 107 chrome.send('DialogClose', [json]); |
25 } | 108 } |
26 | 109 |
27 /** | 110 /** |
28 * Sets the text of the dialog's editable text boxes. | 111 * Sets the text of the dialog's editable text boxes. |
29 * @param {{description: string, details: string, url: string}} details Values | 112 * @param {{description: string, details: string, url: string}} details Values |
30 * for corresponding text fields. | 113 * for corresponding text fields. |
31 */ | 114 */ |
32 function setDetails(details) { | 115 function setDetails(details) { |
flackr
2011/11/30 16:30:24
It seems really odd that we send details from the
kevers
2011/11/30 21:27:29
Added a TODO at the end of the initialize method.
| |
33 $('description-text').value = details.description; | 116 inputFields.description.value = details.description; |
34 $('keyword-text').value = details.keyword; | 117 inputFields.keyword.value = details.keyword; |
35 $('url-text').value = details.url; | 118 inputFields.url.value = details.url; |
36 validate(); | 119 validate(); |
37 } | 120 } |
38 | 121 |
39 /** | 122 /** |
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 | 123 * Sends all strings to Chrome for validation. Chrome is expected to respond |
50 * by calling setValidation. | 124 * by calling setValidation. |
51 */ | 125 */ |
52 function validate() { | 126 function validate() { |
53 chrome.send('requestValidation', [$('description-text').value, | 127 isValidating_ = true; |
54 $('keyword-text').value, $('url-text').value]); | 128 chrome.send('requestValidation', [inputFields.description.value, |
129 inputFields.keyword.value, inputFields.url.value]); | |
55 } | 130 } |
56 | 131 |
57 /** | 132 /** |
58 * Sets dialog state given the results of the validation of input by Chrome. | 133 * Sets dialog state given the results of the validation of input by Chrome. |
59 * @param {{description: boolean, details: boolean, url: boolean, ok:boolean}} | 134 * @param {{description: boolean, |
60 * details A dictionary of booleans indicating the validation results of | 135 keyword: boolean, |
61 * various parts of the UI. |description|, |details| and |url| indicate | 136 url: boolean, |
62 * the validity of the respective text fields, and |ok| indicates whether | 137 ok: boolean}} details |
138 * A dictionary of booleans indicating the validation results of various | |
139 * parts of the UI. |description|, |keyword| and |url| indicate the | |
140 * validity of the respective text fields, and |ok| indicates whether | |
63 * the OK/Save button can be pressed. | 141 * the OK/Save button can be pressed. |
64 */ | 142 */ |
65 function setValidation(details) { | 143 function setValidation(details) { |
66 setValidImage($('description-icon'), details.description); | 144 inputFields.description.valid = details.description; |
67 setValidImage($('keyword-icon'), details.keyword); | 145 inputFields.keyword.valid = details.keyword; |
68 setValidImage($('url-icon'), details.url); | 146 inputFields.url.valid = details.url; |
69 $('save').disabled = !details.ok; | 147 $('save').disabled = !details.ok; |
148 isValidating_ = false; | |
70 } | 149 } |
71 | 150 |
72 /** | 151 /** |
73 * Reverses the order of child nodes. | 152 * Reverses the order of child nodes. |
74 * @param {HTMLElement} parent The parent node whose children are to be | 153 * @param {HTMLElement} parent The parent node whose children are to be |
75 * reversed. | 154 * reversed. |
76 */ | 155 */ |
77 function reverseChildren(parent) { | 156 function reverseChildren(parent) { |
78 var childNodes = parent.childNodes; | 157 var childNodes = parent.childNodes; |
79 for (var i = childNodes.length - 1; i >= 0; i--) | 158 for (var i = childNodes.length - 1; i >= 0; i--) |
80 parent.appendChild(childNodes[i]); | 159 parent.appendChild(childNodes[i]); |
81 }; | 160 } |
82 | 161 |
83 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); | 162 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
84 | 163 |
85 /** | 164 /** |
86 * Inserts translated strings on loading. | 165 * Inserts translated strings on loading. |
87 */ | 166 */ |
88 function initialize() { | 167 function initialize() { |
89 i18nTemplate.process(document, templateData); | 168 i18nTemplate.process(document, templateData); |
90 | 169 |
170 inputFields.description = new SearchEngineDialogEntryField('description'); | |
171 inputFields.keyword = new SearchEngineDialogEntryField('keyword'); | |
172 inputFields.url = new SearchEngineDialogEntryField('url'); | |
173 | |
91 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : | 174 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : |
92 templateData.titleEdit; | 175 templateData.titleEdit; |
93 | 176 |
94 $('cancel').onclick = function() { | 177 $('cancel').onclick = function() { |
95 closeWithResult(); | 178 closeWithResult(); |
96 } | 179 }; |
97 | 180 |
98 $('save').onclick = function() { | 181 $('save').onclick = function() { |
99 closeWithResult({description: $('description-text').value, | 182 closeWithResult({description: inputFields.description.value, |
100 keyword: $('keyword-text').value, | 183 keyword: inputFields.keyword.value, |
101 url: $('url-text').value}); | 184 url: inputFields.url.value}); |
102 } | 185 }; |
103 | |
104 $('description-text').oninput = validate; | |
105 $('keyword-text').oninput = validate; | |
106 $('url-text').oninput = validate; | |
107 | 186 |
108 setValidation({description: false, keyword: false, url: false}); | 187 setValidation({description: false, keyword: false, url: false}); |
109 if (cr.isViews) | 188 if (cr.isViews) |
110 forEach(document.querySelectorAll('.button-strip'), reverseChildren); | 189 forEach(document.querySelectorAll('.button-strip'), reverseChildren); |
111 chrome.send('requestDetails') | 190 // Mark that we are in the process of validating, since the 'send' call |
191 // is asynchronous. Until the next call to 'setValidation' complete, the | |
192 // validity of the inputs is in an indeterminate state. | |
193 isValidating_ = true; | |
194 chrome.send('requestDetails'); | |
flackr
2011/11/30 16:30:24
We should pass all of these details in as dialog a
kevers
2011/11/30 21:27:29
Done.
| |
195 } | |
196 | |
197 /** | |
198 * Indicates if we are in the process of validating input. | |
199 * @return {boolean} True if validation is in progress. | |
200 */ | |
201 function isValidating() { | |
202 return isValidating_; | |
203 } | |
204 | |
205 /** | |
206 * Retrieves the save button element. | |
207 * @return {Element} | |
208 */ | |
209 function getSave() { | |
210 return $('save'); | |
112 } | 211 } |
113 | 212 |
114 document.addEventListener('DOMContentLoaded', initialize); | 213 document.addEventListener('DOMContentLoaded', initialize); |
115 | 214 |
116 return { | 215 return { |
216 inputFields: inputFields, | |
217 isValidating: isValidating, | |
218 getSave: getSave, | |
117 setDetails: setDetails, | 219 setDetails: setDetails, |
118 setValidation: setValidation, | 220 setValidation: setValidation, |
221 validate: validate | |
119 }; | 222 }; |
120 }); | 223 }); |
121 | 224 |
122 | 225 |
OLD | NEW |