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

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

Powered by Google App Engine
This is Rietveld 408576698