OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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('options', function() { | |
6 const OptionsPage = options.OptionsPage; | |
7 | |
8 /** | |
9 * EditSearchEngineOverlay class | |
10 * Encapsulated handling of the 'Edit Search Engine' overlay page. | |
11 * @class | |
12 * @constructor | |
13 */ | |
14 function EditSearchEngineOverlay() { | |
15 OptionsPage.call(this, 'editSearchEngineOverlay', | |
16 templateData.editSearchEngineTitle, | |
17 'editSearchEngineOverlay'); | |
18 } | |
19 | |
20 cr.addSingletonGetter(EditSearchEngineOverlay); | |
21 | |
22 EditSearchEngineOverlay.prototype = { | |
23 __proto__: OptionsPage.prototype, | |
24 | |
25 /** | |
26 * Initializes the page. | |
27 */ | |
28 initializePage: function() { | |
29 OptionsPage.prototype.initializePage.call(this); | |
30 | |
31 var self = this; | |
32 var editForm = $('editSearchEngineForm'); | |
33 editForm.onreset = function(e) { | |
34 chrome.send('searchEngineEditCancelled'); | |
35 self.dismissOverlay_(); | |
36 }; | |
37 editForm.onsubmit = function(e) { | |
38 chrome.send('searchEngineEditCompleted', self.getInputFieldValues_()); | |
39 self.dismissOverlay_(); | |
40 return false; | |
41 }; | |
42 var fieldIDs = ['editSearchEngineName', | |
43 'editSearchEngineKeyword', | |
44 'editSearchEngineURL']; | |
45 for (var i = 0; i < fieldIDs.length; i++) { | |
46 var field = $(fieldIDs[i]); | |
47 field.oninput = this.validateFields_.bind(this); | |
48 field.onkeydown = function(e) { | |
49 if (e.keyCode == 27) // Esc | |
50 editForm.reset(); | |
51 }; | |
52 } | |
53 }, | |
54 | |
55 /** | |
56 * Clears any uncommited input, and dismisses the overlay. | |
57 * @private | |
58 */ | |
59 dismissOverlay_: function() { | |
60 this.setEditDetails_(); | |
61 OptionsPage.clearOverlays(); | |
62 }, | |
63 | |
64 /** | |
65 * Fills the text fields from the given search engine. | |
66 * @private | |
67 */ | |
68 setEditDetails_: function(engineDetails) { | |
69 if (engineDetails) { | |
70 $('editSearchEngineName').value = engineDetails['name']; | |
71 $('editSearchEngineKeyword').value = engineDetails['keyword']; | |
72 var urlField = $('editSearchEngineURL'); | |
73 urlField.value = engineDetails['url']; | |
74 urlField.disabled = engineDetails['urlLocked']; | |
75 this.validateFields_(); | |
76 } else { | |
77 $('editSearchEngineName').value = ''; | |
78 $('editSearchEngineKeyword').value = ''; | |
79 $('editSearchEngineURL').value = ''; | |
80 var invalid = { name: false, keyword: false, url: false }; | |
81 this.updateValidityWithResults_(invalid); | |
82 } | |
83 }, | |
84 | |
85 /** | |
86 * Starts the process of asynchronously validating the user input. Results | |
87 * will be reported to updateValidityWithResults_. | |
88 * @private | |
89 */ | |
90 validateFields_: function() { | |
91 chrome.send('checkSearchEngineInfoValidity', this.getInputFieldValues_()); | |
92 }, | |
93 | |
94 /** | |
95 * Sets the validation images and the enabled state of the Add button based | |
96 * on the current values of the text fields. | |
97 * @private | |
98 * @param {Object} The dictionary of validity states. | |
99 */ | |
100 updateValidityWithResults_: function(validity) { | |
101 this.setBadgeValidity_($('editSearchEngineNameValidity'), | |
102 validity['name'], | |
103 'editSearchEngineInvalidTitleToolTip'); | |
104 this.setBadgeValidity_($('editSearchEngineKeywordValidity'), | |
105 validity['keyword'], | |
106 'editSearchEngineInvalidKeywordToolTip'); | |
107 this.setBadgeValidity_($('editSearchEngineURLValidity'), | |
108 validity['url'], | |
109 'editSearchEngineInvalidURLToolTip'); | |
110 $('editSearchEngineOkayButton').disabled = | |
111 !(validity['name'] && validity['keyword'] && validity['url']); | |
112 }, | |
113 | |
114 /** | |
115 * Updates the state of the given validity indicator badge. | |
116 * @private | |
117 * @param {HTMLElement} The badge element to adjust. | |
118 * @param {boolean} Whether or not the badge should be set to the valid | |
119 * state. | |
120 * @param {string} The tooltip string id for the invalid state. | |
121 */ | |
122 setBadgeValidity_: function(element, isValid, tooltip_id) { | |
123 if (isValid) { | |
124 element.className = 'valid-badge'; | |
125 element.title = ''; | |
126 } else { | |
127 element.className = 'alert-badge'; | |
128 element.title = localStrings.getString(tooltip_id); | |
129 } | |
130 }, | |
131 | |
132 /** | |
133 * Returns the input field values as an array suitable for passing to | |
134 * chrome.send. The order of the array is important. | |
135 * @private | |
136 * @return {array} The current input field values. | |
137 */ | |
138 getInputFieldValues_: function() { | |
139 return [ $('editSearchEngineName').value, | |
140 $('editSearchEngineKeyword').value, | |
141 $('editSearchEngineURL').value ]; | |
142 } | |
143 }; | |
144 | |
145 EditSearchEngineOverlay.setEditDetails = function(engineDetails) { | |
146 EditSearchEngineOverlay.getInstance().setEditDetails_(engineDetails); | |
147 }; | |
148 | |
149 EditSearchEngineOverlay.validityCheckCallback = function(validity) { | |
150 EditSearchEngineOverlay.getInstance().updateValidityWithResults_(validity); | |
151 }; | |
152 | |
153 // Export | |
154 return { | |
155 EditSearchEngineOverlay: EditSearchEngineOverlay | |
156 }; | |
157 | |
158 }); | |
OLD | NEW |