| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 var OptionsPage = options.OptionsPage; | 6 var OptionsPage = options.OptionsPage; |
| 7 | 7 |
| 8 // The offset of the first profile in either the address list or the credit | 8 // The offset of the first profile in either the address list or the credit |
| 9 // card list. Consists of the header and the horizontal rule. | 9 // card list. Consists of the header and the horizontal rule. |
| 10 const profileOffset = 2; | 10 const addressOffset = 2; |
| 11 const creditCardOffset = 3; |
| 11 | 12 |
| 12 ///////////////////////////////////////////////////////////////////////////// | 13 ///////////////////////////////////////////////////////////////////////////// |
| 13 // AutoFillOptions class: | 14 // AutoFillOptions class: |
| 14 // | 15 // |
| 15 // TODO(jhawkins): Replace <select> with a DOMUI List. | 16 // TODO(jhawkins): Replace <select> with a DOMUI List. |
| 16 | 17 |
| 17 /** | 18 /** |
| 18 * Encapsulated handling of AutoFill options page. | 19 * Encapsulated handling of AutoFill options page. |
| 19 * @constructor | 20 * @constructor |
| 20 */ | 21 */ |
| 21 function AutoFillOptions() { | 22 function AutoFillOptions() { |
| 22 this.numAddresses = 0; | 23 this.numAddresses = 0; |
| 23 this.numCreditCards = 0; | 24 this.numCreditCards = 0; |
| 24 this.activeNavTab = null; | 25 this.activeNavTab = null; |
| 26 this.addressIDs = null; |
| 27 this.creditCardIDs = null; |
| 25 OptionsPage.call(this, 'autoFillOptions', | 28 OptionsPage.call(this, 'autoFillOptions', |
| 26 templateData.autoFillOptionsTitle, | 29 templateData.autoFillOptionsTitle, |
| 27 'autoFillOptionsPage'); | 30 'autoFillOptionsPage'); |
| 28 } | 31 } |
| 29 | 32 |
| 30 cr.addSingletonGetter(AutoFillOptions); | 33 cr.addSingletonGetter(AutoFillOptions); |
| 31 | 34 |
| 32 AutoFillOptions.prototype = { | 35 AutoFillOptions.prototype = { |
| 33 __proto__: OptionsPage.prototype, | 36 __proto__: OptionsPage.prototype, |
| 34 | 37 |
| 35 initializePage: function() { | 38 initializePage: function() { |
| 36 OptionsPage.prototype.initializePage.call(this); | 39 OptionsPage.prototype.initializePage.call(this); |
| 37 | 40 |
| 38 var self = this; | 41 var self = this; |
| 39 $('profileList').onchange = function(event) { | 42 $('profileList').onchange = function(event) { |
| 40 self.updateButtonState_(); | 43 self.updateButtonState_(); |
| 41 }; | 44 }; |
| 42 $('addAddressButton').onclick = function(event) { | 45 $('addAddressButton').onclick = function(event) { |
| 43 self.showAddAddressOverlay_(); | 46 self.showAddAddressOverlay_(); |
| 44 }; | 47 }; |
| 45 $('addCreditCardButton').onclick = function(event) { | 48 $('addCreditCardButton').onclick = function(event) { |
| 46 self.showAddCreditCardOverlay_(); | 49 self.showAddCreditCardOverlay_(); |
| 47 }; | 50 }; |
| 51 $('autoFillRemoveButton').onclick = function(event) { |
| 52 self.removeProfile_(); |
| 53 }; |
| 48 | 54 |
| 49 Preferences.getInstance().addEventListener('autofill.enabled', | 55 Preferences.getInstance().addEventListener('autofill.enabled', |
| 50 cr.bind(self.updateEnabledState_, self)); | 56 cr.bind(self.updateEnabledState_, self)); |
| 51 }, | 57 }, |
| 52 | 58 |
| 53 /** | 59 /** |
| 54 * Sets the enabled state of the button controls based on the current state | 60 * Sets the enabled state of the button controls based on the current state |
| 55 * of the |autoFillEnabled| checkbox. | 61 * of the |autoFillEnabled| checkbox. |
| 56 * @private | 62 * @private |
| 57 */ | 63 */ |
| 58 updateEnabledState_: function() { | 64 updateEnabledState_: function() { |
| 59 var checkbox = $('autoFillEnabled'); | 65 var disabled = !$('autoFillEnabled').checked; |
| 60 $('addAddressButton').disabled = $('addCreditCardButton').disabled = | 66 $('addAddressButton').disabled = disabled; |
| 61 $('editButton').disabled = $('autoFillRemoveButton').disabled = | 67 $('addCreditCardButton').disabled = disabled; |
| 62 !checkbox.checked; | 68 $('autoFillEditButton').disabled = disabled; |
| 69 $('autoFillRemoveButton').disabled = disabled; |
| 63 }, | 70 }, |
| 64 | 71 |
| 65 /** | 72 /** |
| 66 * Shows the 'Add address' overlay, specifically by loading the | 73 * Shows the 'Add address' overlay, specifically by loading the |
| 67 * 'Edit address' overlay, emptying the input fields and modifying the | 74 * 'Edit address' overlay, emptying the input fields and modifying the |
| 68 * overlay title. | 75 * overlay title. |
| 69 * @private | 76 * @private |
| 70 */ | 77 */ |
| 71 showAddAddressOverlay_: function() { | 78 showAddAddressOverlay_: function() { |
| 72 var title = localStrings.getString('addAddressTitle'); | 79 var title = localStrings.getString('addAddressTitle'); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 89 }, | 96 }, |
| 90 | 97 |
| 91 /** | 98 /** |
| 92 * Resets the address list. This method leaves the header and horizontal | 99 * Resets the address list. This method leaves the header and horizontal |
| 93 * rule unchanged. | 100 * rule unchanged. |
| 94 * @private | 101 * @private |
| 95 */ | 102 */ |
| 96 resetAddresses_: function() { | 103 resetAddresses_: function() { |
| 97 var profiles = $('profileList'); | 104 var profiles = $('profileList'); |
| 98 for (var i = 0; i < this.numAddresses; ++i) | 105 for (var i = 0; i < this.numAddresses; ++i) |
| 99 profiles.remove(profileOffset); | 106 profiles.remove(addressOffset); |
| 107 this.numAddresses = 0; |
| 100 }, | 108 }, |
| 101 | 109 |
| 102 /** | 110 /** |
| 103 * Resets the credit card list. This method leaves the header and horizontal | 111 * Resets the credit card list. This method leaves the header and horizontal |
| 104 * rule unchanged. | 112 * rule unchanged. |
| 105 * @private | 113 * @private |
| 106 */ | 114 */ |
| 107 resetCreditCards_: function() { | 115 resetCreditCards_: function() { |
| 108 var profiles = $('profileList'); | 116 var profiles = $('profileList'); |
| 109 var offset = this.numAddresses + profileOffset; | 117 var offset = this.numAddresses + addressOffset + creditCardOffset; |
| 110 for (var i = 0; i < this.numCreditCards; ++i) | 118 for (var i = 0; i < this.numCreditCards; ++i) |
| 111 profiles.remove(offset); | 119 profiles.remove(offset); |
| 120 this.numCreditCards = 0; |
| 112 }, | 121 }, |
| 113 | 122 |
| 114 /** | 123 /** |
| 115 * Updates the address list with the given entries. | 124 * Updates the address list with the given entries. |
| 116 * @private | 125 * @private |
| 117 * @param {Array} address List of addresses. | 126 * @param {Array} address List of addresses. |
| 118 */ | 127 */ |
| 119 updateAddresses_: function(addresses) { | 128 updateAddresses_: function(addresses) { |
| 120 this.resetAddresses_(); | 129 this.resetAddresses_(); |
| 121 profileList = $('profileList'); | 130 var profileList = $('profileList'); |
| 122 var blankAddress = | 131 var blankAddress = profileList.options[addressOffset]; |
| 123 profileList.options[profileOffset + this.numAddresses]; | |
| 124 this.numAddresses = addresses.length; | 132 this.numAddresses = addresses.length; |
| 133 this.addressIDs = new Array(this.numAddresses); |
| 125 for (var i = 0; i < this.numAddresses; i++) { | 134 for (var i = 0; i < this.numAddresses; i++) { |
| 126 var address = addresses[i]; | 135 var address = addresses[i]; |
| 127 var option = new Option(address['label']); | 136 var option = new Option(address['label']); |
| 137 this.addressIDs[i] = address['unique_id']; |
| 128 profileList.add(option, blankAddress); | 138 profileList.add(option, blankAddress); |
| 129 } | 139 } |
| 130 | 140 |
| 131 this.updateButtonState_(); | 141 this.updateButtonState_(); |
| 132 }, | 142 }, |
| 133 | 143 |
| 134 /** | 144 /** |
| 135 * Updates the credit card list with the given entries. | 145 * Updates the credit card list with the given entries. |
| 136 * @private | 146 * @private |
| 137 * @param {Array} creditCards List of credit cards. | 147 * @param {Array} creditCards List of credit cards. |
| 138 */ | 148 */ |
| 139 updateCreditCards_: function(creditCards) { | 149 updateCreditCards_: function(creditCards) { |
| 140 this.resetCreditCards_(); | 150 this.resetCreditCards_(); |
| 141 profileList = $('profileList'); | 151 var profileList = $('profileList'); |
| 142 this.numCreditCards = creditCards.length; | 152 this.numCreditCards = creditCards.length; |
| 153 this.creditCardIDs = new Array(this.numCreditCards); |
| 143 for (var i = 0; i < this.numCreditCards; i++) { | 154 for (var i = 0; i < this.numCreditCards; i++) { |
| 144 var creditCard = creditCards[i]; | 155 var creditCard = creditCards[i]; |
| 145 var option = new Option(creditCard['label']); | 156 var option = new Option(creditCard['label']); |
| 157 this.creditCardIDs[i] = creditCard['unique_id']; |
| 146 profileList.add(option, null); | 158 profileList.add(option, null); |
| 147 } | 159 } |
| 148 | 160 |
| 149 this.updateButtonState_(); | 161 this.updateButtonState_(); |
| 150 }, | 162 }, |
| 151 | 163 |
| 152 /** | 164 /** |
| 153 * Sets the enabled state of the AutoFill Edit and Remove buttons based on | 165 * Sets the enabled state of the AutoFill Edit and Remove buttons based on |
| 154 * the current selection in the profile list. | 166 * the current selection in the profile list. |
| 155 * @private | 167 * @private |
| 156 */ | 168 */ |
| 157 updateButtonState_: function() { | 169 updateButtonState_: function() { |
| 158 $('autoFillRemoveButton').disabled = $('autoFillEditButton').disabled = | 170 $('autoFillRemoveButton').disabled = $('autoFillEditButton').disabled = |
| 159 ($('profileList').selectedIndex == -1); | 171 ($('profileList').selectedIndex == -1); |
| 160 }, | 172 }, |
| 173 |
| 174 /** |
| 175 * Removes the currently selected profile, whether it's an address or a |
| 176 * credit card. |
| 177 * @private |
| 178 */ |
| 179 removeProfile_: function() { |
| 180 var idx = $('profileList').selectedIndex; |
| 181 if ((profileIndex = this.getAddressIndex_(idx)) != -1) |
| 182 chrome.send('removeAddress', [String(this.addressIDs[profileIndex])]); |
| 183 else if ((profileIndex = this.getCreditCardIndex_(idx)) != -1) |
| 184 chrome.send('removeCreditCard', |
| 185 [String(this.creditCardIDs[profileIndex])]); |
| 186 }, |
| 187 |
| 188 /** |
| 189 * Returns the index into the address list based on |index|, the index into |
| 190 * the select control. Returns -1 if this is not an address index. |
| 191 * @private |
| 192 */ |
| 193 getAddressIndex_: function(index) { |
| 194 index -= addressOffset; |
| 195 if (index >= 0 && index < this.numAddresses) |
| 196 return index; |
| 197 |
| 198 return -1; |
| 199 }, |
| 200 |
| 201 /** |
| 202 * Returns the index into the credit card list based on |index|, the index |
| 203 * into the select control. Returns -1 if this is not a credit card index. |
| 204 * @private |
| 205 */ |
| 206 getCreditCardIndex_: function(index) { |
| 207 index -= addressOffset + this.numAddresses + creditCardOffset; |
| 208 if (index >= 0 && index < this.numCreditCards) |
| 209 return index; |
| 210 |
| 211 return -1; |
| 212 }, |
| 213 |
| 214 /** |
| 215 * Returns true if |index| points to a credit card profile. |
| 216 * @private |
| 217 */ |
| 218 profileIndexIsCreditCard_: function(index) { |
| 219 index -= addressOffset + this.numAddresses + creditCardOffset; |
| 220 return (index >= 0 && index < this.numCreditCards); |
| 221 } |
| 161 }; | 222 }; |
| 162 | 223 |
| 163 AutoFillOptions.updateAddresses = function(addresses) { | 224 AutoFillOptions.updateAddresses = function(addresses) { |
| 164 AutoFillOptions.getInstance().updateAddresses_(addresses); | 225 AutoFillOptions.getInstance().updateAddresses_(addresses); |
| 165 }; | 226 }; |
| 166 | 227 |
| 167 AutoFillOptions.updateCreditCards = function(creditCards) { | 228 AutoFillOptions.updateCreditCards = function(creditCards) { |
| 168 AutoFillOptions.getInstance().updateCreditCards_(creditCards); | 229 AutoFillOptions.getInstance().updateCreditCards_(creditCards); |
| 169 }; | 230 }; |
| 170 | 231 |
| 171 // Export | 232 // Export |
| 172 return { | 233 return { |
| 173 AutoFillOptions: AutoFillOptions | 234 AutoFillOptions: AutoFillOptions |
| 174 }; | 235 }; |
| 175 | 236 |
| 176 }); | 237 }); |
| 177 | 238 |
| OLD | NEW |