Index: chrome/browser/resources/edit_search_engine_dialog.js |
diff --git a/chrome/browser/resources/edit_search_engine_dialog.js b/chrome/browser/resources/edit_search_engine_dialog.js |
index 0d9cdb375966bb2343c7f488b399d1e0e2922773..00989c3cb83ccb82d03737a702edc527056b76e2 100644 |
--- a/chrome/browser/resources/edit_search_engine_dialog.js |
+++ b/chrome/browser/resources/edit_search_engine_dialog.js |
@@ -6,6 +6,87 @@ cr.define('editSearchEngineDialog', function() { |
'use strict'; |
/** |
+ * Flag inidicating if we are in the process of validating input. While |
+ * validating, the validity of the inputs is indeterminate. |
+ * @type {boolean} |
+ * @private |
+ */ |
+ var isValidating_ = false; |
+ |
+ /** |
+ * Accessor for in entry field in the search engine dialog. |
+ * @param {string} baseName Name of the field, which servers as a base name |
+ * for the text input field and icon. |
+ * @constructor |
+ */ |
+ function SearchEngineDialogEntryField(baseName) { |
+ this.name_ = baseName; |
+ this.text_ = $(baseName + '-text'); |
+ this.icon_ = $(baseName + '-icon'); |
+ this.text_.oninput = validate; |
+ return this; |
+ } |
+ |
+ SearchEngineDialogEntryField.prototype = { |
+ |
+ /* |
+ * Retrieves the name of the field. |
+ * @type {string} |
+ */ |
+ get name() { |
+ return this.name_; |
+ }, |
+ |
+ /* |
+ * Retrieves the content of the input field. |
+ * @type {string} |
+ */ |
+ get value() { |
+ return this.text_.value; |
+ }, |
+ |
+ /** |
+ * Sets the content of the input field. |
+ * @type {string} |
+ */ |
+ set value(text) { |
+ this.text_.value = text; |
+ }, |
+ |
+ /** |
+ * Get indicator of the validity of an input field. |
+ * @type {boolean} |
+ */ |
+ get valid() { |
+ return this.icon_.className == 'valid'; |
+ }, |
+ |
+ /** |
+ * Set indicator for whether the input field is valid. |
+ * @type {boolean} |
+ */ |
+ set valid(state) { |
+ this.icon_.className = state ? 'valid' : 'invalid'; |
+ }, |
+ |
+ /** |
+ * Creates a text representation of the class containing the name, |
+ * text field contents and validity. |
+ * @return {string} Text representation. |
+ */ |
+ toString: function() { |
+ return this.name_ + ': \'' + this.text_.value + '\' (' + |
+ this.icon_.className + ')'; |
+ } |
+ }; |
+ |
+ /** |
+ * Accessors for entry fields in the search engine dialog. Initialized after |
+ * content is loaded. |
Sheridan Rawlins
2011/11/28 22:32:45
nit: @type
kevers
2011/11/29 17:36:37
Done.
|
+ */ |
+ var InputFields = {}; |
Sheridan Rawlins
2011/11/28 22:32:45
nit: inputFields.
kevers
2011/11/29 17:36:37
Done.
|
+ |
+ /** |
* Disables the controls while the dialog is busy. |
*/ |
function disableControls() { |
@@ -30,43 +111,39 @@ cr.define('editSearchEngineDialog', function() { |
* for corresponding text fields. |
*/ |
function setDetails(details) { |
- $('description-text').value = details.description; |
- $('keyword-text').value = details.keyword; |
- $('url-text').value = details.url; |
+ InputFields.description.value = details.description; |
+ InputFields.keyword.value = details.keyword; |
+ InputFields.url.value = details.url; |
validate(); |
} |
/** |
- * Updates the validity icon element by changing its style. |
- * @param {Object} element The element to change. |
- * @param {boolean} valid True if the data is valid. |
- */ |
- function setValidImage(element, valid) { |
- element.className = valid ? 'valid' : 'invalid'; |
- } |
- |
- /** |
* Sends all strings to Chrome for validation. Chrome is expected to respond |
* by calling setValidation. |
*/ |
function validate() { |
- chrome.send('requestValidation', [$('description-text').value, |
- $('keyword-text').value, $('url-text').value]); |
+ isValidating_ = true; |
+ chrome.send('requestValidation', [InputFields.description.value, |
+ InputFields.keyword.value, InputFields.url.value]); |
} |
/** |
* Sets dialog state given the results of the validation of input by Chrome. |
- * @param {{description: boolean, details: boolean, url: boolean, ok:boolean}} |
- * details A dictionary of booleans indicating the validation results of |
- * various parts of the UI. |description|, |details| and |url| indicate |
- * the validity of the respective text fields, and |ok| indicates whether |
+ * @param {{description: boolean, |
+ keyword: boolean, |
+ url: boolean, |
+ ok: boolean}} details |
+ * A dictionary of booleans indicating the validation results of various |
+ * parts of the UI. |description|, |keyword| and |url| indicate the |
+ * validity of the respective text fields, and |ok| indicates whether |
* the OK/Save button can be pressed. |
*/ |
function setValidation(details) { |
- setValidImage($('description-icon'), details.description); |
- setValidImage($('keyword-icon'), details.keyword); |
- setValidImage($('url-icon'), details.url); |
+ InputFields.description.valid = details.description; |
+ InputFields.keyword.valid = details.keyword; |
+ InputFields.url.valid = details.url; |
$('save').disabled = !details.ok; |
+ isValidating_ = false; |
} |
/** |
@@ -78,7 +155,7 @@ cr.define('editSearchEngineDialog', function() { |
var childNodes = parent.childNodes; |
for (var i = childNodes.length - 1; i >= 0; i--) |
parent.appendChild(childNodes[i]); |
- }; |
+ } |
var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
@@ -88,34 +165,58 @@ cr.define('editSearchEngineDialog', function() { |
function initialize() { |
i18nTemplate.process(document, templateData); |
+ InputFields.description = new SearchEngineDialogEntryField('description'); |
+ InputFields.keyword = new SearchEngineDialogEntryField('keyword'); |
+ InputFields.url = new SearchEngineDialogEntryField('url'); |
+ |
document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : |
templateData.titleEdit; |
$('cancel').onclick = function() { |
closeWithResult(); |
- } |
+ }; |
$('save').onclick = function() { |
- closeWithResult({description: $('description-text').value, |
- keyword: $('keyword-text').value, |
- url: $('url-text').value}); |
- } |
- |
- $('description-text').oninput = validate; |
- $('keyword-text').oninput = validate; |
- $('url-text').oninput = validate; |
+ closeWithResult({description: InputFields.description.value, |
+ keyword: InputFields.keyword.value, |
+ url: InputFields.url.value}); |
+ }; |
setValidation({description: false, keyword: false, url: false}); |
if (cr.isViews) |
forEach(document.querySelectorAll('.button-strip'), reverseChildren); |
- chrome.send('requestDetails') |
+ // Mark that we are in the process of validating, since the 'send' call |
+ // is asynchronous. Until the next call to 'setValidation' complete, the |
+ // validity of the inputs is in an indeterminate state. |
+ isValidating_ = true; |
+ chrome.send('requestDetails'); |
+ } |
+ |
+ /** |
+ * Indicates if we are in the process of validating input. |
+ * @return {boolean} True if validation is in progress. |
+ */ |
+ function isValidating() { |
+ return isValidating_; |
+ } |
+ |
+ /** |
+ * Retrieves the save button element. |
+ * @return {Element} |
+ */ |
+ function getSave() { |
+ return $('save'); |
} |
document.addEventListener('DOMContentLoaded', initialize); |
return { |
+ InputFields: InputFields, |
+ isValidating: isValidating, |
+ getSave: getSave, |
setDetails: setDetails, |
setValidation: setValidation, |
+ validate: validate |
}; |
}); |