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 * Flag indicating if 'setValidation' should reset the 'isValidating_' flag. | |
| 18 * We do not reset the flag during initialization. | |
| 19 * @type {boolean} | |
| 20 * @private | |
| 21 */ | |
| 22 var armRevalidation_ = false; | |
| 23 | |
| 24 /** | |
| 9 * Disables the controls while the dialog is busy. | 25 * Disables the controls while the dialog is busy. |
| 10 */ | 26 */ |
| 11 function disableControls() { | 27 function disableControls() { |
| 12 $('cancel').disabled = true; | 28 $('cancel').disabled = true; |
| 13 $('save').disabled = true; | 29 $('save').disabled = true; |
| 14 } | 30 } |
| 15 | 31 |
| 16 /** | 32 /** |
| 17 * Close the dialog and pass a result value to the dialog close handler. | 33 * Close the dialog and pass a result value to the dialog close handler. |
| 18 * @param {{description: string, details: string, url: string}=} opt_result | 34 * @param {{description: string, details: string, url: string}=} opt_result |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 43 */ | 59 */ |
| 44 function setValidImage(element, valid) { | 60 function setValidImage(element, valid) { |
| 45 element.className = valid ? 'valid' : 'invalid'; | 61 element.className = valid ? 'valid' : 'invalid'; |
| 46 } | 62 } |
| 47 | 63 |
| 48 /** | 64 /** |
| 49 * Sends all strings to Chrome for validation. Chrome is expected to respond | 65 * Sends all strings to Chrome for validation. Chrome is expected to respond |
| 50 * by calling setValidation. | 66 * by calling setValidation. |
| 51 */ | 67 */ |
| 52 function validate() { | 68 function validate() { |
| 69 isValidating_ = true; | |
| 53 chrome.send('requestValidation', [$('description-text').value, | 70 chrome.send('requestValidation', [$('description-text').value, |
| 54 $('keyword-text').value, $('url-text').value]); | 71 $('keyword-text').value, $('url-text').value]); |
| 55 } | 72 } |
| 56 | 73 |
| 57 /** | 74 /** |
| 58 * Sets dialog state given the results of the validation of input by Chrome. | 75 * Sets dialog state given the results of the validation of input by Chrome. |
| 59 * @param {{description: boolean, details: boolean, url: boolean, ok:boolean}} | 76 * @param {{description: boolean, |
| 60 * details A dictionary of booleans indicating the validation results of | 77 keyword: boolean, |
| 61 * various parts of the UI. |description|, |details| and |url| indicate | 78 url: boolean, |
| 62 * the validity of the respective text fields, and |ok| indicates whether | 79 ok: boolean}} details |
| 80 * A dictionary of booleans indicating the validation results of various | |
| 81 * parts of the UI. |description|, |keyword| and |url| indicate the | |
| 82 * validity of the respective text fields, and |ok| indicates whether | |
| 63 * the OK/Save button can be pressed. | 83 * the OK/Save button can be pressed. |
| 64 */ | 84 */ |
| 65 function setValidation(details) { | 85 function setValidation(details) { |
| 66 setValidImage($('description-icon'), details.description); | 86 setValidImage($('description-icon'), details.description); |
| 67 setValidImage($('keyword-icon'), details.keyword); | 87 setValidImage($('keyword-icon'), details.keyword); |
| 68 setValidImage($('url-icon'), details.url); | 88 setValidImage($('url-icon'), details.url); |
| 69 $('save').disabled = !details.ok; | 89 $('save').disabled = !details.ok; |
| 90 if (armRevalidation_) | |
| 91 isValidating_ = false; | |
| 70 } | 92 } |
| 71 | 93 |
| 72 /** | 94 /** |
| 73 * Reverses the order of child nodes. | 95 * Reverses the order of child nodes. |
| 74 * @param {HTMLElement} parent The parent node whose children are to be | 96 * @param {HTMLElement} parent The parent node whose children are to be |
| 75 * reversed. | 97 * reversed. |
| 76 */ | 98 */ |
| 77 function reverseChildren(parent) { | 99 function reverseChildren(parent) { |
| 78 var childNodes = parent.childNodes; | 100 var childNodes = parent.childNodes; |
| 79 for (var i = childNodes.length - 1; i >= 0; i--) | 101 for (var i = childNodes.length - 1; i >= 0; i--) |
| 80 parent.appendChild(childNodes[i]); | 102 parent.appendChild(childNodes[i]); |
| 81 }; | 103 }; |
| 82 | 104 |
| 83 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); | 105 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
| 84 | 106 |
| 85 /** | 107 /** |
| 86 * Inserts translated strings on loading. | 108 * Inserts translated strings on loading. |
| 87 */ | 109 */ |
| 88 function initialize() { | 110 function initialize() { |
| 111 // The initialization process triggers asynchronous calls. Until these | |
| 112 // calls complete, the validity of the inputs is in an indeterminate | |
| 113 // state. | |
| 114 isValidating_ = true; | |
| 115 | |
| 89 i18nTemplate.process(document, templateData); | 116 i18nTemplate.process(document, templateData); |
| 90 | 117 |
| 91 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : | 118 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : |
| 92 templateData.titleEdit; | 119 templateData.titleEdit; |
| 93 | 120 |
| 94 $('cancel').onclick = function() { | 121 $('cancel').onclick = function() { |
| 95 closeWithResult(); | 122 closeWithResult(); |
| 96 } | 123 } |
| 97 | 124 |
| 98 $('save').onclick = function() { | 125 $('save').onclick = function() { |
| 99 closeWithResult({description: $('description-text').value, | 126 closeWithResult({description: $('description-text').value, |
| 100 keyword: $('keyword-text').value, | 127 keyword: $('keyword-text').value, |
| 101 url: $('url-text').value}); | 128 url: $('url-text').value}); |
| 102 } | 129 } |
| 103 | 130 |
| 104 $('description-text').oninput = validate; | 131 $('description-text').oninput = validate; |
| 105 $('keyword-text').oninput = validate; | 132 $('keyword-text').oninput = validate; |
| 106 $('url-text').oninput = validate; | 133 $('url-text').oninput = validate; |
| 107 | 134 |
| 135 // Do not mark the input as validated during the initial reset. | |
| 136 armRevalidation_ = false; | |
| 108 setValidation({description: false, keyword: false, url: false}); | 137 setValidation({description: false, keyword: false, url: false}); |
| 109 if (cr.isViews) | 138 if (cr.isViews) |
| 110 forEach(document.querySelectorAll('.button-strip'), reverseChildren); | 139 forEach(document.querySelectorAll('.button-strip'), reverseChildren); |
| 111 chrome.send('requestDetails') | 140 // The next call to set validation will have the correct flags. |
| 141 armRevalidation_ = true; | |
|
flackr
2011/11/23 15:22:37
Is there any reason we can't set isValidating_ her
kevers
2011/11/23 20:04:53
Done.
| |
| 142 chrome.send('requestDetails'); | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * Indicates if we are in the process of validating input. | |
| 147 * @return {boolean} True if validation is in progress. | |
| 148 */ | |
| 149 function isValidating() { | |
| 150 return isValidating_; | |
| 112 } | 151 } |
| 113 | 152 |
| 114 document.addEventListener('DOMContentLoaded', initialize); | 153 document.addEventListener('DOMContentLoaded', initialize); |
| 115 | 154 |
| 116 return { | 155 return { |
| 156 isValidating: isValidating, | |
| 117 setDetails: setDetails, | 157 setDetails: setDetails, |
| 118 setValidation: setValidation, | 158 setValidation: setValidation, |
| 119 }; | 159 }; |
| 120 }); | 160 }); |
| 121 | 161 |
| 122 | 162 |
| OLD | NEW |