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..8a624931ebf5f85f7e030b2ca460916bc6ed2e43 100644 |
--- a/chrome/browser/resources/edit_search_engine_dialog.js |
+++ b/chrome/browser/resources/edit_search_engine_dialog.js |
@@ -6,6 +6,83 @@ cr.define('editSearchEngineDialog', function() { |
'use strict'; |
/** |
+ * Accessor for an entry field in the search engine dialog. |
+ * @param {string} baseName Name of the field, which serves 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 = { |
+ /* |
+ * Getter for the name of the field. |
+ * @type {string} Descriptive name of the field. |
+ */ |
+ get name() { |
+ return this.name_; |
+ }, |
+ |
+ /* |
+ * Getter for the content of the input field. |
+ * @type {string} Text content in the input field. |
+ */ |
+ get value() { |
+ return this.text_.value; |
+ }, |
+ |
+ /** |
+ * Setter for the content of the input field. The validity of the input is |
+ * not automatically revalidated. |
+ * @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.
|
+ */ |
+ set value(text) { |
+ this.text_.value = text; |
+ // Validity is in an indeterminate state until validate is called. Clear |
+ // the class name associated with the icon. |
+ this.icon_.className = ''; |
+ }, |
+ |
+ /** |
+ * Getter for the validity of an input field. |
+ * @type {boolean} True if the text input is valid, otherwise false. |
+ */ |
+ get valid() { |
+ return this.icon_.className == 'valid'; |
+ }, |
+ |
+ /** |
+ * Setter for the input field validily. |
+ * @type {boolean} True if the input field is valid, false for invalid. |
+ */ |
+ 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. |
+ * @type{Object.<string,SearchEngineDialogEntryField>} |
Sheridan Rawlins
2011/12/02 17:58:23
space after @type.
kevers
2011/12/02 18:37:29
Done.
|
+ */ |
+ var inputFields = {}; |
+ |
+ /** |
* Disables the controls while the dialog is busy. |
*/ |
function disableControls() { |
@@ -30,42 +107,36 @@ 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]); |
+ 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.
|
+ 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; |
} |
@@ -78,7 +149,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 +159,52 @@ 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') |
+ |
+ // TODO(kevers): Should be a cleaner way to implement without requiring |
+ // multiple callback to C++. The call to |requestDetails| fetches the |
+ // content to insert into the input fields without inidicating if the |
+ // inputs are valid. A separate callback is then required to determine if |
+ // the inputs are OK. In fact, it should be possible to pass in the details |
+ // when the dialog is created rather than using a callback. |
+ chrome.send('requestDetails'); |
+ } |
+ |
+ /** |
+ * Retrieves the save button element. |
+ * @return {Element} |
+ */ |
+ function getSave() { |
+ return $('save'); |
} |
document.addEventListener('DOMContentLoaded', initialize); |
return { |
+ inputFields: inputFields, |
+ getSave: getSave, |
setDetails: setDetails, |
setValidation: setValidation, |
+ validate: validate |
}; |
}); |