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

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: Address comments. 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 ';
Evan Stade 2014/04/28 23:23:16 not useful. Use this.querySelector in most places.
please use gerrit instead 2014/04/30 02:06:15 Done.
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(
168 OverlaySelector + ':-webkit-any(textarea, input)[field]');
169 },
170
171 /**
172 * Aggregates the values in the input fields into an object.
173 * @return {object} The mapping from field names to values.
174 * @private
175 */
176 getInputFields_: function() {
177 var address = {};
178 address['country'] = this.getCountrySelector_().value;
179
180 var lists = this.getLists_();
181 for (var i = 0; i < lists.length; i++) {
182 address[lists[i].getAttribute('field')] =
183 lists[i].dataModel.slice(0, lists[i].dataModel.length - 1);
184 }
185
186 var fields = this.getTextFields_();
187 for (var i = 0; i < fields.length; i++)
188 address[fields[i].getAttribute('field')] = fields[i].value;
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 /**
138 * Aggregates the values in the input fields into an array and sends the 213 * Aggregates the values in the input fields into an array and sends the
139 * array to the Autofill handler. 214 * array to the Autofill handler.
140 * @private 215 * @private
141 */ 216 */
142 saveAddress_: function() { 217 saveAddress_: function() {
218 var inputFields = this.getInputFields_();
143 var address = new Array(); 219 var address = new Array();
144 address[0] = this.guid; 220 address[0] = this.guid;
145 var list = $('full-name-list'); 221 address[1] = inputFields['fullName'] || [];
146 address[1] = list.dataModel.slice(0, list.dataModel.length - 1); 222 address[2] = inputFields['companyName'] || '';
147 address[2] = $('company-name').value; 223 address[3] = inputFields['addrLines'] || '';
148 address[3] = $('addr-line-1').value; 224 address[4] = inputFields['dependentLocality'] || '';
149 address[4] = $('addr-line-2').value; 225 address[5] = inputFields['city'] || '';
150 address[5] = $('city').value; 226 address[6] = inputFields['state'] || '';
151 address[6] = $('state').value; 227 address[7] = inputFields['postalCode'] || '';
152 address[7] = $('postal-code').value; 228 address[8] = inputFields['sortingCode'] || '';
153 address[8] = $('country').value; 229 address[9] = inputFields['country'] || '';
154 list = $('phone-list'); 230 address[10] = inputFields['phone'] || [];
155 address[9] = list.dataModel.slice(0, list.dataModel.length - 1); 231 address[11] = inputFields['email'] || [];
156 list = $('email-list'); 232 address[12] = this.languageCode;
157 address[10] = list.dataModel.slice(0, list.dataModel.length - 1);
158 233
159 chrome.send('setAddress', address); 234 chrome.send('setAddress', address);
160 }, 235 },
161 236
162 /** 237 /**
163 * Connects each input field to the inputFieldChanged_() method that enables 238 * 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 239 * or disables the 'Ok' button based on whether all the fields are empty or
165 * not. 240 * not.
166 * @private 241 * @private
167 */ 242 */
168 connectInputEvents_: function() { 243 connectInputEvents_: function() {
169 var self = this; 244 var self = this;
170 $('company-name').oninput = $('addr-line-1').oninput = 245 var fields = this.getTextFields_();
171 $('addr-line-2').oninput = $('city').oninput = $('state').oninput = 246 for (var i = 0; i < fields.length; i++)
Evan Stade 2014/04/28 23:23:16 loops should always have curlies
please use gerrit instead 2014/04/30 02:06:15 Done.
172 $('postal-code').oninput = function(event) { 247 fields[i].oninput = function(event) { self.inputFieldChanged_(); };
173 self.inputFieldChanged_();
174 };
175
176 $('country').onchange = function(event) {
177 self.countryChanged_();
178 };
179 }, 248 },
180 249
181 /** 250 /**
182 * Checks the values of each of the input fields and disables the 'Ok' 251 * Disables the 'Ok' button if all of the fields are empty.
183 * button if all of the fields are empty.
184 * @private 252 * @private
185 */ 253 */
186 inputFieldChanged_: function() { 254 inputFieldChanged_: function() {
187 // Length of lists are tested for <= 1 due to the "add" placeholder item 255 var disabled = true;
188 // in the list. 256 if (this.getCountrySelector_().value)
189 var disabled = 257 disabled = false;
190 $('full-name-list').items.length <= 1 && 258
191 !$('company-name').value && 259 if (disabled) {
192 !$('addr-line-1').value && !$('addr-line-2').value && 260 // Length of lists are tested for > 1 due to the "add" placeholder item
193 !$('city').value && !$('state').value && !$('postal-code').value && 261 // in the list.
194 !$('country').value && $('phone-list').items.length <= 1 && 262 var lists = this.getLists_();
195 $('email-list').items.length <= 1; 263 for (var i = 0; i < lists.length; i++) {
264 if (lists[i].items.length > 1) {
265 disabled = false;
266 break;
267 }
268 }
269 }
270
271 if (disabled) {
272 var fields = this.getTextFields_();
273 for (var i = 0; i < fields.length; i++) {
274 if (fields[i].value) {
275 disabled = false;
276 break;
277 }
278 }
279 }
280
196 $('autofill-edit-address-apply-button').disabled = disabled; 281 $('autofill-edit-address-apply-button').disabled = disabled;
197 }, 282 },
198 283
199 /** 284 /**
200 * Updates the postal code and state field labels appropriately for the 285 * Updates the address fields appropriately for the selected country.
201 * selected country.
202 * @private 286 * @private
203 */ 287 */
204 countryChanged_: function() { 288 countryChanged_: function() {
205 var countryCode = $('country').value || 289 var countryCode = this.getCountrySelector_().value;
206 loadTimeData.getString('defaultCountryCode'); 290 if (countryCode)
207 291 chrome.send('loadAddressEditorComponents', [countryCode]);
208 var details = loadTimeData.getValue('autofillCountryData')[countryCode]; 292 else
209 var postal = $('postal-code-label'); 293 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 }, 294 },
216 295
217 /** 296 /**
218 * Populates the country <select> list. 297 * Populates the country <select> list.
219 * @private 298 * @private
220 */ 299 */
221 populateCountryList_: function() { 300 populateCountryList_: function() {
222 var countryList = loadTimeData.getValue('autofillCountrySelectList'); 301 var countryList = loadTimeData.getValue('autofillCountrySelectList');
223 302
224 // Add the countries to the country <select> list. 303 // Add the countries to the country <select> list.
225 var countrySelect = $('country'); 304 var countrySelect = this.getCountrySelector_();
226 // Add an empty option. 305 // Add an empty option.
227 countrySelect.appendChild(new Option('', '')); 306 countrySelect.appendChild(new Option('', ''));
228 for (var i = 0; i < countryList.length; i++) { 307 for (var i = 0; i < countryList.length; i++) {
229 var option = new Option(countryList[i].name, 308 var option = new Option(countryList[i].name,
230 countryList[i].value); 309 countryList[i].value);
231 option.disabled = countryList[i].value == 'separator'; 310 option.disabled = countryList[i].value == 'separator';
232 countrySelect.appendChild(option); 311 countrySelect.appendChild(option);
233 } 312 }
234 }, 313 },
235 314
236 /** 315 /**
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 316 * Loads the address data from |address|, sets the input fields based on
257 * this data and stores the GUID of the address. 317 * this data, and stores the GUID and language code of the address.
258 * @private 318 * @private
259 */ 319 */
260 loadAddress_: function(address) { 320 loadAddress_: function(address) {
321 this.rebuildInputFields_(address.components);
261 this.setInputFields_(address); 322 this.setInputFields_(address);
262 this.inputFieldChanged_(); 323 this.inputFieldChanged_();
324 this.connectInputEvents_();
263 this.guid = address.guid; 325 this.guid = address.guid;
326 this.languageCode = address.languageCode;
264 }, 327 },
265 328
266 /** 329 /**
267 * Sets the value of each input field according to |address| 330 * Takes a snapshot of the input values, clears the input values, loads the
331 * address input layout from |input.components|, restores the input values
332 * from snapshot, and stores the |input.languageCode| for the address.
268 * @private 333 * @private
269 */ 334 */
270 setInputFields_: function(address) { 335 loadAddressComponents_: function(input) {
271 this.setMultiValueList_('full-name-list', address.fullName); 336 var address = this.getInputFields_();
272 $('company-name').value = address.companyName; 337 this.rebuildInputFields_(input.components);
273 $('addr-line-1').value = address.addrLine1; 338 this.setInputFields_(address);
274 $('addr-line-2').value = address.addrLine2; 339 this.inputFieldChanged_();
275 $('city').value = address.city; 340 this.connectInputEvents_();
276 $('state').value = address.state; 341 this.languageCode = input.languageCode;
277 $('postal-code').value = address.postalCode; 342 },
278 $('country').value = address.country;
279 this.setMultiValueList_('phone-list', address.phone);
280 this.setMultiValueList_('email-list', address.email);
281 343
282 this.countryChanged_(); 344 /**
345 * Clears address inputs and rebuilds the input fields according to
346 * |components|.
347 * @private
348 */
349 rebuildInputFields_: function(components) {
350 var content = $('autofill-edit-address-fields');
351 while (content.firstChild)
352 content.removeChild(content.firstChild);
353
354 var customFieldElements = {'fullName': 'div'};
Evan Stade 2014/04/28 23:23:16 what's wrong with making this a label?
please use gerrit instead 2014/04/30 02:06:15 This how chrome://settings/editoAutofillAddress wo
355 var customInputElements = {'fullName': 'list', 'addrLines': 'textarea'};
356
357 for (var i in components) {
358 var row = document.createElement('div');
359 row.classList.add('input-group', 'settings-row');
360 content.appendChild(row);
361
362 for (var j in components[i]) {
363 if (components[i][j].field == 'country')
364 continue;
365
366 var field = document.createElement(
367 customFieldElements[components[i][j].field] || 'label');
368 row.appendChild(field);
369
370 var label = document.createElement('div');
Evan Stade 2014/04/28 23:23:16 it's confusing that field is a label while label i
please use gerrit instead 2014/04/30 02:06:15 Renamed field to fieldContainer. Renamed label to
371 label.textContent = components[i][j].name;
372 field.appendChild(label);
373
374 var input = document.createElement(
375 customInputElements[components[i][j].field] || 'input');
376 input.setAttribute('field', components[i][j].field);
377 input.classList.add(components[i][j].length);
378 input.setAttribute('placeholder', components[i][j].placeholder || '');
379 field.appendChild(input);
380
381 if (input.tagName == 'LIST') {
Evan Stade 2014/04/28 23:23:16 This seems vaguely better: if (input instanceOf H
please use gerrit instead 2014/04/30 02:06:15 HTMLListElement does not exist. (HTMLUListELement
Evan Stade 2014/04/30 05:25:22 input instanceof cr.ui.List
please use gerrit instead 2014/04/30 17:23:55 "input instanceof cr.ui.List" works only after the
382 options.autofillOptions.AutofillValuesList.decorate(input);
383 input.autoExpands = true;
384 }
385 }
386 }
283 }, 387 },
284 }; 388 };
285 389
286 AutofillEditAddressOverlay.loadAddress = function(address) { 390 AutofillEditAddressOverlay.loadAddress = function(address) {
287 AutofillEditAddressOverlay.getInstance().loadAddress_(address); 391 AutofillEditAddressOverlay.getInstance().loadAddress_(address);
288 }; 392 };
289 393
394 AutofillEditAddressOverlay.loadAddressComponents = function(input) {
395 AutofillEditAddressOverlay.getInstance().loadAddressComponents_(input);
396 };
397
290 AutofillEditAddressOverlay.setTitle = function(title) { 398 AutofillEditAddressOverlay.setTitle = function(title) {
291 $('autofill-address-title').textContent = title; 399 $('autofill-address-title').textContent = title;
292 }; 400 };
293 401
294 AutofillEditAddressOverlay.setValidatedPhoneNumbers = function(numbers) { 402 AutofillEditAddressOverlay.setValidatedPhoneNumbers = function(numbers) {
295 AutofillEditAddressOverlay.getInstance().setMultiValueList_('phone-list', 403 var phoneList = document.querySelector(OverlaySelector + '[field=phone]');
404 AutofillEditAddressOverlay.getInstance().setMultiValueList_(phoneList,
296 numbers); 405 numbers);
297 $('phone-list').didReceiveValidationResult(); 406 phoneList.didReceiveValidationResult();
298 }; 407 };
299 408
300 // Export 409 // Export
301 return { 410 return {
302 AutofillEditAddressOverlay: AutofillEditAddressOverlay 411 AutofillEditAddressOverlay: AutofillEditAddressOverlay
303 }; 412 };
304 }); 413 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698