Index: chrome/browser/resources/options/autofill_edit_address_overlay.js |
diff --git a/chrome/browser/resources/options/autofill_edit_address_overlay.js b/chrome/browser/resources/options/autofill_edit_address_overlay.js |
index 1f1bf5b3c9f1286119a31807a0b1309109f720e4..3b9f851bf4ed9efae6e3180d5c665a06e0d4b533 100644 |
--- a/chrome/browser/resources/options/autofill_edit_address_overlay.js |
+++ b/chrome/browser/resources/options/autofill_edit_address_overlay.js |
@@ -5,10 +5,14 @@ |
cr.define('options', function() { |
/** @const */ var OptionsPage = options.OptionsPage; |
/** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; |
+ /** @const */ var OverlaySelector = '#autofill-edit-address-overlay '; |
// The GUID of the loaded address. |
var guid; |
+ // The BCP 47 language code for the layout of input fields. |
+ var languageCode; |
+ |
/** |
* AutofillEditAddressOverlay class |
* Encapsulated handling of the 'Add Page' overlay page. |
@@ -46,10 +50,11 @@ cr.define('options', function() { |
// Blurring is delayed for list elements. Queue save and close to |
// ensure that pending changes have been applied. |
setTimeout(function() { |
- $('phone-list').doneValidating().then(function() { |
- self.saveAddress_(); |
- self.dismissOverlay_(); |
- }); |
+ document.querySelector(OverlaySelector + '[field=phone]') |
+ .doneValidating().then(function() { |
+ self.saveAddress_(); |
+ self.dismissOverlay_(); |
+ }); |
}, 0); |
}; |
@@ -64,10 +69,17 @@ cr.define('options', function() { |
event.preventDefault(); |
}; |
- self.guid = ''; |
- self.populateCountryList_(); |
- self.clearInputFields_(); |
- self.connectInputEvents_(); |
+ this.guid = ''; |
+ this.populateCountryList_(); |
+ this.rebuildInputFields_( |
+ loadTimeData.getValue('autofillDefaultCountryComponents')); |
+ this.languageCode = |
+ loadTimeData.getString('autofillDefaultCountryLanguageCode'); |
+ this.setInputFields_({}); |
+ this.connectInputEvents_(); |
+ this.getCountrySelector_().onchange = function(event) { |
+ self.countryChanged_(); |
+ }; |
}, |
/** |
@@ -81,34 +93,27 @@ cr.define('options', function() { |
}, |
/** |
- * Creates, decorates and initializes the multi-value lists for full name, |
- * phone, and email. |
+ * Creates, decorates and initializes the multi-value lists for phone and |
+ * email. |
* @private |
*/ |
createMultiValueLists_: function() { |
- var list = $('full-name-list'); |
- options.autofillOptions.AutofillNameValuesList.decorate(list); |
- list.autoExpands = true; |
- |
- list = $('phone-list'); |
+ var list = document.querySelector(OverlaySelector + '[field=phone]'); |
options.autofillOptions.AutofillPhoneValuesList.decorate(list); |
list.autoExpands = true; |
- list = $('email-list'); |
+ list = document.querySelector(OverlaySelector + '[field=email]'); |
options.autofillOptions.AutofillValuesList.decorate(list); |
list.autoExpands = true; |
}, |
/** |
- * Updates the data model for the list named |listName| with the values from |
- * |entries|. |
- * @param {string} listName The id of the list. |
+ * Updates the data model for the |list| with the values from |entries|. |
+ * @param {element} list The list to update. |
* @param {Array} entries The list of items to be added to the list. |
+ * @private |
*/ |
- setMultiValueList_: function(listName, entries) { |
- // Add data entries. |
- var list = $(listName); |
- |
+ setMultiValueList_: function(list, entries) { |
// Add special entry for adding new values. |
var augmentedList = entries.slice(); |
augmentedList.push(null); |
@@ -129,32 +134,101 @@ cr.define('options', function() { |
* @private |
*/ |
dismissOverlay_: function() { |
- this.clearInputFields_(); |
+ this.setInputFields_({}); |
this.guid = ''; |
+ this.languageCode = ''; |
OptionsPage.closeOverlay(); |
}, |
/** |
+ * Returns the country selector element. |
+ * @return {element} The country selector. |
+ * @private |
+ */ |
+ getCountrySelector_: function() { |
+ return document.querySelector(OverlaySelector + '[field=country]'); |
+ }, |
+ |
+ /** |
+ * Returns all list elements. |
+ * @return {NodeList} The list elements. |
+ * @private |
+ */ |
+ getLists_: function() { |
+ return document.querySelectorAll(OverlaySelector + 'list[field]'); |
+ }, |
+ |
+ /** |
+ * Returns all text input elements. |
+ * @return {NodeList} The text input elements. |
+ * @private |
+ */ |
+ getTextFields_: function() { |
+ return document.querySelectorAll(OverlaySelector + 'input[field]'); |
+ }, |
+ |
+ /** |
+ * Aggregates the values in the input fields into an object. |
+ * @return {object} The mapping from field names to values. |
+ * @private |
+ */ |
+ getInputFields_: function() { |
+ var address = {}; |
+ address['country'] = this.getCountrySelector_().value; |
+ |
+ var lists = this.getLists_(); |
+ for (var i = 0; i < lists.length; i++) { |
+ address[lists[i].getAttribute('field')] = |
+ lists[i].dataModel.slice(0, lists[i].dataModel.length - 1); |
+ } |
+ |
+ var fields = this.getTextFields_(); |
+ for (var i = 0; i < fields.length; i++) |
+ address[fields[i].getAttribute('field')] = fields[i].value; |
+ |
+ return address; |
+ }, |
+ |
+ /** |
+ * Sets the value of each input field according to |address|. |
+ * @param {object} address The object with values to use. |
+ * @private |
+ */ |
+ setInputFields_: function(address) { |
+ this.getCountrySelector_().value = address['country'] || ''; |
+ |
+ var lists = this.getLists_(); |
+ for (var i = 0; i < lists.length; i++) { |
+ this.setMultiValueList_( |
+ lists[i], address[lists[i].getAttribute('field')] || []); |
+ } |
+ |
+ var fields = this.getTextFields_(); |
+ for (var i = 0; i < fields.length; i++) |
+ fields[i].value = address[fields[i].getAttribute('field')] || ''; |
+ }, |
+ |
+ /** |
* Aggregates the values in the input fields into an array and sends the |
* array to the Autofill handler. |
* @private |
*/ |
saveAddress_: function() { |
+ var inputFields = this.getInputFields_(); |
var address = new Array(); |
address[0] = this.guid; |
- var list = $('full-name-list'); |
- address[1] = list.dataModel.slice(0, list.dataModel.length - 1); |
- address[2] = $('company-name').value; |
- address[3] = $('addr-line-1').value; |
- address[4] = $('addr-line-2').value; |
- address[5] = $('city').value; |
- address[6] = $('state').value; |
- address[7] = $('postal-code').value; |
- address[8] = $('country').value; |
- list = $('phone-list'); |
- address[9] = list.dataModel.slice(0, list.dataModel.length - 1); |
- list = $('email-list'); |
- address[10] = list.dataModel.slice(0, list.dataModel.length - 1); |
+ address[1] = inputFields['fullName'] || []; |
+ address[2] = inputFields['companyName'] || ''; |
+ address[3] = inputFields['addrLines'] || []; |
+ address[4] = inputFields['dependentLocality'] || ''; |
+ address[5] = inputFields['city'] || ''; |
+ address[6] = inputFields['state'] || ''; |
+ address[7] = inputFields['postalCode'] || ''; |
+ address[8] = inputFields['sortingCode'] || ''; |
+ address[9] = inputFields['country'] || ''; |
+ address[10] = inputFields['phone'] || []; |
+ address[11] = inputFields['email'] || []; |
+ address[12] = this.languageCode; |
chrome.send('setAddress', address); |
}, |
@@ -167,51 +241,55 @@ cr.define('options', function() { |
*/ |
connectInputEvents_: function() { |
var self = this; |
- $('company-name').oninput = $('addr-line-1').oninput = |
- $('addr-line-2').oninput = $('city').oninput = $('state').oninput = |
- $('postal-code').oninput = function(event) { |
- self.inputFieldChanged_(); |
- }; |
- |
- $('country').onchange = function(event) { |
- self.countryChanged_(); |
- }; |
+ var fields = this.getTextFields_(); |
+ for (var i = 0; i < fields.length; i++) |
+ fields[i].oninput = function(event) { self.inputFieldChanged_(); }; |
}, |
/** |
- * Checks the values of each of the input fields and disables the 'Ok' |
- * button if all of the fields are empty. |
+ * Disables the 'Ok' button if all of the fields are empty. |
* @private |
*/ |
inputFieldChanged_: function() { |
- // Length of lists are tested for <= 1 due to the "add" placeholder item |
- // in the list. |
- var disabled = |
- $('full-name-list').items.length <= 1 && |
- !$('company-name').value && |
- !$('addr-line-1').value && !$('addr-line-2').value && |
- !$('city').value && !$('state').value && !$('postal-code').value && |
- !$('country').value && $('phone-list').items.length <= 1 && |
- $('email-list').items.length <= 1; |
+ var disabled = true; |
+ if (this.getCountrySelector_().value) |
+ disabled = false; |
+ |
+ if (disabled) { |
+ // Length of lists are tested for > 1 due to the "add" placeholder item |
+ // in the list. |
+ var lists = this.getLists_(); |
+ for (var i = 0; i < lists.length; i++) { |
+ if (lists[i].items.length > 1) { |
+ disabled = false; |
+ break; |
+ } |
+ } |
+ } |
+ |
+ if (disabled) { |
+ var fields = this.getTextFields_(); |
+ for (var i = 0; i < fields.length; i++) { |
+ if (fields[i].value) { |
+ disabled = false; |
+ break; |
+ } |
+ } |
+ } |
+ |
$('autofill-edit-address-apply-button').disabled = disabled; |
}, |
/** |
- * Updates the postal code and state field labels appropriately for the |
- * selected country. |
+ * Updates the address fields appropriately for the selected country. |
* @private |
*/ |
countryChanged_: function() { |
- var countryCode = $('country').value || |
- loadTimeData.getString('defaultCountryCode'); |
- |
- var details = loadTimeData.getValue('autofillCountryData')[countryCode]; |
- var postal = $('postal-code-label'); |
- postal.textContent = details.postalCodeLabel; |
- $('state-label').textContent = details.stateLabel; |
- |
- // Also update the 'Ok' button as needed. |
- this.inputFieldChanged_(); |
+ var countryCode = this.getCountrySelector_().value; |
+ if (countryCode) |
+ chrome.send('loadAddressEditorComponents', [countryCode]); |
+ else |
+ this.inputFieldChanged_(); |
}, |
/** |
@@ -222,7 +300,7 @@ cr.define('options', function() { |
var countryList = loadTimeData.getValue('autofillCountrySelectList'); |
// Add the countries to the country <select> list. |
- var countrySelect = $('country'); |
+ var countrySelect = this.getCountrySelector_(); |
// Add an empty option. |
countrySelect.appendChild(new Option('', '')); |
for (var i = 0; i < countryList.length; i++) { |
@@ -234,52 +312,89 @@ cr.define('options', function() { |
}, |
/** |
- * Clears the value of each input field. |
+ * Loads the address data from |address|, sets the input fields based on |
+ * this data, and stores the GUID and language code of the address. |
* @private |
*/ |
- clearInputFields_: function() { |
- this.setMultiValueList_('full-name-list', []); |
- $('company-name').value = ''; |
- $('addr-line-1').value = ''; |
- $('addr-line-2').value = ''; |
- $('city').value = ''; |
- $('state').value = ''; |
- $('postal-code').value = ''; |
- $('country').value = ''; |
- this.setMultiValueList_('phone-list', []); |
- this.setMultiValueList_('email-list', []); |
- |
- this.countryChanged_(); |
+ loadAddress_: function(address) { |
+ this.rebuildInputFields_(address.components); |
+ this.setInputFields_(address); |
+ this.inputFieldChanged_(); |
+ this.connectInputEvents_(); |
+ this.guid = address.guid; |
+ this.languageCode = address.languageCode; |
}, |
/** |
- * Loads the address data from |address|, sets the input fields based on |
- * this data and stores the GUID of the address. |
+ * Takes a snapshot of the input values, clears the input values, loads the |
+ * address input layout from |input.components|, restores the input values |
+ * from snapshot, and stores the |input.languageCode| for the address. |
* @private |
*/ |
- loadAddress_: function(address) { |
+ loadAddressComponents_: function(input) { |
+ var address = this.getInputFields_(); |
+ this.rebuildInputFields_(input.components); |
this.setInputFields_(address); |
this.inputFieldChanged_(); |
- this.guid = address.guid; |
+ this.connectInputEvents_(); |
+ this.languageCode = input.languageCode; |
}, |
/** |
- * Sets the value of each input field according to |address| |
+ * Clears address inputs and rebuilds the input fields according to |
+ * |components|. |
* @private |
*/ |
- setInputFields_: function(address) { |
- this.setMultiValueList_('full-name-list', address.fullName); |
- $('company-name').value = address.companyName; |
- $('addr-line-1').value = address.addrLine1; |
- $('addr-line-2').value = address.addrLine2; |
- $('city').value = address.city; |
- $('state').value = address.state; |
- $('postal-code').value = address.postalCode; |
- $('country').value = address.country; |
- this.setMultiValueList_('phone-list', address.phone); |
- this.setMultiValueList_('email-list', address.email); |
- |
- this.countryChanged_(); |
+ rebuildInputFields_: function(components) { |
+ var content = $('autofill-edit-address-fields'); |
+ while (content.firstChild) |
+ content.removeChild(content.firstChild); |
+ |
+ for (var i in components) { |
+ var row = $('autofill-address-row-template').cloneNode(); |
+ row.removeAttribute('id'); |
+ for (var j in components[i]) { |
+ switch (components[i][j].field) { |
+ case 'country': |
+ break; |
+ |
+ case 'fullName': |
+ var field = $('autofill-full-name-list-template').cloneNode(true); |
+ field.removeAttribute('id'); |
+ row.appendChild(field); |
+ break; |
+ |
+ case 'addrLines': |
+ var field = $('autofill-addr-lines-template').cloneNode(true); |
+ field.removeAttribute('id'); |
+ row.appendChild(field); |
+ break; |
+ |
+ default: |
+ var field = $('autofill-field-template').cloneNode(true); |
+ field.removeAttribute('id'); |
+ field.querySelector('div').textContent = components[i][j].name; |
+ var input = field.querySelector('input'); |
+ input.setAttribute('field', components[i][j].field); |
+ 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.
|
+ row.appendChild(field); |
+ break; |
+ } |
+ } |
+ content.appendChild(row); |
+ } |
+ |
+ var list = document.querySelector(OverlaySelector + '[field=fullName]'); |
+ 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.
|
+ options.autofillOptions.AutofillNameValuesList.decorate(list); |
+ list.autoExpands = true; |
+ } |
+ |
+ list = document.querySelector(OverlaySelector + '[field=addrLines]'); |
+ if (list) { |
+ options.autofillOptions.AutofillValuesList.decorate(list); |
+ list.autoExpands = true; |
+ } |
}, |
}; |
@@ -287,14 +402,19 @@ cr.define('options', function() { |
AutofillEditAddressOverlay.getInstance().loadAddress_(address); |
}; |
+ AutofillEditAddressOverlay.loadAddressComponents = function(input) { |
+ AutofillEditAddressOverlay.getInstance().loadAddressComponents_(input); |
+ }; |
+ |
AutofillEditAddressOverlay.setTitle = function(title) { |
$('autofill-address-title').textContent = title; |
}; |
AutofillEditAddressOverlay.setValidatedPhoneNumbers = function(numbers) { |
- AutofillEditAddressOverlay.getInstance().setMultiValueList_('phone-list', |
+ var phoneList = document.querySelector(OverlaySelector + '[field=phone]'); |
+ AutofillEditAddressOverlay.getInstance().setMultiValueList_(phoneList, |
numbers); |
- $('phone-list').didReceiveValidationResult(); |
+ phoneList.didReceiveValidationResult(); |
}; |
// Export |