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