Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / options / autofill_options.js
blob5a6f691d3bd50eff2232693dc64e742e2d6f45ab
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 /**
6 * @typedef {{
7 * creditCardNumber: string,
8 * expirationMonth: string,
9 * expirationYear: string,
10 * guid: string,
11 * nameOnCard: string
12 * }}
13 * @see chrome/browser/ui/webui/options/autofill_options_handler.cc
15 var CreditCardData;
17 cr.define('options', function() {
18 var Page = cr.ui.pageManager.Page;
19 var PageManager = cr.ui.pageManager.PageManager;
20 var ArrayDataModel = cr.ui.ArrayDataModel;
22 /////////////////////////////////////////////////////////////////////////////
23 // AutofillOptions class:
25 /**
26 * Encapsulated handling of Autofill options page.
27 * @constructor
28 * @extends {cr.ui.pageManager.Page}
30 function AutofillOptions() {
31 Page.call(this, 'autofill',
32 loadTimeData.getString('autofillOptionsPageTabTitle'),
33 'autofill-options');
36 cr.addSingletonGetter(AutofillOptions);
38 AutofillOptions.prototype = {
39 __proto__: Page.prototype,
41 /**
42 * The address list.
43 * @type {options.DeletableItemList}
44 * @private
46 addressList_: null,
48 /**
49 * The credit card list.
50 * @type {options.DeletableItemList}
51 * @private
53 creditCardList_: null,
55 /** @override */
56 initializePage: function() {
57 Page.prototype.initializePage.call(this);
59 this.createAddressList_();
60 this.createCreditCardList_();
62 var self = this;
63 $('autofill-add-address').onclick = function(event) {
64 self.showAddAddressOverlay_();
66 $('autofill-add-creditcard').onclick = function(event) {
67 self.showAddCreditCardOverlay_();
69 $('autofill-options-confirm').onclick = function(event) {
70 PageManager.closeOverlay();
72 // TODO(erikchen): After Address Book integration has been disabled for
73 // 6 weeks, and there are no major problems, rip out all the code. Expected
74 // removal date: 07/15/2015. http://crbug.com/488146.
76 <if expr="is_macosx">
77 $('autofill-use-mac-address-book-checkbox').onchange = function(event) {
78 if (this.checked) {
79 setTimeout(function() {
80 // Prompt the user to give Chrome access to the user's Address
81 // Book, if the user was not previously prompted. The dialog that
82 // appears blocks the Chrome process, so wait for a small period of
83 // time to allow the checkbox to appear filled in.
84 chrome.send('accessAddressBook');
85 }, 10);
88 </if>
91 $('autofill-help').onclick = function(event) {
92 chrome.send('coreOptionsUserMetricsAction',
93 ['Options_AutofillShowAbout']);
94 return true; // Always follow the href
97 this.walletIntegrationAvailableStateChanged_(
98 loadTimeData.getBoolean('autofillWalletIntegrationAvailable'));
100 // TODO(jhawkins): What happens when Autofill is disabled whilst on the
101 // Autofill options page?
105 * Creates, decorates and initializes the address list.
106 * @private
108 createAddressList_: function() {
109 var addressList = $('address-list');
110 options.autofillOptions.AutofillAddressList.decorate(addressList);
111 this.addressList_ = assertInstanceof(addressList,
112 options.DeletableItemList);
113 this.addressList_.autoExpands = true;
117 * Creates, decorates and initializes the credit card list.
118 * @private
120 createCreditCardList_: function() {
121 var creditCardList = $('creditcard-list');
122 options.autofillOptions.AutofillCreditCardList.decorate(creditCardList);
123 this.creditCardList_ = assertInstanceof(creditCardList,
124 options.DeletableItemList);
125 this.creditCardList_.autoExpands = true;
129 * Shows the 'Add address' overlay, specifically by loading the
130 * 'Edit address' overlay and modifying the overlay title.
131 * @private
133 showAddAddressOverlay_: function() {
134 var title = loadTimeData.getString('addAddressTitle');
135 AutofillEditAddressOverlay.setTitle(title);
136 PageManager.showPageByName('autofillEditAddress');
137 AutofillEditAddressOverlay.prepForNewAddress();
141 * Shows the 'Add credit card' overlay, specifically by loading the
142 * 'Edit credit card' overlay and modifying the overlay title.
143 * @private
145 showAddCreditCardOverlay_: function() {
146 var title = loadTimeData.getString('addCreditCardTitle');
147 AutofillEditCreditCardOverlay.setTitle(title);
148 PageManager.showPageByName('autofillEditCreditCard');
149 AutofillEditCreditCardOverlay.prepForNewCard();
153 * Updates the data model for the address list with the values from
154 * |entries|.
155 * @param {!Array} entries The list of addresses.
157 setAddressList_: function(entries) {
158 this.addressList_.dataModel = new ArrayDataModel(entries);
162 * Updates the data model for the credit card list with the values from
163 * |entries|.
164 * @param {!Array} entries The list of credit cards.
166 setCreditCardList_: function(entries) {
167 this.creditCardList_.dataModel = new ArrayDataModel(entries);
171 * Removes the Autofill address or credit card represented by |guid|.
172 * @param {string} guid The GUID of the address to remove.
173 * @param {string=} metricsAction The name of the action to log for metrics.
174 * @private
176 removeData_: function(guid, metricsAction) {
177 chrome.send('removeData', [guid]);
178 if (metricsAction)
179 chrome.send('coreOptionsUserMetricsAction', [metricsAction]);
183 * Shows the 'Edit address' overlay, using the data in |address| to fill the
184 * input fields. |address| is a list with one item, an associative array
185 * that contains the address data.
186 * @private
188 showEditAddressOverlay_: function(address) {
189 var title = loadTimeData.getString('editAddressTitle');
190 AutofillEditAddressOverlay.setTitle(title);
191 AutofillEditAddressOverlay.loadAddress(address);
192 PageManager.showPageByName('autofillEditAddress');
196 * Shows the 'Edit credit card' overlay, using the data in |credit_card| to
197 * fill the input fields. |creditCard| is a list with one item, an
198 * associative array that contains the credit card data.
199 * @param {CreditCardData} creditCard
200 * @private
202 showEditCreditCardOverlay_: function(creditCard) {
203 var title = loadTimeData.getString('editCreditCardTitle');
204 AutofillEditCreditCardOverlay.setTitle(title);
205 AutofillEditCreditCardOverlay.loadCreditCard(creditCard);
206 PageManager.showPageByName('autofillEditCreditCard');
210 * Toggles the visibility of the Wallet integration checkbox.
211 * @param {boolean} available Whether the user has the option of using
212 * Wallet data.
213 * @private
215 walletIntegrationAvailableStateChanged_: function(available) {
216 $('autofill-wallet-setting-area').hidden = !available;
220 AutofillOptions.setAddressList = function(entries) {
221 AutofillOptions.getInstance().setAddressList_(entries);
224 AutofillOptions.setCreditCardList = function(entries) {
225 AutofillOptions.getInstance().setCreditCardList_(entries);
228 AutofillOptions.removeData = function(guid, metricsAction) {
229 AutofillOptions.getInstance().removeData_(guid, metricsAction);
232 AutofillOptions.editAddress = function(address) {
233 AutofillOptions.getInstance().showEditAddressOverlay_(address);
236 AutofillOptions.walletIntegrationAvailableStateChanged = function(available) {
237 AutofillOptions.getInstance().
238 walletIntegrationAvailableStateChanged_(available);
242 * @param {CreditCardData} creditCard
244 AutofillOptions.editCreditCard = function(creditCard) {
245 AutofillOptions.getInstance().showEditCreditCardOverlay_(creditCard);
248 // Export
249 return {
250 AutofillOptions: AutofillOptions