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