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