• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5cr.define('options', function() {
6  var OptionsPage = options.OptionsPage;
7  var ArrayDataModel = cr.ui.ArrayDataModel;
8
9  /////////////////////////////////////////////////////////////////////////////
10  // AutofillOptions class:
11
12  /**
13   * Encapsulated handling of Autofill options page.
14   * @constructor
15   */
16  function AutofillOptions() {
17    OptionsPage.call(this,
18                     'autofill',
19                     loadTimeData.getString('autofillOptionsPageTabTitle'),
20                     'autofill-options');
21  }
22
23  cr.addSingletonGetter(AutofillOptions);
24
25  AutofillOptions.prototype = {
26    __proto__: OptionsPage.prototype,
27
28    /**
29     * The address list.
30     * @type {DeletableItemList}
31     * @private
32     */
33    addressList_: null,
34
35    /**
36     * The credit card list.
37     * @type {DeletableItemList}
38     * @private
39     */
40    creditCardList_: null,
41
42    initializePage: function() {
43      OptionsPage.prototype.initializePage.call(this);
44
45      this.createAddressList_();
46      this.createCreditCardList_();
47
48      var self = this;
49      $('autofill-add-address').onclick = function(event) {
50        self.showAddAddressOverlay_();
51      };
52      $('autofill-add-creditcard').onclick = function(event) {
53        self.showAddCreditCardOverlay_();
54      };
55      $('autofill-options-confirm').onclick = function(event) {
56        OptionsPage.closeOverlay();
57      };
58<if expr="is_macosx">
59      $('autofill-use-mac-address-book-checkbox').onchange = function(event) {
60        if (this.checked) {
61          setTimeout(function() {
62            // Prompt the user to give Chrome access to the user's Address
63            // Book, if the user was not previously prompted. The dialog that
64            // appears blocks the Chrome process, so wait for a small period of
65            // time to allow the checkbox to appear filled in.
66            chrome.send('accessAddressBook');
67          }, 10);
68        }
69      };
70</if>
71
72      // TODO(jhawkins): What happens when Autofill is disabled whilst on the
73      // Autofill options page?
74    },
75
76    /**
77     * Creates, decorates and initializes the address list.
78     * @private
79     */
80    createAddressList_: function() {
81      this.addressList_ = $('address-list');
82      options.autofillOptions.AutofillAddressList.decorate(this.addressList_);
83      this.addressList_.autoExpands = true;
84    },
85
86    /**
87     * Creates, decorates and initializes the credit card list.
88     * @private
89     */
90    createCreditCardList_: function() {
91      this.creditCardList_ = $('creditcard-list');
92      options.autofillOptions.AutofillCreditCardList.decorate(
93          this.creditCardList_);
94      this.creditCardList_.autoExpands = true;
95    },
96
97    /**
98     * Shows the 'Add address' overlay, specifically by loading the
99     * 'Edit address' overlay and modifying the overlay title.
100     * @private
101     */
102    showAddAddressOverlay_: function() {
103      var title = loadTimeData.getString('addAddressTitle');
104      AutofillEditAddressOverlay.setTitle(title);
105      OptionsPage.navigateToPage('autofillEditAddress');
106    },
107
108    /**
109     * Shows the 'Add credit card' overlay, specifically by loading the
110     * 'Edit credit card' overlay and modifying the overlay title.
111     * @private
112     */
113    showAddCreditCardOverlay_: function() {
114      var title = loadTimeData.getString('addCreditCardTitle');
115      AutofillEditCreditCardOverlay.setTitle(title);
116      OptionsPage.navigateToPage('autofillEditCreditCard');
117    },
118
119    /**
120     * Updates the data model for the address list with the values from
121     * |entries|.
122     * @param {Array} entries The list of addresses.
123     */
124    setAddressList_: function(entries) {
125      this.addressList_.dataModel = new ArrayDataModel(entries);
126    },
127
128    /**
129     * Updates the data model for the credit card list with the values from
130     * |entries|.
131     * @param {Array} entries The list of credit cards.
132     */
133    setCreditCardList_: function(entries) {
134      this.creditCardList_.dataModel = new ArrayDataModel(entries);
135    },
136
137    /**
138     * Removes the Autofill address or credit card represented by |guid|.
139     * @param {string} guid The GUID of the address to remove.
140     * @private
141     */
142    removeData_: function(guid) {
143      chrome.send('removeData', [guid]);
144    },
145
146    /**
147     * Requests profile data for the address represented by |guid| from the
148     * PersonalDataManager. Once the data is loaded, the AutofillOptionsHandler
149     * calls showEditAddressOverlay().
150     * @param {string} guid The GUID of the address to edit.
151     * @private
152     */
153    loadAddressEditor_: function(guid) {
154      chrome.send('loadAddressEditor', [guid]);
155    },
156
157    /**
158     * Requests profile data for the credit card represented by |guid| from the
159     * PersonalDataManager. Once the data is loaded, the AutofillOptionsHandler
160     * calls showEditCreditCardOverlay().
161     * @param {string} guid The GUID of the credit card to edit.
162     * @private
163     */
164    loadCreditCardEditor_: function(guid) {
165      chrome.send('loadCreditCardEditor', [guid]);
166    },
167
168    /**
169     * Shows the 'Edit address' overlay, using the data in |address| to fill the
170     * input fields. |address| is a list with one item, an associative array
171     * that contains the address data.
172     * @private
173     */
174    showEditAddressOverlay_: function(address) {
175      var title = loadTimeData.getString('editAddressTitle');
176      AutofillEditAddressOverlay.setTitle(title);
177      AutofillEditAddressOverlay.loadAddress(address);
178      OptionsPage.navigateToPage('autofillEditAddress');
179    },
180
181    /**
182     * Shows the 'Edit credit card' overlay, using the data in |credit_card| to
183     * fill the input fields. |address| is a list with one item, an associative
184     * array that contains the credit card data.
185     * @private
186     */
187    showEditCreditCardOverlay_: function(creditCard) {
188      var title = loadTimeData.getString('editCreditCardTitle');
189      AutofillEditCreditCardOverlay.setTitle(title);
190      AutofillEditCreditCardOverlay.loadCreditCard(creditCard);
191      OptionsPage.navigateToPage('autofillEditCreditCard');
192    },
193  };
194
195  AutofillOptions.setAddressList = function(entries) {
196    AutofillOptions.getInstance().setAddressList_(entries);
197  };
198
199  AutofillOptions.setCreditCardList = function(entries) {
200    AutofillOptions.getInstance().setCreditCardList_(entries);
201  };
202
203  AutofillOptions.removeData = function(guid) {
204    AutofillOptions.getInstance().removeData_(guid);
205  };
206
207  AutofillOptions.loadAddressEditor = function(guid) {
208    AutofillOptions.getInstance().loadAddressEditor_(guid);
209  };
210
211  AutofillOptions.loadCreditCardEditor = function(guid) {
212    AutofillOptions.getInstance().loadCreditCardEditor_(guid);
213  };
214
215  AutofillOptions.editAddress = function(address) {
216    AutofillOptions.getInstance().showEditAddressOverlay_(address);
217  };
218
219  AutofillOptions.editCreditCard = function(creditCard) {
220    AutofillOptions.getInstance().showEditCreditCardOverlay_(creditCard);
221  };
222
223  // Export
224  return {
225    AutofillOptions: AutofillOptions
226  };
227
228});
229
230