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 node.appendChild(childNodes[i]); | |
wyck
2011/10/12 13:52:59
oops. node instead of parent. I could use some t
| |
81 }; | |
82 | |
83 /** | |
84 * Executes a function for each item in a collection. Similar to Array.filter | |
85 * except that it works with other kinds of collections. | |
86 * @param {Object} collection The collection to iterate over. | |
87 * @param {function(Object)} fn The function to be executed for each item in | |
88 * the collection. | |
89 */ | |
90 function filter(collection, fn) { | |
arv (Not doing code reviews)
2011/10/11 21:41:06
var filter = Array.prototype.filter.call.bind(Arra
arv (Not doing code reviews)
2011/10/11 21:41:06
Did you mean forEach here?
wyck
2011/10/12 13:52:59
Yes, thanks.
| |
91 for (var i=0, len = collection.length; i<len; i++) | |
92 fn(collection[i]); | |
93 } | |
94 | |
95 /** | |
96 * Inserts translated strings on loading. | |
97 */ | |
98 function initialize() { | |
99 i18nTemplate.process(document, templateData); | |
100 | |
101 document.title = chrome.dialogArguments == 'add' ? templateData.titleNew : | |
102 templateData.titleEdit; | |
103 | |
104 $('cancel').onclick = function() { | |
105 closeWithResult(); | |
106 } | |
107 | |
108 $('save').onclick = function() { | |
109 closeWithResult({description: $('description-text').value, | |
110 keyword: $('keyword-text').value, | |
111 url: $('url-text').value}); | |
112 } | |
113 | |
114 $('description-text').oninput = validate; | |
115 $('keyword-text').oninput = validate; | |
116 $('url-text').oninput = validate; | |
117 | |
118 setValidation({description: false, keyword: false, url: false}); | |
119 if (cr.isViews) | |
120 filter(document.getElementsByClassName('button-strip'), reverseChildren); | |
arv (Not doing code reviews)
2011/10/11 21:41:06
document.qeurySelectorAll('.button-strip')
| |
121 chrome.send('requestDetails') | |
122 } | |
123 | |
124 document.addEventListener('DOMContentLoaded', initialize); | |
125 | |
126 return { | |
127 setDetails: setDetails, | |
128 setValidation: setValidation, | |
129 }; | |
130 }); | |
131 | |
132 | |
OLD | NEW |