| 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. |
| 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>} |
| 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, |
| 54 $('keyword-text').value, $('url-text').value]); | 122 inputFields.keyword.value, |
| 123 inputFields.url.value]); |
| 55 } | 124 } |
| 56 | 125 |
| 57 /** | 126 /** |
| 58 * Sets dialog state given the results of the validation of input by Chrome. | 127 * Sets dialog state given the results of the validation of input by Chrome. |
| 59 * @param {{description: boolean, details: boolean, url: boolean, ok:boolean}} | 128 * @param {{description: boolean, |
| 60 * details A dictionary of booleans indicating the validation results of | 129 keyword: boolean, |
| 61 * various parts of the UI. |description|, |details| and |url| indicate | 130 url: boolean, |
| 62 * the validity of the respective text fields, and |ok| indicates whether | 131 ok: boolean}} details |
| 132 * A dictionary of booleans indicating the validation results of various |
| 133 * parts of the UI. |description|, |keyword| and |url| indicate the |
| 134 * validity of the respective text fields, and |ok| indicates whether |
| 63 * the OK/Save button can be pressed. | 135 * the OK/Save button can be pressed. |
| 64 */ | 136 */ |
| 65 function setValidation(details) { | 137 function setValidation(details) { |
| 66 setValidImage($('description-icon'), details.description); | 138 inputFields.description.valid = details.description; |
| 67 setValidImage($('keyword-icon'), details.keyword); | 139 inputFields.keyword.valid = details.keyword; |
| 68 setValidImage($('url-icon'), details.url); | 140 inputFields.url.valid = details.url; |
| 69 $('save').disabled = !details.ok; | 141 $('save').disabled = !details.ok; |
| 70 } | 142 } |
| 71 | 143 |
| 72 /** | 144 /** |
| 73 * Reverses the order of child nodes. | 145 * Reverses the order of child nodes. |
| 74 * @param {HTMLElement} parent The parent node whose children are to be | 146 * @param {HTMLElement} parent The parent node whose children are to be |
| 75 * reversed. | 147 * reversed. |
| 76 */ | 148 */ |
| 77 function reverseChildren(parent) { | 149 function reverseChildren(parent) { |
| 78 var childNodes = parent.childNodes; | 150 var childNodes = parent.childNodes; |
| 79 for (var i = childNodes.length - 1; i >= 0; i--) | 151 for (var i = childNodes.length - 1; i >= 0; i--) |
| 80 parent.appendChild(childNodes[i]); | 152 parent.appendChild(childNodes[i]); |
| 81 }; | 153 } |
| 82 | 154 |
| 83 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); | 155 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
| 84 | 156 |
| 85 /** | 157 /** |
| 86 * Inserts translated strings on loading. | 158 * Inserts translated strings on loading. |
| 87 */ | 159 */ |
| 88 function initialize() { | 160 function initialize() { |
| 89 i18nTemplate.process(document, templateData); | 161 i18nTemplate.process(document, templateData); |
| 90 | 162 |
| 163 inputFields.description = new SearchEngineDialogEntryField('description'); |
| 164 inputFields.keyword = new SearchEngineDialogEntryField('keyword'); |
| 165 inputFields.url = new SearchEngineDialogEntryField('url'); |
| 166 |
| 91 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : | 167 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : |
| 92 templateData.titleEdit; | 168 templateData.titleEdit; |
| 93 | 169 |
| 94 $('cancel').onclick = function() { | 170 $('cancel').onclick = function() { |
| 95 closeWithResult(); | 171 closeWithResult(); |
| 96 } | 172 }; |
| 97 | 173 |
| 98 $('save').onclick = function() { | 174 $('save').onclick = function() { |
| 99 closeWithResult({description: $('description-text').value, | 175 closeWithResult({description: inputFields.description.value, |
| 100 keyword: $('keyword-text').value, | 176 keyword: inputFields.keyword.value, |
| 101 url: $('url-text').value}); | 177 url: inputFields.url.value}); |
| 102 } | 178 }; |
| 103 | |
| 104 $('description-text').oninput = validate; | |
| 105 $('keyword-text').oninput = validate; | |
| 106 $('url-text').oninput = validate; | |
| 107 | 179 |
| 108 setValidation({description: false, keyword: false, url: false}); | 180 setValidation({description: false, keyword: false, url: false}); |
| 109 if (cr.isViews) | 181 if (cr.isViews) |
| 110 forEach(document.querySelectorAll('.button-strip'), reverseChildren); | 182 forEach(document.querySelectorAll('.button-strip'), reverseChildren); |
| 111 chrome.send('requestDetails') | 183 |
| 184 // TODO(kevers): Should be a cleaner way to implement without requiring |
| 185 // multiple callback to C++. The call to |requestDetails| fetches the |
| 186 // content to insert into the input fields without inidicating if the |
| 187 // inputs are valid. A separate callback is then required to determine if |
| 188 // the inputs are OK. In fact, it should be possible to pass in the details |
| 189 // when the dialog is created rather than using a callback. |
| 190 chrome.send('requestDetails'); |
| 191 } |
| 192 |
| 193 /** |
| 194 * Retrieves the save button element. |
| 195 * @return {Element} |
| 196 */ |
| 197 function getSave() { |
| 198 return $('save'); |
| 112 } | 199 } |
| 113 | 200 |
| 114 document.addEventListener('DOMContentLoaded', initialize); | 201 document.addEventListener('DOMContentLoaded', initialize); |
| 115 | 202 |
| 116 return { | 203 return { |
| 204 inputFields: inputFields, |
| 205 getSave: getSave, |
| 117 setDetails: setDetails, | 206 setDetails: setDetails, |
| 118 setValidation: setValidation, | 207 setValidation: setValidation, |
| 208 validate: validate |
| 119 }; | 209 }; |
| 120 }); | 210 }); |
| 121 | 211 |
| 122 | 212 |
| OLD | NEW |