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