OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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('options', function() { | 5 cr.define('options', function() { |
6 var OptionsPage = options.OptionsPage; | 6 const OptionsPage = options.OptionsPage; |
7 | 7 const ArrayDataModel = cr.ui.ArrayDataModel; |
8 // The offset of the first profile in either the address list or the credit | 8 const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; |
9 // card list. Consists of the header and the horizontal rule. | |
10 const addressOffset = 2; | |
11 const creditCardOffset = 3; | |
12 | 9 |
13 ///////////////////////////////////////////////////////////////////////////// | 10 ///////////////////////////////////////////////////////////////////////////// |
14 // AutoFillOptions class: | 11 // AutoFillOptions class: |
15 // | |
16 // TODO(jhawkins): Replace <select> with a DOMUI List. | |
17 | 12 |
18 /** | 13 /** |
19 * Encapsulated handling of AutoFill options page. | 14 * Encapsulated handling of AutoFill options page. |
20 * @constructor | 15 * @constructor |
21 */ | 16 */ |
22 function AutoFillOptions() { | 17 function AutoFillOptions() { |
23 this.numAddresses = 0; | |
24 this.numCreditCards = 0; | |
25 this.activeNavTab = null; | |
26 this.addressGUIDs = null; | |
27 this.creditCardGUIDs = null; | |
28 OptionsPage.call(this, | 18 OptionsPage.call(this, |
29 'autoFillOptions', | 19 'autoFillOptions', |
30 templateData.autoFillOptionsTitle, | 20 templateData.autoFillOptionsTitle, |
31 'autofill-options'); | 21 'autofill-options'); |
32 } | 22 } |
33 | 23 |
34 cr.addSingletonGetter(AutoFillOptions); | 24 cr.addSingletonGetter(AutoFillOptions); |
35 | 25 |
36 AutoFillOptions.prototype = { | 26 AutoFillOptions.prototype = { |
37 __proto__: OptionsPage.prototype, | 27 __proto__: OptionsPage.prototype, |
38 | 28 |
| 29 /** |
| 30 * The address list. |
| 31 * @type {DeletableItemList} |
| 32 * @private |
| 33 */ |
| 34 addressList_: null, |
| 35 |
| 36 /** |
| 37 * The credit card list. |
| 38 * @type {DeletableItemList} |
| 39 * @private |
| 40 */ |
| 41 creditCardList_: null, |
| 42 |
39 initializePage: function() { | 43 initializePage: function() { |
40 OptionsPage.prototype.initializePage.call(this); | 44 OptionsPage.prototype.initializePage.call(this); |
41 | 45 |
| 46 this.createAddressList_(); |
| 47 this.createCreditCardList_(); |
| 48 |
42 var self = this; | 49 var self = this; |
43 $('profile-list').onchange = function(event) { | 50 $('autofill-add-address').onclick = function(event) { |
44 self.updateButtonState_(); | |
45 }; | |
46 $('profile-list').addEventListener('dblclick', function(event) { | |
47 if ($('autoFillEnabled').checked) | |
48 self.editProfile_(); | |
49 }); | |
50 $('add-address-button').onclick = function(event) { | |
51 self.showAddAddressOverlay_(); | 51 self.showAddAddressOverlay_(); |
52 }; | 52 }; |
53 $('add-credit-card-button').onclick = function(event) { | 53 $('autofill-add-creditcard').onclick = function(event) { |
54 self.showAddCreditCardOverlay_(); | 54 self.showAddCreditCardOverlay_(); |
55 }; | 55 }; |
56 $('autofill-edit-button').onclick = function(event) { | |
57 self.editProfile_(); | |
58 }; | |
59 $('autofill-remove-button').onclick = function(event) { | |
60 self.removeProfile_(); | |
61 }; | |
62 | 56 |
63 Preferences.getInstance().addEventListener('autofill.enabled', | 57 // TODO(jhawkins): What happens when AutoFill is disabled whilst on the |
64 this.updateButtonState_.bind(this)); | 58 // AutoFill options page? |
65 }, | 59 }, |
66 | 60 |
67 /** | 61 /** |
| 62 * Creates, decorates and initializes the address list. |
| 63 * @private |
| 64 */ |
| 65 createAddressList_: function() { |
| 66 this.addressList_ = $('address-list'); |
| 67 options.autoFillOptions.AutoFillList.decorate(this.addressList_); |
| 68 this.addressList_.selectionModel = new ListSingleSelectionModel; |
| 69 this.addressList_.autoExpands = true; |
| 70 }, |
| 71 |
| 72 /** |
| 73 * Creates, decorates and initializes the credit card list. |
| 74 * @private |
| 75 */ |
| 76 createCreditCardList_: function() { |
| 77 this.creditCardList_ = $('creditcard-list'); |
| 78 options.autoFillOptions.AutoFillList.decorate(this.creditCardList_); |
| 79 this.creditCardList_.selectionModel = new ListSingleSelectionModel; |
| 80 this.creditCardList_.autoExpands = true; |
| 81 }, |
| 82 |
| 83 /** |
68 * Shows the 'Add address' overlay, specifically by loading the | 84 * Shows the 'Add address' overlay, specifically by loading the |
69 * 'Edit address' overlay, emptying the input fields and modifying the | 85 * 'Edit address' overlay, emptying the input fields and modifying the |
70 * overlay title. | 86 * overlay title. |
71 * @private | 87 * @private |
72 */ | 88 */ |
73 showAddAddressOverlay_: function() { | 89 showAddAddressOverlay_: function() { |
74 var title = localStrings.getString('addAddressTitle'); | 90 var title = localStrings.getString('addAddressTitle'); |
75 AutoFillEditAddressOverlay.setTitle(title); | 91 AutoFillEditAddressOverlay.setTitle(title); |
76 AutoFillEditAddressOverlay.clearInputFields(); | 92 AutoFillEditAddressOverlay.clearInputFields(); |
77 OptionsPage.showOverlay('autoFillEditAddressOverlay'); | 93 OptionsPage.showOverlay('autoFillEditAddressOverlay'); |
78 }, | 94 }, |
79 | 95 |
80 /** | 96 /** |
81 * Shows the 'Edit address' overlay, using the data in |address| to fill the | |
82 * input fields. |address| is a list with one item, an associative array | |
83 * that contains the address data. | |
84 * @private | |
85 */ | |
86 showEditAddressOverlay_: function(address) { | |
87 var title = localStrings.getString('editAddressTitle'); | |
88 AutoFillEditAddressOverlay.setTitle(title); | |
89 AutoFillEditAddressOverlay.loadAddress(address[0]); | |
90 OptionsPage.showOverlay('autoFillEditAddressOverlay'); | |
91 }, | |
92 | |
93 /** | |
94 * Shows the 'Add credit card' overlay, specifically by loading the | 97 * Shows the 'Add credit card' overlay, specifically by loading the |
95 * 'Edit credit card' overlay, emptying the input fields and modifying the | 98 * 'Edit credit card' overlay, emptying the input fields and modifying the |
96 * overlay title. | 99 * overlay title. |
97 * @private | 100 * @private |
98 */ | 101 */ |
99 showAddCreditCardOverlay_: function() { | 102 showAddCreditCardOverlay_: function() { |
100 var title = localStrings.getString('addCreditCardTitle'); | 103 var title = localStrings.getString('addCreditCardTitle'); |
101 AutoFillEditCreditCardOverlay.setTitle(title); | 104 AutoFillEditCreditCardOverlay.setTitle(title); |
102 AutoFillEditCreditCardOverlay.clearInputFields(); | 105 AutoFillEditCreditCardOverlay.clearInputFields(); |
103 OptionsPage.showOverlay('autoFillEditCreditCardOverlay'); | 106 OptionsPage.showOverlay('autoFillEditCreditCardOverlay'); |
104 }, | 107 }, |
105 | 108 |
106 /** | 109 /** |
| 110 * Updates the data model for the address list with the values from |
| 111 * |entries|. |
| 112 * @param {Array} entries The list of addresses. |
| 113 */ |
| 114 setAddressList_: function(entries) { |
| 115 this.addressList_.dataModel = new ArrayDataModel(entries); |
| 116 }, |
| 117 |
| 118 /** |
| 119 * Updates the data model for the credit card list with the values from |
| 120 * |entries|. |
| 121 * @param {Array} entries The list of credit cards. |
| 122 */ |
| 123 setCreditCardList_: function(entries) { |
| 124 this.creditCardList_.dataModel = new ArrayDataModel(entries); |
| 125 }, |
| 126 |
| 127 /** |
| 128 * Removes the AutoFill profile represented by |guid|. |
| 129 * @param {String} guid The GUID of the profile to remove. |
| 130 * @private |
| 131 */ |
| 132 removeAutoFillProfile_: function(guid) { |
| 133 chrome.send('removeAutoFillProfile', [guid]); |
| 134 }, |
| 135 |
| 136 /** |
| 137 * Requests profile data for the profile represented by |guid| from the |
| 138 * PersonalDataManager. Once the data is loaded, the AutoFillOptionsHandler |
| 139 * calls showEdit[Address,CreditCard]Overlay(), depending on the type of the |
| 140 * profile. |
| 141 * @param {String} guid The GUID of the profile to edit. |
| 142 * @private |
| 143 */ |
| 144 loadProfileEditor_: function(guid) { |
| 145 chrome.send('loadProfileEditor', [guid]); |
| 146 }, |
| 147 |
| 148 /** |
| 149 * Shows the 'Edit address' overlay, using the data in |address| to fill the |
| 150 * input fields. |address| is a list with one item, an associative array |
| 151 * that contains the address data. |
| 152 * @private |
| 153 */ |
| 154 showEditAddressOverlay_: function(address) { |
| 155 var title = localStrings.getString('editAddressTitle'); |
| 156 AutoFillEditAddressOverlay.setTitle(title); |
| 157 AutoFillEditAddressOverlay.loadAddress(address[0]); |
| 158 OptionsPage.showOverlay('autoFillEditAddressOverlay'); |
| 159 }, |
| 160 |
| 161 /** |
107 * Shows the 'Edit credit card' overlay, using the data in |credit_card| to | 162 * Shows the 'Edit credit card' overlay, using the data in |credit_card| to |
108 * fill the input fields. |address| is a list with one item, an associative | 163 * fill the input fields. |address| is a list with one item, an associative |
109 * array that contains the credit card data. | 164 * array that contains the credit card data. |
110 * @private | 165 * @private |
111 */ | 166 */ |
112 showEditCreditCardOverlay_: function(creditCard) { | 167 showEditCreditCardOverlay_: function(creditCard) { |
113 var title = localStrings.getString('editCreditCardTitle'); | 168 var title = localStrings.getString('editCreditCardTitle'); |
114 AutoFillEditCreditCardOverlay.setTitle(title); | 169 AutoFillEditCreditCardOverlay.setTitle(title); |
115 AutoFillEditCreditCardOverlay.loadCreditCard(creditCard[0]); | 170 AutoFillEditCreditCardOverlay.loadCreditCard(creditCard[0]); |
116 OptionsPage.showOverlay('autoFillEditCreditCardOverlay'); | 171 OptionsPage.showOverlay('autoFillEditCreditCardOverlay'); |
117 }, | 172 }, |
118 | |
119 /** | |
120 * Resets the address list. This method leaves the header and horizontal | |
121 * rule unchanged. | |
122 * @private | |
123 */ | |
124 resetAddresses_: function() { | |
125 var profiles = $('profile-list'); | |
126 for (var i = 0; i < this.numAddresses; ++i) | |
127 profiles.remove(addressOffset); | |
128 this.numAddresses = 0; | |
129 }, | |
130 | |
131 /** | |
132 * Resets the credit card list. This method leaves the header and horizontal | |
133 * rule unchanged. | |
134 * @private | |
135 */ | |
136 resetCreditCards_: function() { | |
137 var profiles = $('profile-list'); | |
138 var offset = this.numAddresses + addressOffset + creditCardOffset; | |
139 for (var i = 0; i < this.numCreditCards; ++i) | |
140 profiles.remove(offset); | |
141 this.numCreditCards = 0; | |
142 }, | |
143 | |
144 /** | |
145 * Updates the address list with the given entries. | |
146 * @private | |
147 * @param {Array} address List of addresses. | |
148 */ | |
149 updateAddresses_: function(addresses) { | |
150 this.resetAddresses_(); | |
151 var profileList = $('profile-list'); | |
152 var blankAddress = profileList.options[addressOffset]; | |
153 this.numAddresses = addresses.length; | |
154 this.addressGUIDs = new Array(this.numAddresses); | |
155 for (var i = 0; i < this.numAddresses; i++) { | |
156 var address = addresses[i]; | |
157 var option = new Option(address['label']); | |
158 this.addressGUIDs[i] = address['guid']; | |
159 profileList.add(option, blankAddress); | |
160 } | |
161 | |
162 this.updateButtonState_(); | |
163 }, | |
164 | |
165 /** | |
166 * Updates the credit card list with the given entries. | |
167 * @private | |
168 * @param {Array} creditCards List of credit cards. | |
169 */ | |
170 updateCreditCards_: function(creditCards) { | |
171 this.resetCreditCards_(); | |
172 var profileList = $('profile-list'); | |
173 this.numCreditCards = creditCards.length; | |
174 this.creditCardGUIDs = new Array(this.numCreditCards); | |
175 for (var i = 0; i < this.numCreditCards; i++) { | |
176 var creditCard = creditCards[i]; | |
177 var option = new Option(creditCard['label']); | |
178 this.creditCardGUIDs[i] = creditCard['guid']; | |
179 profileList.add(option, null); | |
180 } | |
181 | |
182 this.updateButtonState_(); | |
183 }, | |
184 | |
185 /** | |
186 * Sets the enabled state of the AutoFill Add Address and Credit Card | |
187 * buttons on the current state of the |autoFillEnabled| checkbox. | |
188 * Sets the enabled state of the AutoFill Edit and Remove buttons based on | |
189 * the current selection in the profile list. | |
190 * @private | |
191 */ | |
192 updateButtonState_: function() { | |
193 var disabled = !$('autoFillEnabled').checked; | |
194 $('add-address-button').disabled = disabled; | |
195 $('add-credit-card-button').disabled = disabled; | |
196 | |
197 disabled = disabled || ($('profile-list').selectedIndex == -1); | |
198 $('autofill-remove-button').disabled = disabled; | |
199 $('autofill-edit-button').disabled = disabled; | |
200 }, | |
201 | |
202 /** | |
203 * Calls into the browser to load either an address or a credit card, | |
204 * depending on the selected index. The browser calls back into either | |
205 * editAddress() or editCreditCard() which show their respective editors. | |
206 * @private | |
207 */ | |
208 editProfile_: function() { | |
209 var idx = $('profile-list').selectedIndex; | |
210 if ((profileIndex = this.getAddressIndex_(idx)) != -1) { | |
211 chrome.send('editAddress', [this.addressGUIDs[profileIndex]]); | |
212 } else if ((profileIndex = this.getCreditCardIndex_(idx)) != -1) { | |
213 chrome.send('editCreditCard', [this.creditCardGUIDs[profileIndex]]); | |
214 } | |
215 }, | |
216 | |
217 /** | |
218 * Removes the currently selected profile, whether it's an address or a | |
219 * credit card. | |
220 * @private | |
221 */ | |
222 removeProfile_: function() { | |
223 var idx = $('profile-list').selectedIndex; | |
224 if ((profileIndex = this.getAddressIndex_(idx)) != -1) | |
225 chrome.send('removeAddress', [this.addressGUIDs[profileIndex]]); | |
226 else if ((profileIndex = this.getCreditCardIndex_(idx)) != -1) | |
227 chrome.send('removeCreditCard', [this.creditCardGUIDs[profileIndex]]); | |
228 }, | |
229 | |
230 /** | |
231 * Returns the index into the address list based on |index|, the index into | |
232 * the select control. Returns -1 if this is not an address index. | |
233 * @private | |
234 */ | |
235 getAddressIndex_: function(index) { | |
236 index -= addressOffset; | |
237 if (index >= 0 && index < this.numAddresses) | |
238 return index; | |
239 | |
240 return -1; | |
241 }, | |
242 | |
243 /** | |
244 * Returns the index into the credit card list based on |index|, the index | |
245 * into the select control. Returns -1 if this is not a credit card index. | |
246 * @private | |
247 */ | |
248 getCreditCardIndex_: function(index) { | |
249 index -= addressOffset + this.numAddresses + creditCardOffset; | |
250 if (index >= 0 && index < this.numCreditCards) | |
251 return index; | |
252 | |
253 return -1; | |
254 }, | |
255 | |
256 /** | |
257 * Returns true if |index| points to a credit card profile. | |
258 * @private | |
259 */ | |
260 profileIndexIsCreditCard_: function(index) { | |
261 index -= addressOffset + this.numAddresses + creditCardOffset; | |
262 return (index >= 0 && index < this.numCreditCards); | |
263 } | |
264 }; | 173 }; |
265 | 174 |
266 AutoFillOptions.updateAddresses = function(addresses) { | 175 AutoFillOptions.setAddressList = function(entries) { |
267 AutoFillOptions.getInstance().updateAddresses_(addresses); | 176 AutoFillOptions.getInstance().setAddressList_(entries); |
| 177 }; |
| 178 |
| 179 AutoFillOptions.setCreditCardList = function(entries) { |
| 180 AutoFillOptions.getInstance().setCreditCardList_(entries); |
| 181 }; |
| 182 |
| 183 AutoFillOptions.removeAutoFillProfile = function(guid) { |
| 184 AutoFillOptions.getInstance().removeAutoFillProfile_(guid); |
| 185 }; |
| 186 |
| 187 AutoFillOptions.loadProfileEditor = function(guid) { |
| 188 AutoFillOptions.getInstance().loadProfileEditor_(guid); |
268 }; | 189 }; |
269 | 190 |
270 AutoFillOptions.editAddress = function(address) { | 191 AutoFillOptions.editAddress = function(address) { |
271 AutoFillOptions.getInstance().showEditAddressOverlay_(address); | 192 AutoFillOptions.getInstance().showEditAddressOverlay_(address); |
272 }; | 193 }; |
273 | 194 |
274 AutoFillOptions.updateCreditCards = function(creditCards) { | |
275 AutoFillOptions.getInstance().updateCreditCards_(creditCards); | |
276 }; | |
277 | |
278 AutoFillOptions.editCreditCard = function(creditCard) { | 195 AutoFillOptions.editCreditCard = function(creditCard) { |
279 AutoFillOptions.getInstance().showEditCreditCardOverlay_(creditCard); | 196 AutoFillOptions.getInstance().showEditCreditCardOverlay_(creditCard); |
280 }; | 197 }; |
281 | 198 |
282 // Export | 199 // Export |
283 return { | 200 return { |
284 AutoFillOptions: AutoFillOptions | 201 AutoFillOptions: AutoFillOptions |
285 }; | 202 }; |
286 | 203 |
287 }); | 204 }); |
288 | 205 |
OLD | NEW |