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