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

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

Powered by Google App Engine
This is Rietveld 408576698