Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/browser/resources/options/autofill_edit_address_overlay.js

Issue 243013004: i18n address editing in chrome://settings/autofillEditAddress. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Templates. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 /** @const */ var OptionsPage = options.OptionsPage; 6 /** @const */ var OptionsPage = options.OptionsPage;
7 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; 7 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel;
8 8
9 /**
10 * The top-level selector for all input fields.
11 * @const
12 */
13 var OverlaySelector =
14 '#autofill-edit-address-overlay :not([style*="display: none"]) > ';
15
9 // The GUID of the loaded address. 16 // The GUID of the loaded address.
10 var guid; 17 var guid;
11 18
19 // The BCP 47 language code for the layout of input fields.
20 var languageCode;
21
12 /** 22 /**
13 * AutofillEditAddressOverlay class 23 * AutofillEditAddressOverlay class
14 * Encapsulated handling of the 'Add Page' overlay page. 24 * Encapsulated handling of the 'Add Page' overlay page.
15 * @class 25 * @class
16 */ 26 */
17 function AutofillEditAddressOverlay() { 27 function AutofillEditAddressOverlay() {
18 OptionsPage.call(this, 'autofillEditAddress', 28 OptionsPage.call(this, 'autofillEditAddress',
19 loadTimeData.getString('autofillEditAddressTitle'), 29 loadTimeData.getString('autofillEditAddressTitle'),
20 'autofill-edit-address-overlay'); 30 'autofill-edit-address-overlay');
21 } 31 }
(...skipping 17 matching lines...) Expand all
39 }; 49 };
40 50
41 // TODO(jhawkins): Investigate other possible solutions. 51 // TODO(jhawkins): Investigate other possible solutions.
42 $('autofill-edit-address-apply-button').onclick = function(event) { 52 $('autofill-edit-address-apply-button').onclick = function(event) {
43 // Blur active element to ensure that pending changes are committed. 53 // Blur active element to ensure that pending changes are committed.
44 if (document.activeElement) 54 if (document.activeElement)
45 document.activeElement.blur(); 55 document.activeElement.blur();
46 // Blurring is delayed for list elements. Queue save and close to 56 // Blurring is delayed for list elements. Queue save and close to
47 // ensure that pending changes have been applied. 57 // ensure that pending changes have been applied.
48 setTimeout(function() { 58 setTimeout(function() {
49 $('phone-list').doneValidating().then(function() { 59 document.querySelector(OverlaySelector + '[field=phone]')
50 self.saveAddress_(); 60 .doneValidating().then(function() {
51 self.dismissOverlay_(); 61 self.saveAddress_();
52 }); 62 self.dismissOverlay_();
63 });
53 }, 0); 64 }, 0);
54 }; 65 };
55 66
56 // Prevent 'blur' events on the OK and cancel buttons, which can trigger 67 // Prevent 'blur' events on the OK and cancel buttons, which can trigger
57 // insertion of new placeholder elements. The addition of placeholders 68 // insertion of new placeholder elements. The addition of placeholders
58 // affects layout, which interferes with being able to click on the 69 // affects layout, which interferes with being able to click on the
59 // buttons. 70 // buttons.
60 $('autofill-edit-address-apply-button').onmousedown = function(event) { 71 $('autofill-edit-address-apply-button').onmousedown = function(event) {
61 event.preventDefault(); 72 event.preventDefault();
62 }; 73 };
63 $('autofill-edit-address-cancel-button').onmousedown = function(event) { 74 $('autofill-edit-address-cancel-button').onmousedown = function(event) {
64 event.preventDefault(); 75 event.preventDefault();
65 }; 76 };
66 77
67 self.guid = ''; 78 self.guid = '';
68 self.populateCountryList_(); 79 self.populateCountryList_();
69 self.clearInputFields_(); 80 self.rebuildInputFields_(
81 loadTimeData.getValue('autofillDefaultCountryComponents'));
82 self.languageCode =
83 loadTimeData.getString('autofillDefaultCountryLanguageCode');
84 self.setInputFields_({});
70 self.connectInputEvents_(); 85 self.connectInputEvents_();
71 }, 86 },
72 87
73 /** 88 /**
74 * Specifically catch the situations in which the overlay is cancelled 89 * Specifically catch the situations in which the overlay is cancelled
75 * externally (e.g. by pressing <Esc>), so that the input fields and 90 * externally (e.g. by pressing <Esc>), so that the input fields and
76 * GUID can be properly cleared. 91 * GUID can be properly cleared.
77 * @override 92 * @override
78 */ 93 */
79 handleCancel: function() { 94 handleCancel: function() {
80 this.dismissOverlay_(); 95 this.dismissOverlay_();
81 }, 96 },
82 97
83 /** 98 /**
84 * Creates, decorates and initializes the multi-value lists for full name, 99 * Creates, decorates and initializes the multi-value lists for phone and
85 * phone, and email. 100 * email.
86 * @private 101 * @private
87 */ 102 */
88 createMultiValueLists_: function() { 103 createMultiValueLists_: function() {
89 var list = $('full-name-list'); 104 var list = document.querySelector(OverlaySelector + '[field=phone]');
90 options.autofillOptions.AutofillNameValuesList.decorate(list);
91 list.autoExpands = true;
92
93 list = $('phone-list');
94 options.autofillOptions.AutofillPhoneValuesList.decorate(list); 105 options.autofillOptions.AutofillPhoneValuesList.decorate(list);
95 list.autoExpands = true; 106 list.autoExpands = true;
96 107
97 list = $('email-list'); 108 list = document.querySelector(OverlaySelector + '[field=email]');
98 options.autofillOptions.AutofillValuesList.decorate(list); 109 options.autofillOptions.AutofillValuesList.decorate(list);
99 list.autoExpands = true; 110 list.autoExpands = true;
100 }, 111 },
101 112
102 /** 113 /**
103 * Updates the data model for the list named |listName| with the values from 114 * Updates the data model for the |list| with the values from |entries|.
104 * |entries|. 115 * @param {element} list The list to update.
105 * @param {string} listName The id of the list.
106 * @param {Array} entries The list of items to be added to the list. 116 * @param {Array} entries The list of items to be added to the list.
117 * @private
107 */ 118 */
108 setMultiValueList_: function(listName, entries) { 119 setMultiValueList_: function(list, entries) {
109 // Add data entries.
110 var list = $(listName);
111
112 // Add special entry for adding new values. 120 // Add special entry for adding new values.
113 var augmentedList = entries.slice(); 121 var augmentedList = entries.slice();
114 augmentedList.push(null); 122 augmentedList.push(null);
115 list.dataModel = new ArrayDataModel(augmentedList); 123 list.dataModel = new ArrayDataModel(augmentedList);
116 124
117 // Update the status of the 'OK' button. 125 // Update the status of the 'OK' button.
118 this.inputFieldChanged_(); 126 this.inputFieldChanged_();
119 127
120 list.dataModel.addEventListener('splice', 128 list.dataModel.addEventListener('splice',
121 this.inputFieldChanged_.bind(this)); 129 this.inputFieldChanged_.bind(this));
122 list.dataModel.addEventListener('change', 130 list.dataModel.addEventListener('change',
123 this.inputFieldChanged_.bind(this)); 131 this.inputFieldChanged_.bind(this));
124 }, 132 },
125 133
126 /** 134 /**
127 * Clears any uncommitted input, resets the stored GUID and dismisses the 135 * Clears any uncommitted input, resets the stored GUID and dismisses the
128 * overlay. 136 * overlay.
129 * @private 137 * @private
130 */ 138 */
131 dismissOverlay_: function() { 139 dismissOverlay_: function() {
132 this.clearInputFields_(); 140 this.setInputFields_({});
133 this.guid = ''; 141 this.guid = '';
142 this.languageCode = '';
134 OptionsPage.closeOverlay(); 143 OptionsPage.closeOverlay();
135 }, 144 },
136 145
137 /** 146 /**
147 * Returns the country selector element.
148 * @return {element} The country selector.
149 * @private
150 */
151 getCountrySelector_: function() {
152 return document.querySelector(OverlaySelector + '[field=country]');
153 },
154
155 /**
156 * Returns all list elements.
157 * @return {NodeList} The list elements.
158 * @private
159 */
160 getLists_: function() {
161 return document.querySelectorAll(OverlaySelector + 'list[field]');
162 },
163
164 /**
165 * Returns all text input elements.
166 * @return {NodeList} The text input elements.
167 * @private
168 */
169 getTextFields_: function() {
170 return document.querySelectorAll(OverlaySelector + 'input[field]');
171 },
172
173 /**
174 * Aggregates the values in the input fields into an object.
175 * @return {object} The mapping from field names to values.
176 * @private
177 */
178 getInputFields_: function() {
179 var address = {};
180 address['country'] = this.getCountrySelector_().value;
181
182 var lists = this.getLists_();
183 for (var i = 0; i < lists.length; i++) {
184 address[lists[i].getAttribute('field')] =
185 lists[i].dataModel.slice(0, lists[i].dataModel.length - 1);
186 }
187
188 var fields = this.getTextFields_();
189 for (var i = 0; i < fields.length; i++)
190 address[fields[i].getAttribute('field')] = fields[i].value;
191
192 return address;
193 },
194
195 /**
196 * Sets the value of each input field according to |address|.
197 * @param {object} address The object with values to use.
198 * @private
199 */
200 setInputFields_: function(address) {
201 this.getCountrySelector_().value = address['country'] || '';
202
203 var lists = this.getLists_();
204 for (var i = 0; i < lists.length; i++) {
205 this.setMultiValueList_(
206 lists[i], address[lists[i].getAttribute('field')] || []);
207 }
208
209 var fields = this.getTextFields_();
210 for (var i = 0; i < fields.length; i++)
211 fields[i].value = address[fields[i].getAttribute('field')] || '';
212 },
213
214 /**
138 * Aggregates the values in the input fields into an array and sends the 215 * Aggregates the values in the input fields into an array and sends the
139 * array to the Autofill handler. 216 * array to the Autofill handler.
140 * @private 217 * @private
141 */ 218 */
142 saveAddress_: function() { 219 saveAddress_: function() {
220 var inputFields = this.getInputFields_();
143 var address = new Array(); 221 var address = new Array();
144 address[0] = this.guid; 222 address[0] = this.guid;
145 var list = $('full-name-list'); 223 address[1] = inputFields['fullName'] || [];
146 address[1] = list.dataModel.slice(0, list.dataModel.length - 1); 224 address[2] = inputFields['companyName'] || '';
147 address[2] = $('company-name').value; 225 address[3] = inputFields['addrLines'] || [];
148 address[3] = $('addr-line-1').value; 226 address[4] = inputFields['dependentLocality'] || '';
149 address[4] = $('addr-line-2').value; 227 address[5] = inputFields['city'] || '';
150 address[5] = $('city').value; 228 address[6] = inputFields['state'] || '';
151 address[6] = $('state').value; 229 address[7] = inputFields['postalCode'] || '';
152 address[7] = $('postal-code').value; 230 address[8] = inputFields['sortingCode'] || '';
153 address[8] = $('country').value; 231 address[9] = inputFields['country'] || '';
154 list = $('phone-list'); 232 address[10] = inputFields['phone'] || [];
155 address[9] = list.dataModel.slice(0, list.dataModel.length - 1); 233 address[11] = inputFields['email'] || [];
156 list = $('email-list'); 234 address[12] = this.languageCode;
157 address[10] = list.dataModel.slice(0, list.dataModel.length - 1);
158 235
159 chrome.send('setAddress', address); 236 chrome.send('setAddress', address);
160 }, 237 },
161 238
162 /** 239 /**
163 * Connects each input field to the inputFieldChanged_() method that enables 240 * Connects each input field to the inputFieldChanged_() method that enables
164 * or disables the 'Ok' button based on whether all the fields are empty or 241 * or disables the 'Ok' button based on whether all the fields are empty or
165 * not. 242 * not.
166 * @private 243 * @private
167 */ 244 */
168 connectInputEvents_: function() { 245 connectInputEvents_: function() {
169 var self = this; 246 var self = this;
170 $('company-name').oninput = $('addr-line-1').oninput =
171 $('addr-line-2').oninput = $('city').oninput = $('state').oninput =
172 $('postal-code').oninput = function(event) {
173 self.inputFieldChanged_();
174 };
175 247
176 $('country').onchange = function(event) { 248 var fields = this.getTextFields_();
177 self.countryChanged_(); 249 for (var i = 0; i < fields.length; i++)
178 }; 250 fields[i].oninput = function(event) { self.inputFieldChanged_(); };
251
252 this.getCountrySelector_().onchange =
253 function(event) { self.countryChanged_(); };
179 }, 254 },
180 255
181 /** 256 /**
182 * Checks the values of each of the input fields and disables the 'Ok' 257 * Disables the 'Ok' button if all of the fields are empty.
183 * button if all of the fields are empty.
184 * @private 258 * @private
185 */ 259 */
186 inputFieldChanged_: function() { 260 inputFieldChanged_: function() {
187 // Length of lists are tested for <= 1 due to the "add" placeholder item 261 var disabled = true;
188 // in the list. 262 if (this.getCountrySelector_().value)
189 var disabled = 263 disabled = false;
190 $('full-name-list').items.length <= 1 && 264
191 !$('company-name').value && 265 if (disabled) {
192 !$('addr-line-1').value && !$('addr-line-2').value && 266 // Length of lists are tested for > 1 due to the "add" placeholder item
193 !$('city').value && !$('state').value && !$('postal-code').value && 267 // in the list.
194 !$('country').value && $('phone-list').items.length <= 1 && 268 var lists = this.getLists_();
195 $('email-list').items.length <= 1; 269 for (var i = 0; i < lists.length; i++) {
270 if (lists[i].items.length > 1) {
271 disabled = false;
272 break;
273 }
274 }
275 }
276
277 if (disabled) {
278 var fields = this.getTextFields_();
279 for (var i = 0; i < fields.length; i++) {
280 if (fields[i].value) {
281 disabled = false;
282 break;
283 }
284 }
285 }
286
196 $('autofill-edit-address-apply-button').disabled = disabled; 287 $('autofill-edit-address-apply-button').disabled = disabled;
197 }, 288 },
198 289
199 /** 290 /**
200 * Updates the postal code and state field labels appropriately for the 291 * Updates the address fields appropriately for the selected country.
201 * selected country.
202 * @private 292 * @private
203 */ 293 */
204 countryChanged_: function() { 294 countryChanged_: function() {
205 var countryCode = $('country').value || 295 var countryCode = this.getCountrySelector_().value;
206 loadTimeData.getString('defaultCountryCode'); 296 if (countryCode)
207 297 chrome.send('loadAddressEditorComponents', [countryCode]);
208 var details = loadTimeData.getValue('autofillCountryData')[countryCode]; 298 else
209 var postal = $('postal-code-label'); 299 this.inputFieldChanged_();
210 postal.textContent = details.postalCodeLabel;
211 $('state-label').textContent = details.stateLabel;
212
213 // Also update the 'Ok' button as needed.
214 this.inputFieldChanged_();
215 }, 300 },
216 301
217 /** 302 /**
218 * Populates the country <select> list. 303 * Populates the country <select> list.
219 * @private 304 * @private
220 */ 305 */
221 populateCountryList_: function() { 306 populateCountryList_: function() {
222 var countryList = loadTimeData.getValue('autofillCountrySelectList'); 307 var countryList = loadTimeData.getValue('autofillCountrySelectList');
223 308
224 // Add the countries to the country <select> list. 309 // Add the countries to the country <select> list.
225 var countrySelect = $('country'); 310 var countrySelect = this.getCountrySelector_();
226 // Add an empty option. 311 // Add an empty option.
227 countrySelect.appendChild(new Option('', '')); 312 countrySelect.appendChild(new Option('', ''));
228 for (var i = 0; i < countryList.length; i++) { 313 for (var i = 0; i < countryList.length; i++) {
229 var option = new Option(countryList[i].name, 314 var option = new Option(countryList[i].name,
230 countryList[i].value); 315 countryList[i].value);
231 option.disabled = countryList[i].value == 'separator'; 316 option.disabled = countryList[i].value == 'separator';
232 countrySelect.appendChild(option); 317 countrySelect.appendChild(option);
233 } 318 }
234 }, 319 },
235 320
236 /** 321 /**
237 * Clears the value of each input field.
238 * @private
239 */
240 clearInputFields_: function() {
241 this.setMultiValueList_('full-name-list', []);
242 $('company-name').value = '';
243 $('addr-line-1').value = '';
244 $('addr-line-2').value = '';
245 $('city').value = '';
246 $('state').value = '';
247 $('postal-code').value = '';
248 $('country').value = '';
249 this.setMultiValueList_('phone-list', []);
250 this.setMultiValueList_('email-list', []);
251
252 this.countryChanged_();
253 },
254
255 /**
256 * Loads the address data from |address|, sets the input fields based on 322 * Loads the address data from |address|, sets the input fields based on
257 * this data and stores the GUID of the address. 323 * this data, and stores the GUID and language code of the address.
258 * @private 324 * @private
259 */ 325 */
260 loadAddress_: function(address) { 326 loadAddress_: function(address) {
327 this.rebuildInputFields_(address.components);
261 this.setInputFields_(address); 328 this.setInputFields_(address);
262 this.inputFieldChanged_(); 329 this.inputFieldChanged_();
330 this.connectInputEvents_();
263 this.guid = address.guid; 331 this.guid = address.guid;
332 this.languageCode = address.languageCode;
264 }, 333 },
265 334
266 /** 335 /**
267 * Sets the value of each input field according to |address| 336 * Takes a snapshot of the input values, clears the input values, loads the
337 * address input layout from |input.components|, restores the input values
338 * from snapshot, and stores the |input.languageCode| for the address.
268 * @private 339 * @private
269 */ 340 */
270 setInputFields_: function(address) { 341 loadAddressComponents_: function(input) {
271 this.setMultiValueList_('full-name-list', address.fullName); 342 var address = this.getInputFields_();
272 $('company-name').value = address.companyName; 343 this.rebuildInputFields_(input.components);
273 $('addr-line-1').value = address.addrLine1; 344 this.setInputFields_(address);
274 $('addr-line-2').value = address.addrLine2; 345 this.inputFieldChanged_();
275 $('city').value = address.city; 346 this.connectInputEvents_();
276 $('state').value = address.state; 347 this.languageCode = input.languageCode;
277 $('postal-code').value = address.postalCode; 348 },
278 $('country').value = address.country;
279 this.setMultiValueList_('phone-list', address.phone);
280 this.setMultiValueList_('email-list', address.email);
281 349
282 this.countryChanged_(); 350 /**
351 * Clears address inputs and rebuilds the input fields according to
352 * |components|.
353 * @private
354 */
355 rebuildInputFields_: function(components) {
356 jstProcess(new JsEvalContext(components),
357 $('autofill-edit-address-overlay'));
358
359 var list = document.querySelector(OverlaySelector + '[field=fullName]');
360 if (list) {
361 options.autofillOptions.AutofillNameValuesList.decorate(list);
362 list.autoExpands = true;
363 }
364
365 list = document.querySelector(OverlaySelector + '[field=addrLines]');
366 if (list) {
367 options.autofillOptions.AutofillValuesList.decorate(list);
368 list.autoExpands = true;
369 }
283 }, 370 },
284 }; 371 };
285 372
286 AutofillEditAddressOverlay.loadAddress = function(address) { 373 AutofillEditAddressOverlay.loadAddress = function(address) {
287 AutofillEditAddressOverlay.getInstance().loadAddress_(address); 374 AutofillEditAddressOverlay.getInstance().loadAddress_(address);
288 }; 375 };
289 376
377 AutofillEditAddressOverlay.loadAddressComponents = function(input) {
378 AutofillEditAddressOverlay.getInstance().loadAddressComponents_(input);
379 };
380
290 AutofillEditAddressOverlay.setTitle = function(title) { 381 AutofillEditAddressOverlay.setTitle = function(title) {
291 $('autofill-address-title').textContent = title; 382 $('autofill-address-title').textContent = title;
292 }; 383 };
293 384
294 AutofillEditAddressOverlay.setValidatedPhoneNumbers = function(numbers) { 385 AutofillEditAddressOverlay.setValidatedPhoneNumbers = function(numbers) {
295 AutofillEditAddressOverlay.getInstance().setMultiValueList_('phone-list', 386 var phoneList = document.querySelector(OverlaySelector + '[field=phone]');
387 AutofillEditAddressOverlay.getInstance().setMultiValueList_(phoneList,
296 numbers); 388 numbers);
297 $('phone-list').didReceiveValidationResult(); 389 phoneList.didReceiveValidationResult();
298 }; 390 };
299 391
300 // Export 392 // Export
301 return { 393 return {
302 AutofillEditAddressOverlay: AutofillEditAddressOverlay 394 AutofillEditAddressOverlay: AutofillEditAddressOverlay
303 }; 395 };
304 }); 396 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698