OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('editSearchEngineDialog', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * Disables the controls while the dialog is busy. |
| 10 */ |
| 11 function disableControls() { |
| 12 $('cancel').disabled = true; |
| 13 $('save').disabled = true; |
| 14 } |
| 15 |
| 16 /** |
| 17 * Close the dialog and pass a result value to the dialog close handler. |
| 18 * @param {{description: string, details: string, url: string}=} opt_result |
| 19 * The value to pass to the dialog close handler. |
| 20 */ |
| 21 function closeWithResult(opt_result) { |
| 22 disableControls(); |
| 23 var json = JSON.stringify(opt_result ? [opt_result] : []); |
| 24 chrome.send('DialogClose', [json]); |
| 25 } |
| 26 |
| 27 /** |
| 28 * Sets the text of the dialog's editable text boxes. |
| 29 * @param {{description: string, details: string, url: string}} details Values |
| 30 * for corresponding text fields. |
| 31 */ |
| 32 function setDetails(details) { |
| 33 $('description-text').value = details.description; |
| 34 $('keyword-text').value = details.keyword; |
| 35 $('url-text').value = details.url; |
| 36 validate(); |
| 37 } |
| 38 |
| 39 /** |
| 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 |
| 50 * by calling setValidation. |
| 51 */ |
| 52 function validate() { |
| 53 chrome.send('requestValidation', [$('description-text').value, |
| 54 $('keyword-text').value, $('url-text').value]); |
| 55 } |
| 56 |
| 57 /** |
| 58 * Sets dialog state given the results of the validation of input by Chrome. |
| 59 * @param {{description: boolean, details: boolean, url: boolean, ok:boolean}} |
| 60 * details A dictionary of booleans indicating the validation results of |
| 61 * various parts of the UI. |description|, |details| and |url| indicate |
| 62 * the validity of the respective text fields, and |ok| indicates whether |
| 63 * the OK/Save button can be pressed. |
| 64 */ |
| 65 function setValidation(details) { |
| 66 setValidImage($('description-icon'), details.description); |
| 67 setValidImage($('keyword-icon'), details.keyword); |
| 68 setValidImage($('url-icon'), details.url); |
| 69 $('save').disabled = !details.ok; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Reverses the order of child nodes. |
| 74 * @param {HTMLElement} parent The parent node whose children are to be |
| 75 * reversed. |
| 76 */ |
| 77 function reverseChildren(parent) { |
| 78 var childNodes = parent.childNodes; |
| 79 for (var i = childNodes.length - 1; i >= 0; i--) |
| 80 parent.appendChild(childNodes[i]); |
| 81 }; |
| 82 |
| 83 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
| 84 |
| 85 /** |
| 86 * Inserts translated strings on loading. |
| 87 */ |
| 88 function initialize() { |
| 89 i18nTemplate.process(document, templateData); |
| 90 |
| 91 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : |
| 92 templateData.titleEdit; |
| 93 |
| 94 $('cancel').onclick = function() { |
| 95 closeWithResult(); |
| 96 } |
| 97 |
| 98 $('save').onclick = function() { |
| 99 closeWithResult({description: $('description-text').value, |
| 100 keyword: $('keyword-text').value, |
| 101 url: $('url-text').value}); |
| 102 } |
| 103 |
| 104 $('description-text').oninput = validate; |
| 105 $('keyword-text').oninput = validate; |
| 106 $('url-text').oninput = validate; |
| 107 |
| 108 setValidation({description: false, keyword: false, url: false}); |
| 109 if (cr.isViews) |
| 110 forEach(document.querySelectorAll('.button-strip'), reverseChildren); |
| 111 chrome.send('requestDetails') |
| 112 } |
| 113 |
| 114 document.addEventListener('DOMContentLoaded', initialize); |
| 115 |
| 116 return { |
| 117 setDetails: setDetails, |
| 118 setValidation: setValidation, |
| 119 }; |
| 120 }); |
| 121 |
| 122 |
OLD | NEW |