| 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 const OptionsPage = options.OptionsPage; | 6 const OptionsPage = options.OptionsPage; |
| 7 | 7 |
| 8 // The GUID of the loaded credit card. | 8 // The GUID of the loaded credit card. |
| 9 var guid_; | 9 var guid_; |
| 10 | 10 |
| 11 // The CC number of the profile, used to check for changes to the input field. | 11 // The CC number of the profile, used to check for changes to the input field. |
| 12 var storedCCNumber_; | 12 var storedCCNumber_; |
| 13 | 13 |
| 14 // Set to true if the user has edited the CC number field. When saving the | 14 // Set to true if the user has edited the CC number field. When saving the |
| 15 // CC profile after editing, the stored CC number is saved if the input field | 15 // CC profile after editing, the stored CC number is saved if the input field |
| 16 // has not been modified. | 16 // has not been modified. |
| 17 var hasEditedNumber_; | 17 var hasEditedNumber_; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * AutoFillEditCreditCardOverlay class | 20 * AutoFillEditCreditCardOverlay class |
| 21 * Encapsulated handling of the 'Add Page' overlay page. | 21 * Encapsulated handling of the 'Add Page' overlay page. |
| 22 * @class | 22 * @class |
| 23 */ | 23 */ |
| 24 function AutoFillEditCreditCardOverlay() { | 24 function AutoFillEditCreditCardOverlay() { |
| 25 OptionsPage.call(this, 'autoFillEditCreditCardOverlay', | 25 OptionsPage.call(this, 'autoFillEditCreditCardOverlay', |
| 26 templateData.autoFillEditCreditCardTitle, | 26 templateData.autoFillEditCreditCardTitle, |
| 27 'autoFillEditCreditCardOverlay'); | 27 'autofill-edit-credit-card-overlay'); |
| 28 } | 28 } |
| 29 | 29 |
| 30 cr.addSingletonGetter(AutoFillEditCreditCardOverlay); | 30 cr.addSingletonGetter(AutoFillEditCreditCardOverlay); |
| 31 | 31 |
| 32 AutoFillEditCreditCardOverlay.prototype = { | 32 AutoFillEditCreditCardOverlay.prototype = { |
| 33 __proto__: OptionsPage.prototype, | 33 __proto__: OptionsPage.prototype, |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * Initializes the page. | 36 * Initializes the page. |
| 37 */ | 37 */ |
| 38 initializePage: function() { | 38 initializePage: function() { |
| 39 OptionsPage.prototype.initializePage.call(this); | 39 OptionsPage.prototype.initializePage.call(this); |
| 40 | 40 |
| 41 var self = this; | 41 var self = this; |
| 42 $('autoFillEditCreditCardCancelButton').onclick = function(event) { | 42 $('autofill-edit-credit-card-cancel-button').onclick = function(event) { |
| 43 self.dismissOverlay_(); | 43 self.dismissOverlay_(); |
| 44 } | 44 } |
| 45 $('autoFillEditCreditCardApplyButton').onclick = function(event) { | 45 $('autofill-edit-credit-card-apply-button').onclick = function(event) { |
| 46 self.saveCreditCard_(); | 46 self.saveCreditCard_(); |
| 47 self.dismissOverlay_(); | 47 self.dismissOverlay_(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 self.guid_ = ''; | 50 self.guid_ = ''; |
| 51 self.storedCCNumber_ = ''; | 51 self.storedCCNumber_ = ''; |
| 52 self.hasEditedNumber_ = false; | 52 self.hasEditedNumber_ = false; |
| 53 self.clearInputFields_(); | 53 self.clearInputFields_(); |
| 54 self.connectInputEvents_(); | 54 self.connectInputEvents_(); |
| 55 self.setDefaultSelectOptions_(); | 55 self.setDefaultSelectOptions_(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 68 }, | 68 }, |
| 69 | 69 |
| 70 /** | 70 /** |
| 71 * Aggregates the values in the input fields into an array and sends the | 71 * Aggregates the values in the input fields into an array and sends the |
| 72 * array to the AutoFill handler. | 72 * array to the AutoFill handler. |
| 73 * @private | 73 * @private |
| 74 */ | 74 */ |
| 75 saveCreditCard_: function() { | 75 saveCreditCard_: function() { |
| 76 var creditCard = new Array(5); | 76 var creditCard = new Array(5); |
| 77 creditCard[0] = this.guid_; | 77 creditCard[0] = this.guid_; |
| 78 creditCard[1] = $('nameOnCard').value; | 78 creditCard[1] = $('name-on-card').value; |
| 79 creditCard[3] = $('expirationMonth').value; | 79 creditCard[3] = $('expiration-month').value; |
| 80 creditCard[4] = $('expirationYear').value; | 80 creditCard[4] = $('expiration-year').value; |
| 81 | 81 |
| 82 if (this.hasEditedNumber_) | 82 if (this.hasEditedNumber_) |
| 83 creditCard[2] = $('creditCardNumber').value; | 83 creditCard[2] = $('credit-card-number').value; |
| 84 else | 84 else |
| 85 creditCard[2] = this.storedCCNumber_; | 85 creditCard[2] = this.storedCCNumber_; |
| 86 | 86 |
| 87 chrome.send('setCreditCard', creditCard); | 87 chrome.send('setCreditCard', creditCard); |
| 88 }, | 88 }, |
| 89 | 89 |
| 90 /** | 90 /** |
| 91 * Connects each input field to the inputFieldChanged_() method that enables | 91 * Connects each input field to the inputFieldChanged_() method that enables |
| 92 * or disables the 'Ok' button based on whether all the fields are empty or | 92 * or disables the 'Ok' button based on whether all the fields are empty or |
| 93 * not. | 93 * not. |
| 94 * @private | 94 * @private |
| 95 */ | 95 */ |
| 96 connectInputEvents_: function() { | 96 connectInputEvents_: function() { |
| 97 $('nameOnCard').oninput = $('creditCardNumber').oninput = | 97 $('name-on-card').oninput = $('credit-card-number').oninput = |
| 98 $('expirationMonth').onchange = $('expirationYear').onchange = | 98 $('expiration-month').onchange = $('expiration-year').onchange = |
| 99 this.inputFieldChanged_.bind(this); | 99 this.inputFieldChanged_.bind(this); |
| 100 }, | 100 }, |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * Checks the values of each of the input fields and disables the 'Ok' | 103 * Checks the values of each of the input fields and disables the 'Ok' |
| 104 * button if all of the fields are empty. | 104 * button if all of the fields are empty. |
| 105 * @param {Event} opt_event Optional data for the 'input' event. | 105 * @param {Event} opt_event Optional data for the 'input' event. |
| 106 * @private | 106 * @private |
| 107 */ | 107 */ |
| 108 inputFieldChanged_: function(opt_event) { | 108 inputFieldChanged_: function(opt_event) { |
| 109 var ccNumber = $('creditCardNumber'); | 109 var ccNumber = $('credit-card-number'); |
| 110 var disabled = !$('nameOnCard').value && !ccNumber.value; | 110 var disabled = !$('name-on-card').value && !ccNumber.value; |
| 111 $('autoFillEditCreditCardApplyButton').disabled = disabled; | 111 $('autofill-edit-credit-card-apply-button').disabled = disabled; |
| 112 | 112 |
| 113 if (opt_event && opt_event.target == ccNumber) { | 113 if (opt_event && opt_event.target == ccNumber) { |
| 114 // If the user hasn't edited the text yet, delete it all on edit. | 114 // If the user hasn't edited the text yet, delete it all on edit. |
| 115 if (!this.hasEditedNumber_ && this.storedCCNumber_.length && | 115 if (!this.hasEditedNumber_ && this.storedCCNumber_.length && |
| 116 ccNumber.value != this.storedCCNumber_) { | 116 ccNumber.value != this.storedCCNumber_) { |
| 117 ccNumber.value = ''; | 117 ccNumber.value = ''; |
| 118 } | 118 } |
| 119 | 119 |
| 120 this.hasEditedNumber_ = true; | 120 this.hasEditedNumber_ = true; |
| 121 } | 121 } |
| 122 }, | 122 }, |
| 123 | 123 |
| 124 /** | 124 /** |
| 125 * Sets the default values of the options in the 'Expiration date' select | 125 * Sets the default values of the options in the 'Expiration date' select |
| 126 * controls. | 126 * controls. |
| 127 * @private | 127 * @private |
| 128 */ | 128 */ |
| 129 setDefaultSelectOptions_: function() { | 129 setDefaultSelectOptions_: function() { |
| 130 // Set the 'Expiration month' default options. | 130 // Set the 'Expiration month' default options. |
| 131 var expirationMonth = $('expirationMonth'); | 131 var expirationMonth = $('expiration-month'); |
| 132 expirationMonth.options.length = 0; | 132 expirationMonth.options.length = 0; |
| 133 for (var i = 1; i <= 12; ++i) { | 133 for (var i = 1; i <= 12; ++i) { |
| 134 var text; | 134 var text; |
| 135 if (i < 10) | 135 if (i < 10) |
| 136 text = '0' + i; | 136 text = '0' + i; |
| 137 else | 137 else |
| 138 text = i; | 138 text = i; |
| 139 | 139 |
| 140 var option = document.createElement('option'); | 140 var option = document.createElement('option'); |
| 141 option.text = text; | 141 option.text = text; |
| 142 option.value = text; | 142 option.value = text; |
| 143 expirationMonth.add(option, null); | 143 expirationMonth.add(option, null); |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Set the 'Expiration year' default options. | 146 // Set the 'Expiration year' default options. |
| 147 var expirationYear = $('expirationYear'); | 147 var expirationYear = $('expiration-year'); |
| 148 expirationYear.options.length = 0; | 148 expirationYear.options.length = 0; |
| 149 | 149 |
| 150 var date = new Date(); | 150 var date = new Date(); |
| 151 var year = parseInt(date.getFullYear()); | 151 var year = parseInt(date.getFullYear()); |
| 152 for (var i = 0; i < 10; ++i) { | 152 for (var i = 0; i < 10; ++i) { |
| 153 var text = year + i; | 153 var text = year + i; |
| 154 var option = document.createElement('option'); | 154 var option = document.createElement('option'); |
| 155 option.text = text; | 155 option.text = text; |
| 156 option.value = text; | 156 option.value = text; |
| 157 expirationYear.add(option, null); | 157 expirationYear.add(option, null); |
| 158 } | 158 } |
| 159 }, | 159 }, |
| 160 | 160 |
| 161 /** | 161 /** |
| 162 * Clears the value of each input field. | 162 * Clears the value of each input field. |
| 163 * @private | 163 * @private |
| 164 */ | 164 */ |
| 165 clearInputFields_: function() { | 165 clearInputFields_: function() { |
| 166 $('nameOnCard').value = ''; | 166 $('name-on-card').value = ''; |
| 167 $('creditCardNumber').value = ''; | 167 $('credit-card-number').value = ''; |
| 168 $('expirationMonth').selectedIndex = 0; | 168 $('expiration-month').selectedIndex = 0; |
| 169 $('expirationYear').selectedIndex = 0; | 169 $('expiration-year').selectedIndex = 0; |
| 170 }, | 170 }, |
| 171 | 171 |
| 172 /** | 172 /** |
| 173 * Sets the value of each input field according to |creditCard| | 173 * Sets the value of each input field according to |creditCard| |
| 174 * @private | 174 * @private |
| 175 */ | 175 */ |
| 176 setInputFields_: function(creditCard) { | 176 setInputFields_: function(creditCard) { |
| 177 $('nameOnCard').value = creditCard['nameOnCard']; | 177 $('name-on-card').value = creditCard['nameOnCard']; |
| 178 $('creditCardNumber').value = creditCard['obfuscatedCardNumber']; | 178 $('credit-card-number').value = creditCard['obfuscatedCardNumber']; |
| 179 | 179 |
| 180 // The options for the year select control may be out-dated at this point, | 180 // The options for the year select control may be out-dated at this point, |
| 181 // e.g. the user opened the options page before midnight on New Year's Eve | 181 // e.g. the user opened the options page before midnight on New Year's Eve |
| 182 // and then loaded a credit card profile to edit in the new year, so | 182 // and then loaded a credit card profile to edit in the new year, so |
| 183 // reload the select options just to be safe. | 183 // reload the select options just to be safe. |
| 184 this.setDefaultSelectOptions_(); | 184 this.setDefaultSelectOptions_(); |
| 185 | 185 |
| 186 var idx = parseInt(creditCard['expirationMonth'], 10); | 186 var idx = parseInt(creditCard['expirationMonth'], 10); |
| 187 $('expirationMonth').selectedIndex = idx - 1; | 187 $('expiration-month').selectedIndex = idx - 1; |
| 188 | 188 |
| 189 expYear = creditCard['expirationYear']; | 189 expYear = creditCard['expirationYear']; |
| 190 var date = new Date(); | 190 var date = new Date(); |
| 191 var year = parseInt(date.getFullYear()); | 191 var year = parseInt(date.getFullYear()); |
| 192 for (var i = 0; i < 10; ++i) { | 192 for (var i = 0; i < 10; ++i) { |
| 193 var text = year + i; | 193 var text = year + i; |
| 194 if (expYear == String(text)) | 194 if (expYear == String(text)) |
| 195 $('expirationYear').selectedIndex = i; | 195 $('expiration-year').selectedIndex = i; |
| 196 } | 196 } |
| 197 }, | 197 }, |
| 198 | 198 |
| 199 /** | 199 /** |
| 200 * Loads the credit card data from |creditCard|, sets the input fields based | 200 * Loads the credit card data from |creditCard|, sets the input fields based |
| 201 * on this data and stores the GUID of the credit card. | 201 * on this data and stores the GUID of the credit card. |
| 202 * @private | 202 * @private |
| 203 */ | 203 */ |
| 204 loadCreditCard_: function(creditCard) { | 204 loadCreditCard_: function(creditCard) { |
| 205 this.setInputFields_(creditCard); | 205 this.setInputFields_(creditCard); |
| 206 this.inputFieldChanged_(); | 206 this.inputFieldChanged_(); |
| 207 this.guid_ = creditCard['guid']; | 207 this.guid_ = creditCard['guid']; |
| 208 this.storedCCNumber_ = creditCard['creditCardNumber']; | 208 this.storedCCNumber_ = creditCard['creditCardNumber']; |
| 209 }, | 209 }, |
| 210 }; | 210 }; |
| 211 | 211 |
| 212 AutoFillEditCreditCardOverlay.clearInputFields = function(title) { | 212 AutoFillEditCreditCardOverlay.clearInputFields = function(title) { |
| 213 AutoFillEditCreditCardOverlay.getInstance().clearInputFields_(); | 213 AutoFillEditCreditCardOverlay.getInstance().clearInputFields_(); |
| 214 }; | 214 }; |
| 215 | 215 |
| 216 AutoFillEditCreditCardOverlay.loadCreditCard = function(creditCard) { | 216 AutoFillEditCreditCardOverlay.loadCreditCard = function(creditCard) { |
| 217 AutoFillEditCreditCardOverlay.getInstance().loadCreditCard_(creditCard); | 217 AutoFillEditCreditCardOverlay.getInstance().loadCreditCard_(creditCard); |
| 218 }; | 218 }; |
| 219 | 219 |
| 220 AutoFillEditCreditCardOverlay.setTitle = function(title) { | 220 AutoFillEditCreditCardOverlay.setTitle = function(title) { |
| 221 $('autoFillCreditCardTitle').textContent = title; | 221 $('autofill-credit-card-title').textContent = title; |
| 222 }; | 222 }; |
| 223 | 223 |
| 224 // Export | 224 // Export |
| 225 return { | 225 return { |
| 226 AutoFillEditCreditCardOverlay: AutoFillEditCreditCardOverlay | 226 AutoFillEditCreditCardOverlay: AutoFillEditCreditCardOverlay |
| 227 }; | 227 }; |
| 228 }); | 228 }); |
| OLD | NEW |