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