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
3 // found in the LICENSE file.
5 cr.define('options', function() {
6 /** @const */ var OptionsPage = options.OptionsPage;
9 * AutofillEditCreditCardOverlay class
10 * Encapsulated handling of the 'Add Page' overlay page.
13 function AutofillEditCreditCardOverlay() {
14 OptionsPage.call(this, 'autofillEditCreditCard',
15 loadTimeData.getString('autofillEditCreditCardTitle'),
16 'autofill-edit-credit-card-overlay');
19 cr.addSingletonGetter(AutofillEditCreditCardOverlay);
21 AutofillEditCreditCardOverlay.prototype = {
22 __proto__: OptionsPage.prototype,
25 * Initializes the page.
27 initializePage: function() {
28 OptionsPage.prototype.initializePage.call(this);
31 $('autofill-edit-credit-card-cancel-button').onclick = function(event) {
32 self.dismissOverlay_();
34 $('autofill-edit-credit-card-apply-button').onclick = function(event) {
35 self.saveCreditCard_();
36 self.dismissOverlay_();
40 self.hasEditedNumber_ = false;
41 self.clearInputFields_();
42 self.connectInputEvents_();
43 self.setDefaultSelectOptions_();
47 * Clears any uncommitted input, and dismisses the overlay.
50 dismissOverlay_: function() {
51 this.clearInputFields_();
53 this.hasEditedNumber_ = false;
54 OptionsPage.closeOverlay();
58 * Aggregates the values in the input fields into an array and sends the
59 * array to the Autofill handler.
62 saveCreditCard_: function() {
63 var creditCard = new Array(5);
64 creditCard[0] = this.guid_;
65 creditCard[1] = $('name-on-card').value;
66 creditCard[2] = $('credit-card-number').value;
67 creditCard[3] = $('expiration-month').value;
68 creditCard[4] = $('expiration-year').value;
69 chrome.send('setCreditCard', creditCard);
73 * Connects each input field to the inputFieldChanged_() method that enables
74 * or disables the 'Ok' button based on whether all the fields are empty or
78 connectInputEvents_: function() {
79 var ccNumber = $('credit-card-number');
80 $('name-on-card').oninput = ccNumber.oninput =
81 $('expiration-month').onchange = $('expiration-year').onchange =
82 this.inputFieldChanged_.bind(this);
86 * Checks the values of each of the input fields and disables the 'Ok'
87 * button if all of the fields are empty.
88 * @param {Event} opt_event Optional data for the 'input' event.
91 inputFieldChanged_: function(opt_event) {
92 var disabled = !$('name-on-card').value && !$('credit-card-number').value;
93 $('autofill-edit-credit-card-apply-button').disabled = disabled;
97 * Sets the default values of the options in the 'Expiration date' select
101 setDefaultSelectOptions_: function() {
102 // Set the 'Expiration month' default options.
103 var expirationMonth = $('expiration-month');
104 expirationMonth.options.length = 0;
105 for (var i = 1; i <= 12; ++i) {
112 var option = document.createElement('option');
115 expirationMonth.add(option, null);
118 // Set the 'Expiration year' default options.
119 var expirationYear = $('expiration-year');
120 expirationYear.options.length = 0;
122 var date = new Date();
123 var year = parseInt(date.getFullYear());
124 for (var i = 0; i < 10; ++i) {
126 var option = document.createElement('option');
129 expirationYear.add(option, null);
134 * Clears the value of each input field.
137 clearInputFields_: function() {
138 $('name-on-card').value = '';
139 $('credit-card-number').value = '';
140 $('expiration-month').selectedIndex = 0;
141 $('expiration-year').selectedIndex = 0;
143 // Reset the enabled status of the 'Ok' button.
144 this.inputFieldChanged_();
148 * Sets the value of each input field according to |creditCard|
151 setInputFields_: function(creditCard) {
152 $('name-on-card').value = creditCard.nameOnCard;
153 $('credit-card-number').value = creditCard.creditCardNumber;
155 // The options for the year select control may be out-dated at this point,
156 // e.g. the user opened the options page before midnight on New Year's Eve
157 // and then loaded a credit card profile to edit in the new year, so
158 // reload the select options just to be safe.
159 this.setDefaultSelectOptions_();
161 var idx = parseInt(creditCard.expirationMonth, 10);
162 $('expiration-month').selectedIndex = idx - 1;
164 expYear = creditCard.expirationYear;
165 var date = new Date();
166 var year = parseInt(date.getFullYear());
167 for (var i = 0; i < 10; ++i) {
169 if (expYear == String(text))
170 $('expiration-year').selectedIndex = i;
175 * Loads the credit card data from |creditCard|, sets the input fields based
176 * on this data and stores the GUID of the credit card.
179 loadCreditCard_: function(creditCard) {
180 this.setInputFields_(creditCard);
181 this.inputFieldChanged_();
182 this.guid_ = creditCard.guid;
186 AutofillEditCreditCardOverlay.clearInputFields = function(title) {
187 AutofillEditCreditCardOverlay.getInstance().clearInputFields_();
190 AutofillEditCreditCardOverlay.loadCreditCard = function(creditCard) {
191 AutofillEditCreditCardOverlay.getInstance().loadCreditCard_(creditCard);
194 AutofillEditCreditCardOverlay.setTitle = function(title) {
195 $('autofill-credit-card-title').textContent = title;
200 AutofillEditCreditCardOverlay: AutofillEditCreditCardOverlay