1// Copyright (c) 2011 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
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                     templateData.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
56      // TODO(jhawkins): What happens when Autofill is disabled whilst on the
57      // Autofill options page?
58    },
59
60    /**
61     * Creates, decorates and initializes the address list.
62     * @private
63     */
64    createAddressList_: function() {
65      this.addressList_ = $('address-list');
66      options.autofillOptions.AutofillAddressList.decorate(this.addressList_);
67      this.addressList_.autoExpands = true;
68    },
69
70    /**
71     * Creates, decorates and initializes the credit card list.
72     * @private
73     */
74    createCreditCardList_: function() {
75      this.creditCardList_ = $('creditcard-list');
76      options.autofillOptions.AutofillCreditCardList.decorate(
77          this.creditCardList_);
78      this.creditCardList_.autoExpands = true;
79    },
80
81    /**
82     * Shows the 'Add address' overlay, specifically by loading the
83     * 'Edit address' overlay, emptying the input fields and modifying the
84     * overlay title.
85     * @private
86     */
87    showAddAddressOverlay_: function() {
88      var title = localStrings.getString('addAddressTitle');
89      AutofillEditAddressOverlay.setTitle(title);
90      AutofillEditAddressOverlay.clearInputFields();
91      OptionsPage.navigateToPage('autofillEditAddress');
92    },
93
94    /**
95     * Shows the 'Add credit card' overlay, specifically by loading the
96     * 'Edit credit card' overlay, emptying the input fields and modifying the
97     * overlay title.
98     * @private
99     */
100    showAddCreditCardOverlay_: function() {
101      var title = localStrings.getString('addCreditCardTitle');
102      AutofillEditCreditCardOverlay.setTitle(title);
103      AutofillEditCreditCardOverlay.clearInputFields();
104      OptionsPage.navigateToPage('autofillEditCreditCard');
105    },
106
107    /**
108     * Updates the data model for the address list with the values from
109     * |entries|.
110     * @param {Array} entries The list of addresses.
111     */
112    setAddressList_: function(entries) {
113      this.addressList_.dataModel = new ArrayDataModel(entries);
114    },
115
116    /**
117     * Updates the data model for the credit card list with the values from
118     * |entries|.
119     * @param {Array} entries The list of credit cards.
120     */
121    setCreditCardList_: function(entries) {
122      this.creditCardList_.dataModel = new ArrayDataModel(entries);
123    },
124
125    /**
126     * Removes the Autofill address represented by |guid|.
127     * @param {String} guid The GUID of the address to remove.
128     * @private
129     */
130    removeAddress_: function(guid) {
131      chrome.send('removeAddress', [guid]);
132    },
133
134    /**
135     * Removes the Autofill credit card represented by |guid|.
136     * @param {String} guid The GUID of the credit card to remove.
137     * @private
138     */
139    removeCreditCard_: function(guid) {
140      chrome.send('removeCreditCard', [guid]);
141    },
142
143    /**
144     * Requests profile data for the address represented by |guid| from the
145     * PersonalDataManager. Once the data is loaded, the AutofillOptionsHandler
146     * calls showEditAddressOverlay().
147     * @param {String} guid The GUID of the address to edit.
148     * @private
149     */
150    loadAddressEditor_: function(guid) {
151      chrome.send('loadAddressEditor', [guid]);
152    },
153
154    /**
155     * Requests profile data for the credit card represented by |guid| from the
156     * PersonalDataManager. Once the data is loaded, the AutofillOptionsHandler
157     * calls showEditCreditCardOverlay().
158     * @param {String} guid The GUID of the credit card to edit.
159     * @private
160     */
161    loadCreditCardEditor_: function(guid) {
162      chrome.send('loadCreditCardEditor', [guid]);
163    },
164
165    /**
166     * Shows the 'Edit address' overlay, using the data in |address| to fill the
167     * input fields. |address| is a list with one item, an associative array
168     * that contains the address data.
169     * @private
170     */
171    showEditAddressOverlay_: function(address) {
172      var title = localStrings.getString('editAddressTitle');
173      AutofillEditAddressOverlay.setTitle(title);
174      AutofillEditAddressOverlay.loadAddress(address);
175      OptionsPage.navigateToPage('autofillEditAddress');
176    },
177
178    /**
179     * Shows the 'Edit credit card' overlay, using the data in |credit_card| to
180     * fill the input fields. |address| is a list with one item, an associative
181     * array that contains the credit card data.
182     * @private
183     */
184    showEditCreditCardOverlay_: function(creditCard) {
185      var title = localStrings.getString('editCreditCardTitle');
186      AutofillEditCreditCardOverlay.setTitle(title);
187      AutofillEditCreditCardOverlay.loadCreditCard(creditCard);
188      OptionsPage.navigateToPage('autofillEditCreditCard');
189    },
190  };
191
192  AutofillOptions.setAddressList = function(entries) {
193    AutofillOptions.getInstance().setAddressList_(entries);
194  };
195
196  AutofillOptions.setCreditCardList = function(entries) {
197    AutofillOptions.getInstance().setCreditCardList_(entries);
198  };
199
200  AutofillOptions.removeAddress = function(guid) {
201    AutofillOptions.getInstance().removeAddress_(guid);
202  };
203
204  AutofillOptions.removeCreditCard = function(guid) {
205    AutofillOptions.getInstance().removeCreditCard_(guid);
206  };
207
208  AutofillOptions.loadAddressEditor = function(guid) {
209    AutofillOptions.getInstance().loadAddressEditor_(guid);
210  };
211
212  AutofillOptions.loadCreditCardEditor = function(guid) {
213    AutofillOptions.getInstance().loadCreditCardEditor_(guid);
214  };
215
216  AutofillOptions.editAddress = function(address) {
217    AutofillOptions.getInstance().showEditAddressOverlay_(address);
218  };
219
220  AutofillOptions.editCreditCard = function(creditCard) {
221    AutofillOptions.getInstance().showEditCreditCardOverlay_(creditCard);
222  };
223
224  // Export
225  return {
226    AutofillOptions: AutofillOptions
227  };
228
229});
230
231