autofill_options.js revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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  const OptionsPage = options.OptionsPage;
7  const ArrayDataModel = cr.ui.ArrayDataModel;
8  const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel;
9
10  /////////////////////////////////////////////////////////////////////////////
11  // AutoFillOptions class:
12
13  /**
14   * Encapsulated handling of AutoFill options page.
15   * @constructor
16   */
17  function AutoFillOptions() {
18    OptionsPage.call(this,
19                     'autoFillOptions',
20                     templateData.autoFillOptionsTitle,
21                     'autofill-options');
22  }
23
24  cr.addSingletonGetter(AutoFillOptions);
25
26  AutoFillOptions.prototype = {
27    __proto__: OptionsPage.prototype,
28
29    /**
30     * The address list.
31     * @type {DeletableItemList}
32     * @private
33     */
34    addressList_: null,
35
36    /**
37     * The credit card list.
38     * @type {DeletableItemList}
39     * @private
40     */
41    creditCardList_: null,
42
43    initializePage: function() {
44      OptionsPage.prototype.initializePage.call(this);
45
46      this.createAddressList_();
47      this.createCreditCardList_();
48
49      var self = this;
50      $('autofill-add-address').onclick = function(event) {
51        self.showAddAddressOverlay_();
52      };
53      $('autofill-add-creditcard').onclick = function(event) {
54        self.showAddCreditCardOverlay_();
55      };
56
57      // TODO(jhawkins): What happens when AutoFill is disabled whilst on the
58      // AutoFill options page?
59    },
60
61    /**
62     * Creates, decorates and initializes the address list.
63     * @private
64     */
65    createAddressList_: function() {
66      this.addressList_ = $('address-list');
67      options.autoFillOptions.AutoFillList.decorate(this.addressList_);
68      this.addressList_.selectionModel = new ListSingleSelectionModel;
69      this.addressList_.autoExpands = true;
70    },
71
72    /**
73     * Creates, decorates and initializes the credit card list.
74     * @private
75     */
76    createCreditCardList_: function() {
77      this.creditCardList_ = $('creditcard-list');
78      options.autoFillOptions.AutoFillList.decorate(this.creditCardList_);
79      this.creditCardList_.selectionModel = new ListSingleSelectionModel;
80      this.creditCardList_.autoExpands = true;
81    },
82
83    /**
84     * Shows the 'Add address' overlay, specifically by loading the
85     * 'Edit address' overlay, emptying the input fields and modifying the
86     * overlay title.
87     * @private
88     */
89    showAddAddressOverlay_: function() {
90      var title = localStrings.getString('addAddressTitle');
91      AutoFillEditAddressOverlay.setTitle(title);
92      AutoFillEditAddressOverlay.clearInputFields();
93      OptionsPage.showOverlay('autoFillEditAddressOverlay');
94    },
95
96    /**
97     * Shows the 'Add credit card' overlay, specifically by loading the
98     * 'Edit credit card' overlay, emptying the input fields and modifying the
99     * overlay title.
100     * @private
101     */
102    showAddCreditCardOverlay_: function() {
103      var title = localStrings.getString('addCreditCardTitle');
104      AutoFillEditCreditCardOverlay.setTitle(title);
105      AutoFillEditCreditCardOverlay.clearInputFields();
106      OptionsPage.showOverlay('autoFillEditCreditCardOverlay');
107    },
108
109    /**
110     * Updates the data model for the address list with the values from
111     * |entries|.
112     * @param {Array} entries The list of addresses.
113     */
114    setAddressList_: function(entries) {
115      this.addressList_.dataModel = new ArrayDataModel(entries);
116    },
117
118    /**
119     * Updates the data model for the credit card list with the values from
120     * |entries|.
121     * @param {Array} entries The list of credit cards.
122     */
123    setCreditCardList_: function(entries) {
124      this.creditCardList_.dataModel = new ArrayDataModel(entries);
125    },
126
127    /**
128     * Removes the AutoFill profile represented by |guid|.
129     * @param {String} guid The GUID of the profile to remove.
130     * @private
131     */
132    removeAutoFillProfile_: function(guid) {
133      chrome.send('removeAutoFillProfile', [guid]);
134    },
135
136    /**
137     * Requests profile data for the profile represented by |guid| from the
138     * PersonalDataManager. Once the data is loaded, the AutoFillOptionsHandler
139     * calls showEdit[Address,CreditCard]Overlay(), depending on the type of the
140     * profile.
141     * @param {String} guid The GUID of the profile to edit.
142     * @private
143     */
144    loadProfileEditor_: function(guid) {
145      chrome.send('loadProfileEditor', [guid]);
146    },
147
148    /**
149     * Shows the 'Edit address' overlay, using the data in |address| to fill the
150     * input fields. |address| is a list with one item, an associative array
151     * that contains the address data.
152     * @private
153     */
154    showEditAddressOverlay_: function(address) {
155      var title = localStrings.getString('editAddressTitle');
156      AutoFillEditAddressOverlay.setTitle(title);
157      AutoFillEditAddressOverlay.loadAddress(address[0]);
158      OptionsPage.showOverlay('autoFillEditAddressOverlay');
159    },
160
161    /**
162     * Shows the 'Edit credit card' overlay, using the data in |credit_card| to
163     * fill the input fields. |address| is a list with one item, an associative
164     * array that contains the credit card data.
165     * @private
166     */
167    showEditCreditCardOverlay_: function(creditCard) {
168      var title = localStrings.getString('editCreditCardTitle');
169      AutoFillEditCreditCardOverlay.setTitle(title);
170      AutoFillEditCreditCardOverlay.loadCreditCard(creditCard[0]);
171      OptionsPage.showOverlay('autoFillEditCreditCardOverlay');
172    },
173  };
174
175  AutoFillOptions.setAddressList = function(entries) {
176    AutoFillOptions.getInstance().setAddressList_(entries);
177  };
178
179  AutoFillOptions.setCreditCardList = function(entries) {
180    AutoFillOptions.getInstance().setCreditCardList_(entries);
181  };
182
183  AutoFillOptions.removeAutoFillProfile = function(guid) {
184    AutoFillOptions.getInstance().removeAutoFillProfile_(guid);
185  };
186
187  AutoFillOptions.loadProfileEditor = function(guid) {
188    AutoFillOptions.getInstance().loadProfileEditor_(guid);
189  };
190
191  AutoFillOptions.editAddress = function(address) {
192    AutoFillOptions.getInstance().showEditAddressOverlay_(address);
193  };
194
195  AutoFillOptions.editCreditCard = function(creditCard) {
196    AutoFillOptions.getInstance().showEditCreditCardOverlay_(creditCard);
197  };
198
199  // Export
200  return {
201    AutoFillOptions: AutoFillOptions
202  };
203
204});
205
206