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