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 Page = cr.ui.pageManager.Page;
7 /** @const */ var PageManager = cr.ui.pageManager.PageManager;
10 * AutofillEditCreditCardOverlay class
11 * Encapsulated handling of the 'Add Page' overlay page.
14 function AutofillEditCreditCardOverlay() {
15 Page.call(this, 'autofillEditCreditCard',
16 loadTimeData.getString('autofillEditCreditCardTitle'),
17 'autofill-edit-credit-card-overlay');
20 cr.addSingletonGetter(AutofillEditCreditCardOverlay);
22 AutofillEditCreditCardOverlay.prototype = {
23 __proto__: Page.prototype,
26 initializePage: function() {
27 Page.prototype.initializePage.call(this);
30 $('autofill-edit-credit-card-cancel-button').onclick = function(event) {
31 self.dismissOverlay_();
33 $('autofill-edit-credit-card-apply-button').onclick = function(event) {
34 self.saveCreditCard_();
35 self.dismissOverlay_();
39 self.clearInputFields_();
40 self.connectInputEvents_();
41 self.setDefaultSelectOptions_();
45 * Specifically catch the situations in which the overlay is cancelled
46 * externally (e.g. by pressing <Esc>), so that the input fields and
47 * GUID can be properly cleared.
50 handleCancel: function() {
51 this.dismissOverlay_();
55 * Clears any uncommitted input, and dismisses the overlay.
58 dismissOverlay_: function() {
59 this.clearInputFields_();
61 PageManager.closeOverlay();
65 * Aggregates the values in the input fields into an array and sends the
66 * array to the Autofill handler.
69 saveCreditCard_: function() {
70 var creditCard = new Array(5);
71 creditCard[0] = this.guid_;
72 creditCard[1] = $('name-on-card').value;
73 creditCard[2] = $('credit-card-number').value;
74 creditCard[3] = $('expiration-month').value;
75 creditCard[4] = $('expiration-year').value;
76 chrome.send('setCreditCard', creditCard);
80 * Connects each input field to the inputFieldChanged_() method that enables
81 * or disables the 'Ok' button based on whether all the fields are empty or
85 connectInputEvents_: function() {
86 var ccNumber = $('credit-card-number');
87 $('name-on-card').oninput = ccNumber.oninput =
88 $('expiration-month').onchange = $('expiration-year').onchange =
89 this.inputFieldChanged_.bind(this);
93 * Checks the values of each of the input fields and disables the 'Ok'
94 * button if all of the fields are empty.
95 * @param {Event} opt_event Optional data for the 'input' event.
98 inputFieldChanged_: function(opt_event) {
99 var disabled = !$('name-on-card').value && !$('credit-card-number').value;
100 $('autofill-edit-credit-card-apply-button').disabled = disabled;
104 * Sets the default values of the options in the 'Expiration date' select
108 setDefaultSelectOptions_: function() {
109 // Set the 'Expiration month' default options.
110 var expirationMonth = $('expiration-month');
111 expirationMonth.options.length = 0;
112 for (var i = 1; i <= 12; ++i) {
119 var option = document.createElement('option');
122 expirationMonth.add(option, null);
125 // Set the 'Expiration year' default options.
126 var expirationYear = $('expiration-year');
127 expirationYear.options.length = 0;
129 var date = new Date();
130 var year = parseInt(date.getFullYear());
131 for (var i = 0; i < 10; ++i) {
133 var option = document.createElement('option');
136 expirationYear.add(option, null);
141 * Clears the value of each input field.
144 clearInputFields_: function() {
145 $('name-on-card').value = '';
146 $('credit-card-number').value = '';
147 $('expiration-month').selectedIndex = 0;
148 $('expiration-year').selectedIndex = 0;
150 // Reset the enabled status of the 'Ok' button.
151 this.inputFieldChanged_();
155 * Sets the value of each input field according to |creditCard|
158 setInputFields_: function(creditCard) {
159 $('name-on-card').value = creditCard.nameOnCard;
160 $('credit-card-number').value = creditCard.creditCardNumber;
162 // The options for the year select control may be out-dated at this point,
163 // e.g. the user opened the options page before midnight on New Year's Eve
164 // and then loaded a credit card profile to edit in the new year, so
165 // reload the select options just to be safe.
166 this.setDefaultSelectOptions_();
168 var idx = parseInt(creditCard.expirationMonth, 10);
169 $('expiration-month').selectedIndex = idx - 1;
171 expYear = creditCard.expirationYear;
172 var date = new Date();
173 var year = parseInt(date.getFullYear());
174 for (var i = 0; i < 10; ++i) {
176 if (expYear == String(text))
177 $('expiration-year').selectedIndex = i;
182 * Loads the credit card data from |creditCard|, sets the input fields based
183 * on this data and stores the GUID of the credit card.
186 loadCreditCard_: function(creditCard) {
187 this.setInputFields_(creditCard);
188 this.inputFieldChanged_();
189 this.guid_ = creditCard.guid;
193 AutofillEditCreditCardOverlay.loadCreditCard = function(creditCard) {
194 AutofillEditCreditCardOverlay.getInstance().loadCreditCard_(creditCard);
197 AutofillEditCreditCardOverlay.setTitle = function(title) {
198 $('autofill-credit-card-title').textContent = title;
203 AutofillEditCreditCardOverlay: AutofillEditCreditCardOverlay