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 /**
      6  * @fileoverview Base class for dialogs that require saving preferences on
      7  *     confirm and resetting preference inputs on cancel.
      8  */
      9 
     10 cr.define('options', function() {
     11   /** @const */ var OptionsPage = options.OptionsPage;
     12 
     13   /**
     14    * Base class for settings dialogs.
     15    * @constructor
     16    * @param {string} name See OptionsPage constructor.
     17    * @param {string} title See OptionsPage constructor.
     18    * @param {string} pageDivName See OptionsPage constructor.
     19    * @param {HTMLInputElement} okButton The confirmation button element.
     20    * @param {HTMLInputElement} cancelButton The cancellation button element.
     21    * @extends {OptionsPage}
     22    */
     23   function SettingsDialog(name, title, pageDivName, okButton, cancelButton) {
     24     OptionsPage.call(this, name, title, pageDivName);
     25     this.okButton = okButton;
     26     this.cancelButton = cancelButton;
     27   }
     28 
     29   SettingsDialog.prototype = {
     30     __proto__: OptionsPage.prototype,
     31 
     32     /** @override */
     33     initializePage: function() {
     34       this.okButton.onclick = this.handleConfirm.bind(this);
     35       this.cancelButton.onclick = this.handleCancel.bind(this);
     36     },
     37 
     38     /**
     39      * Handles the confirm button by saving the dialog preferences.
     40      */
     41     handleConfirm: function() {
     42       OptionsPage.closeOverlay();
     43 
     44       var prefs = Preferences.getInstance();
     45       var els = this.pageDiv.querySelectorAll('[dialog-pref]');
     46       for (var i = 0; i < els.length; i++) {
     47         if (els[i].pref)
     48           prefs.commitPref(els[i].pref, els[i].metric);
     49       }
     50     },
     51 
     52     /**
     53      * Handles the cancel button by closing the overlay.
     54      */
     55     handleCancel: function() {
     56       OptionsPage.closeOverlay();
     57 
     58       var prefs = Preferences.getInstance();
     59       var els = this.pageDiv.querySelectorAll('[dialog-pref]');
     60       for (var i = 0; i < els.length; i++) {
     61         if (els[i].pref)
     62           prefs.rollbackPref(els[i].pref);
     63       }
     64     },
     65   };
     66 
     67   return {
     68     SettingsDialog: SettingsDialog
     69   };
     70 });
     71