Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Page = cr.ui.pageManager.Page; | 6 /** @const */ var Page = cr.ui.pageManager.Page; |
| 7 /** @const */ var PageManager = cr.ui.pageManager.PageManager; | 7 /** @const */ var PageManager = cr.ui.pageManager.PageManager; |
| 8 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; | 8 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 * States, then the State field will be restored to its previous value, as | 43 * States, then the State field will be restored to its previous value, as |
| 44 * stored in this object. | 44 * stored in this object. |
| 45 * @type {Object} | 45 * @type {Object} |
| 46 */ | 46 */ |
| 47 savedFieldValues_: {}, | 47 savedFieldValues_: {}, |
| 48 | 48 |
| 49 /** @override */ | 49 /** @override */ |
| 50 initializePage: function() { | 50 initializePage: function() { |
| 51 Page.prototype.initializePage.call(this); | 51 Page.prototype.initializePage.call(this); |
| 52 | 52 |
| 53 this.createMultiValueLists_(); | |
| 54 | |
| 55 var self = this; | 53 var self = this; |
| 56 $('autofill-edit-address-cancel-button').onclick = function(event) { | 54 $('autofill-edit-address-cancel-button').onclick = function(event) { |
| 57 self.dismissOverlay_(); | 55 self.dismissOverlay_(); |
| 58 }; | 56 }; |
| 59 | 57 |
| 60 // TODO(jhawkins): Investigate other possible solutions. | 58 // TODO(jhawkins): Investigate other possible solutions. |
| 61 $('autofill-edit-address-apply-button').onclick = function(event) { | 59 $('autofill-edit-address-apply-button').onclick = function(event) { |
| 62 // Blur active element to ensure that pending changes are committed. | 60 // Blur active element to ensure that pending changes are committed. |
| 63 if (document.activeElement) | 61 if (document.activeElement) |
| 64 document.activeElement.blur(); | 62 document.activeElement.blur(); |
| 65 // Blurring is delayed for list elements. Queue save and close to | 63 self.saveAddress_(); |
| 66 // ensure that pending changes have been applied. | 64 self.dismissOverlay_(); |
| 67 setTimeout(function() { | |
| 68 self.pageDiv.querySelector('[field=phone]').doneValidating().then( | |
| 69 function() { | |
| 70 self.saveAddress_(); | |
| 71 self.dismissOverlay_(); | |
| 72 }); | |
| 73 }, 0); | |
| 74 }; | |
| 75 | |
| 76 // Prevent 'blur' events on the OK and cancel buttons, which can trigger | |
| 77 // insertion of new placeholder elements. The addition of placeholders | |
| 78 // affects layout, which interferes with being able to click on the | |
| 79 // buttons. | |
| 80 $('autofill-edit-address-apply-button').onmousedown = function(event) { | |
| 81 event.preventDefault(); | |
| 82 }; | |
| 83 $('autofill-edit-address-cancel-button').onmousedown = function(event) { | |
| 84 event.preventDefault(); | |
| 85 }; | 65 }; |
| 86 | 66 |
| 87 this.guid_ = ''; | 67 this.guid_ = ''; |
| 88 this.populateCountryList_(); | 68 this.populateCountryList_(); |
| 89 this.rebuildInputFields_( | 69 this.rebuildInputFields_( |
| 90 loadTimeData.getValue('autofillDefaultCountryComponents')); | 70 loadTimeData.getValue('autofillDefaultCountryComponents')); |
| 91 this.languageCode_ = | 71 this.languageCode_ = |
| 92 loadTimeData.getString('autofillDefaultCountryLanguageCode'); | 72 loadTimeData.getString('autofillDefaultCountryLanguageCode'); |
| 93 this.connectInputEvents_(); | 73 this.connectInputEvents_(); |
| 94 this.setInputFields_({}); | 74 this.setInputFields_({}); |
| 95 this.getCountrySwitcher_().onchange = function(event) { | 75 this.getCountrySwitcher_().onchange = function(event) { |
| 96 self.countryChanged_(); | 76 self.countryChanged_(); |
| 97 }; | 77 }; |
| 98 }, | 78 }, |
| 99 | 79 |
| 100 /** | 80 /** |
| 101 * Specifically catch the situations in which the overlay is cancelled | 81 * Specifically catch the situations in which the overlay is cancelled |
| 102 * externally (e.g. by pressing <Esc>), so that the input fields and | 82 * externally (e.g. by pressing <Esc>), so that the input fields and |
| 103 * GUID can be properly cleared. | 83 * GUID can be properly cleared. |
| 104 * @override | 84 * @override |
| 105 */ | 85 */ |
| 106 handleCancel: function() { | 86 handleCancel: function() { |
| 107 this.dismissOverlay_(); | 87 this.dismissOverlay_(); |
| 108 }, | 88 }, |
| 109 | 89 |
| 110 /** | 90 /** |
| 111 * Creates, decorates and initializes the multi-value lists for phone and | |
| 112 * email. | |
| 113 * @private | |
| 114 */ | |
| 115 createMultiValueLists_: function() { | |
| 116 var list = this.pageDiv.querySelector('[field=phone]'); | |
| 117 options.autofillOptions.AutofillPhoneValuesList.decorate(list); | |
| 118 list.autoExpands = true; | |
| 119 | |
| 120 list = this.pageDiv.querySelector('[field=email]'); | |
| 121 options.autofillOptions.AutofillValuesList.decorate(list); | |
| 122 list.autoExpands = true; | |
| 123 }, | |
| 124 | |
| 125 /** | |
| 126 * Updates the data model for the |list| with the values from |entries|. | |
| 127 * @param {cr.ui.List} list The list to update. | |
| 128 * @param {Array} entries The list of items to be added to the list. | |
| 129 * @private | |
| 130 */ | |
| 131 setMultiValueList_: function(list, entries) { | |
| 132 // Add special entry for adding new values. | |
| 133 var augmentedList = entries.slice(); | |
| 134 augmentedList.push(null); | |
| 135 list.dataModel = new ArrayDataModel(augmentedList); | |
| 136 | |
| 137 // Update the status of the 'OK' button. | |
| 138 this.inputFieldChanged_(); | |
| 139 | |
| 140 list.dataModel.addEventListener('splice', | |
| 141 this.inputFieldChanged_.bind(this)); | |
| 142 list.dataModel.addEventListener('change', | |
| 143 this.inputFieldChanged_.bind(this)); | |
| 144 }, | |
| 145 | |
| 146 /** | |
| 147 * Clears any uncommitted input, resets the stored GUID and dismisses the | 91 * Clears any uncommitted input, resets the stored GUID and dismisses the |
| 148 * overlay. | 92 * overlay. |
| 149 * @private | 93 * @private |
| 150 */ | 94 */ |
| 151 dismissOverlay_: function() { | 95 dismissOverlay_: function() { |
| 152 this.setInputFields_({}); | 96 this.setInputFields_({}); |
| 153 this.inputFieldChanged_(); | 97 this.inputFieldChanged_(); |
| 154 this.guid_ = ''; | 98 this.guid_ = ''; |
| 155 this.languageCode_ = ''; | 99 this.languageCode_ = ''; |
| 156 this.savedInputFields_ = {}; | 100 this.savedInputFields_ = {}; |
| 157 PageManager.closeOverlay(); | 101 PageManager.closeOverlay(); |
| 158 }, | 102 }, |
| 159 | 103 |
| 160 /** | 104 /** |
| 161 * @return {Element} The element used to switch countries. | 105 * @return {Element} The element used to switch countries. |
| 162 * @private | 106 * @private |
| 163 */ | 107 */ |
| 164 getCountrySwitcher_: function() { | 108 getCountrySwitcher_: function() { |
| 165 return this.pageDiv.querySelector('[field=country]'); | 109 return this.pageDiv.querySelector('[field=country]'); |
| 166 }, | 110 }, |
| 167 | 111 |
| 168 /** | 112 /** |
| 169 * Returns all list elements. | |
| 170 * @return {!NodeList} The list elements. | |
| 171 * @private | |
| 172 */ | |
| 173 getLists_: function() { | |
| 174 return this.pageDiv.querySelectorAll('list[field]'); | |
| 175 }, | |
| 176 | |
| 177 /** | |
| 178 * Returns all text input elements. | 113 * Returns all text input elements. |
| 179 * @return {!NodeList} The text input elements. | 114 * @return {!NodeList} The text input elements. |
| 180 * @private | 115 * @private |
| 181 */ | 116 */ |
| 182 getTextFields_: function() { | 117 getTextFields_: function() { |
| 183 return this.pageDiv.querySelectorAll('textarea[field], input[field]'); | 118 return this.pageDiv.querySelectorAll('textarea[field], input[field]'); |
| 184 }, | 119 }, |
| 185 | 120 |
| 186 /** | 121 /** |
| 187 * Creates a map from type => value for all text fields. | 122 * Creates a map from type => value for all text fields. |
| 188 * @return {Object} The mapping from field names to values. | 123 * @return {Object} The mapping from field names to values. |
| 189 * @private | 124 * @private |
| 190 */ | 125 */ |
| 191 getInputFields_: function() { | 126 getInputFields_: function() { |
| 192 var address = {country: this.getCountrySwitcher_().value}; | 127 var address = {country: this.getCountrySwitcher_().value}; |
| 193 | 128 |
| 194 var lists = this.getLists_(); | |
| 195 for (var i = 0; i < lists.length; i++) { | |
| 196 address[lists[i].getAttribute('field')] = | |
| 197 lists[i].dataModel.slice(0, lists[i].dataModel.length - 1); | |
| 198 } | |
| 199 | |
| 200 var fields = this.getTextFields_(); | 129 var fields = this.getTextFields_(); |
| 201 for (var i = 0; i < fields.length; i++) { | 130 for (var i = 0; i < fields.length; i++) { |
| 202 address[fields[i].getAttribute('field')] = fields[i].value; | 131 address[fields[i].getAttribute('field')] = fields[i].value; |
| 203 } | 132 } |
| 204 | 133 |
| 205 return address; | 134 return address; |
| 206 }, | 135 }, |
| 207 | 136 |
| 208 /** | 137 /** |
| 209 * Sets the value of each input field according to |address|. | 138 * Sets the value of each input field according to |address|. |
| 210 * @param {Object} address The object with values to use. | 139 * @param {Object} address The object with values to use. |
| 211 * @private | 140 * @private |
| 212 */ | 141 */ |
| 213 setInputFields_: function(address) { | 142 setInputFields_: function(address) { |
| 214 this.getCountrySwitcher_().value = address.country || ''; | 143 this.getCountrySwitcher_().value = address.country || ''; |
| 215 | 144 |
| 216 var lists = this.getLists_(); | |
| 217 for (var i = 0; i < lists.length; i++) { | |
| 218 this.setMultiValueList_( | |
| 219 lists[i], address[lists[i].getAttribute('field')] || []); | |
| 220 } | |
| 221 | |
| 222 var fields = this.getTextFields_(); | 145 var fields = this.getTextFields_(); |
| 223 for (var i = 0; i < fields.length; i++) { | 146 for (var i = 0; i < fields.length; i++) { |
| 224 fields[i].value = address[fields[i].getAttribute('field')] || ''; | 147 fields[i].value = address[fields[i].getAttribute('field')] || ''; |
| 225 } | 148 } |
| 226 }, | 149 }, |
| 227 | 150 |
| 228 /** | 151 /** |
| 229 * Aggregates the values in the input fields into an array and sends the | 152 * Aggregates the values in the input fields into an array and sends the |
| 230 * array to the Autofill handler. | 153 * array to the Autofill handler. |
| 231 * @private | 154 * @private |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 } | 193 } |
| 271 }, | 194 }, |
| 272 | 195 |
| 273 /** | 196 /** |
| 274 * Disables the 'Ok' button if all of the fields are empty. | 197 * Disables the 'Ok' button if all of the fields are empty. |
| 275 * @private | 198 * @private |
| 276 */ | 199 */ |
| 277 inputFieldChanged_: function() { | 200 inputFieldChanged_: function() { |
| 278 var disabled = !this.getCountrySwitcher_().value; | 201 var disabled = !this.getCountrySwitcher_().value; |
| 279 if (disabled) { | 202 if (disabled) { |
| 280 // Length of lists are tested for > 1 due to the "add" placeholder item | |
| 281 // in the list. | |
| 282 var lists = this.getLists_(); | |
| 283 for (var i = 0; i < lists.length; i++) { | |
| 284 if (lists[i].items.length > 1) { | |
| 285 disabled = false; | |
| 286 break; | |
| 287 } | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 if (disabled) { | |
| 292 var fields = this.getTextFields_(); | 203 var fields = this.getTextFields_(); |
| 293 for (var i = 0; i < fields.length; i++) { | 204 for (var i = 0; i < fields.length; i++) { |
| 294 if (fields[i].value) { | 205 if (fields[i].value) { |
| 295 disabled = false; | 206 disabled = false; |
| 296 break; | 207 break; |
| 297 } | 208 } |
| 298 } | 209 } |
| 299 } | 210 } |
| 300 | 211 |
| 301 $('autofill-edit-address-apply-button').disabled = disabled; | 212 $('autofill-edit-address-apply-button').disabled = disabled; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 313 this.inputFieldChanged_(); | 224 this.inputFieldChanged_(); |
| 314 }, | 225 }, |
| 315 | 226 |
| 316 /** | 227 /** |
| 317 * Populates the country <select> list. | 228 * Populates the country <select> list. |
| 318 * @private | 229 * @private |
| 319 */ | 230 */ |
| 320 populateCountryList_: function() { | 231 populateCountryList_: function() { |
| 321 var countryList = loadTimeData.getValue('autofillCountrySelectList'); | 232 var countryList = loadTimeData.getValue('autofillCountrySelectList'); |
| 322 | 233 |
| 323 // Add the countries to the country <select> list. | 234 // Add the countries to the country <select>. |
|
please use gerrit instead
2015/06/01 21:20:57
Unnecessary deletion.
| |
| 324 var countrySelect = this.getCountrySwitcher_(); | 235 var countrySelect = this.getCountrySwitcher_(); |
| 325 // Add an empty option. | 236 // Add an empty option. |
| 326 countrySelect.appendChild(new Option('', '')); | 237 countrySelect.appendChild(new Option('', '')); |
| 327 for (var i = 0; i < countryList.length; i++) { | 238 for (var i = 0; i < countryList.length; i++) { |
| 328 var option = new Option(countryList[i].name, | 239 var option = new Option(countryList[i].name, |
| 329 countryList[i].value); | 240 countryList[i].value); |
| 330 option.disabled = countryList[i].value == 'separator'; | 241 option.disabled = countryList[i].value == 'separator'; |
| 331 countrySelect.appendChild(option); | 242 countrySelect.appendChild(option); |
| 332 } | 243 } |
| 333 }, | 244 }, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 381 * Clears address inputs and rebuilds the input fields according to | 292 * Clears address inputs and rebuilds the input fields according to |
| 382 * |components|. | 293 * |components|. |
| 383 * @param {Array<Array<Object>>} components A list of information about | 294 * @param {Array<Array<Object>>} components A list of information about |
| 384 * each input field. | 295 * each input field. |
| 385 * @private | 296 * @private |
| 386 */ | 297 */ |
| 387 rebuildInputFields_: function(components) { | 298 rebuildInputFields_: function(components) { |
| 388 var content = $('autofill-edit-address-fields'); | 299 var content = $('autofill-edit-address-fields'); |
| 389 content.innerHTML = ''; | 300 content.innerHTML = ''; |
| 390 | 301 |
| 391 var customContainerElements = {fullName: 'div'}; | 302 var customInputElements = {addrLines: 'textarea'}; |
| 392 var customInputElements = {fullName: 'list', addrLines: 'textarea'}; | |
| 393 | 303 |
| 394 for (var i in components) { | 304 for (var i in components) { |
| 395 var row = document.createElement('div'); | 305 var row = document.createElement('div'); |
| 396 row.classList.add('input-group', 'settings-row'); | 306 row.classList.add('input-group', 'settings-row'); |
| 397 content.appendChild(row); | 307 content.appendChild(row); |
| 398 | 308 |
| 399 for (var j in components[i]) { | 309 for (var j in components[i]) { |
| 400 if (components[i][j].field == 'country') | 310 if (components[i][j].field == 'country') |
| 401 continue; | 311 continue; |
| 402 | 312 |
| 403 var fieldContainer = document.createElement( | 313 var fieldContainer = document.createElement('label'); |
| 404 customContainerElements[components[i][j].field] || 'label'); | |
| 405 row.appendChild(fieldContainer); | 314 row.appendChild(fieldContainer); |
| 406 | 315 |
| 407 var fieldName = document.createElement('div'); | 316 var fieldName = document.createElement('div'); |
| 408 fieldName.textContent = components[i][j].name; | 317 fieldName.textContent = components[i][j].name; |
| 409 fieldContainer.appendChild(fieldName); | 318 fieldContainer.appendChild(fieldName); |
| 410 | 319 |
| 411 var input = document.createElement( | 320 var input = document.createElement( |
| 412 customInputElements[components[i][j].field] || 'input'); | 321 customInputElements[components[i][j].field] || 'input'); |
| 413 input.setAttribute('field', components[i][j].field); | 322 input.setAttribute('field', components[i][j].field); |
| 414 input.classList.add(components[i][j].length); | 323 input.classList.add(components[i][j].length); |
| 415 input.setAttribute('placeholder', components[i][j].placeholder || ''); | |
| 416 fieldContainer.appendChild(input); | 324 fieldContainer.appendChild(input); |
| 417 | |
| 418 if (input.tagName == 'LIST') { | |
| 419 options.autofillOptions.AutofillValuesList.decorate(input); | |
| 420 input.autoExpands = true; | |
| 421 } | |
| 422 } | 325 } |
| 423 } | 326 } |
| 424 }, | 327 }, |
| 425 }; | 328 }; |
| 426 | 329 |
| 427 AutofillEditAddressOverlay.prepForNewAddress = function() { | 330 AutofillEditAddressOverlay.prepForNewAddress = function() { |
| 428 AutofillEditAddressOverlay.getInstance().prepForNewAddress_(); | 331 AutofillEditAddressOverlay.getInstance().prepForNewAddress_(); |
| 429 }; | 332 }; |
| 430 | 333 |
| 431 AutofillEditAddressOverlay.loadAddress = function(address) { | 334 AutofillEditAddressOverlay.loadAddress = function(address) { |
| 432 AutofillEditAddressOverlay.getInstance().loadAddress_(address); | 335 AutofillEditAddressOverlay.getInstance().loadAddress_(address); |
| 433 }; | 336 }; |
| 434 | 337 |
| 435 AutofillEditAddressOverlay.loadAddressComponents = function(input) { | 338 AutofillEditAddressOverlay.loadAddressComponents = function(input) { |
| 436 AutofillEditAddressOverlay.getInstance().loadAddressComponents_(input); | 339 AutofillEditAddressOverlay.getInstance().loadAddressComponents_(input); |
| 437 }; | 340 }; |
| 438 | 341 |
| 439 AutofillEditAddressOverlay.setTitle = function(title) { | 342 AutofillEditAddressOverlay.setTitle = function(title) { |
| 440 $('autofill-address-title').textContent = title; | 343 $('autofill-address-title').textContent = title; |
| 441 }; | 344 }; |
| 442 | 345 |
| 443 AutofillEditAddressOverlay.setValidatedPhoneNumbers = function(numbers) { | |
| 444 var instance = AutofillEditAddressOverlay.getInstance(); | |
| 445 var phoneList = instance.pageDiv.querySelector('[field=phone]'); | |
| 446 instance.setMultiValueList_(assertInstanceof(phoneList, cr.ui.List), | |
| 447 numbers); | |
| 448 phoneList.didReceiveValidationResult(); | |
| 449 }; | |
| 450 | |
| 451 // Export | 346 // Export |
| 452 return { | 347 return { |
| 453 AutofillEditAddressOverlay: AutofillEditAddressOverlay | 348 AutofillEditAddressOverlay: AutofillEditAddressOverlay |
| 454 }; | 349 }; |
| 455 }); | 350 }); |
| OLD | NEW |