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.clearInputFields_();
41 self.connectInputEvents_();
42 self.setDefaultSelectOptions_();
46 * Specifically catch the situations in which the overlay is cancelled
47 * externally (e.g. by pressing <Esc>), so that the input fields and
48 * GUID can be properly cleared.
51 handleCancel: function() {
52 this.dismissOverlay_();
56 * Clears any uncommitted input, and dismisses the overlay.
59 dismissOverlay_: function() {
60 this.clearInputFields_();
62 OptionsPage.closeOverlay();
66 * Aggregates the values in the input fields into an array and sends the
67 * array to the Autofill handler.
70 saveCreditCard_: function() {
71 var creditCard = new Array(5);
72 creditCard[0] = this.guid_;
73 creditCard[1] = $('name-on-card').value;
74 creditCard[2] = $('credit-card-number').value;
75 creditCard[3] = $('expiration-month').value;
76 creditCard[4] = $('expiration-year').value;
77 chrome.send('setCreditCard', creditCard);
81 * Connects each input field to the inputFieldChanged_() method that enables
82 * or disables the 'Ok' button based on whether all the fields are empty or
86 connectInputEvents_: function() {
87 var ccNumber = $('credit-card-number');
88 $('name-on-card').oninput = ccNumber.oninput =
89 $('expiration-month').onchange = $('expiration-year').onchange =
90 this.inputFieldChanged_.bind(this);
94 * Checks the values of each of the input fields and disables the 'Ok'
95 * button if all of the fields are empty.
96 * @param {Event} opt_event Optional data for the 'input' event.
99 inputFieldChanged_: function(opt_event) {
100 var disabled = !$('name-on-card').value && !$('credit-card-number').value;
101 $('autofill-edit-credit-card-apply-button').disabled = disabled;
105 * Sets the default values of the options in the 'Expiration date' select
109 setDefaultSelectOptions_: function() {
110 // Set the 'Expiration month' default options.
111 var expirationMonth = $('expiration-month');
112 expirationMonth.options.length = 0;
113 for (var i = 1; i <= 12; ++i) {
120 var option = document.createElement('option');
123 expirationMonth.add(option, null);
126 // Set the 'Expiration year' default options.
127 var expirationYear = $('expiration-year');
128 expirationYear.options.length = 0;
130 var date = new Date();
131 var year = parseInt(date.getFullYear());
132 for (var i = 0; i < 10; ++i) {
134 var option = document.createElement('option');
137 expirationYear.add(option, null);
142 * Clears the value of each input field.
145 clearInputFields_: function() {
146 $('name-on-card').value = '';
147 $('credit-card-number').value = '';
148 $('expiration-month').selectedIndex = 0;
149 $('expiration-year').selectedIndex = 0;
151 // Reset the enabled status of the 'Ok' button.
152 this.inputFieldChanged_();
156 * Sets the value of each input field according to |creditCard|
159 setInputFields_: function(creditCard) {
160 $('name-on-card').value = creditCard.nameOnCard;
161 $('credit-card-number').value = creditCard.creditCardNumber;
163 // The options for the year select control may be out-dated at this point,
164 // e.g. the user opened the options page before midnight on New Year's Eve
165 // and then loaded a credit card profile to edit in the new year, so
166 // reload the select options just to be safe.
167 this.setDefaultSelectOptions_();
169 var idx = parseInt(creditCard.expirationMonth, 10);
170 $('expiration-month').selectedIndex = idx - 1;
172 expYear = creditCard.expirationYear;
173 var date = new Date();
174 var year = parseInt(date.getFullYear());
175 for (var i = 0; i < 10; ++i) {
177 if (expYear == String(text))
178 $('expiration-year').selectedIndex = i;
183 * Loads the credit card data from |creditCard|, sets the input fields based
184 * on this data and stores the GUID of the credit card.
187 loadCreditCard_: function(creditCard) {
188 this.setInputFields_(creditCard);
189 this.inputFieldChanged_();
190 this.guid_ = creditCard.guid;
194 AutofillEditCreditCardOverlay.loadCreditCard = function(creditCard) {
195 AutofillEditCreditCardOverlay.getInstance().loadCreditCard_(creditCard);
198 AutofillEditCreditCardOverlay.setTitle = function(title) {
199 $('autofill-credit-card-title').textContent = title;
204 AutofillEditCreditCardOverlay: AutofillEditCreditCardOverlay