Home | History | Annotate | Download | only in options
      1 // Copyright (c) 2010 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 
      8   /**
      9    * AlertOverlay class
     10    * Encapsulated handling of a generic alert.
     11    * @class
     12    */
     13   function AlertOverlay() {
     14     OptionsPage.call(this, 'alertOverlay', '', 'alertOverlay');
     15   }
     16 
     17   cr.addSingletonGetter(AlertOverlay);
     18 
     19   AlertOverlay.prototype = {
     20     // Inherit AlertOverlay from OptionsPage.
     21     __proto__: OptionsPage.prototype,
     22 
     23     /**
     24      * Whether the page can be shown. Used to make sure the page is only
     25      * shown via AlertOverlay.Show(), and not via the address bar.
     26      * @private
     27      */
     28     canShow_: false,
     29 
     30     /**
     31      * Initialize the page.
     32      */
     33     initializePage: function() {
     34       // Call base class implementation to start preference initialization.
     35       OptionsPage.prototype.initializePage.call(this);
     36 
     37       var self = this;
     38       $('alertOverlayOk').onclick = function(event) {
     39         self.handleOK_();
     40       };
     41 
     42       $('alertOverlayCancel').onclick = function(event) {
     43         self.handleCancel_();
     44       };
     45     },
     46 
     47     /**
     48      * Handle the 'ok' button.  Clear the overlay and call the ok callback if
     49      * available.
     50      * @private
     51      */
     52     handleOK_: function() {
     53       OptionsPage.closeOverlay();
     54       if (this.okCallback != undefined) {
     55         this.okCallback.call();
     56       }
     57     },
     58 
     59     /**
     60      * Handle the 'cancel' button.  Clear the overlay and call the cancel
     61      * callback if available.
     62      * @private
     63      */
     64     handleCancel_: function() {
     65       OptionsPage.closeOverlay();
     66       if (this.cancelCallback != undefined) {
     67         this.cancelCallback.call();
     68       }
     69     },
     70 
     71     /**
     72      * The page is getting hidden. Don't let it be shown again.
     73      */
     74     willHidePage: function() {
     75       canShow_ = false;
     76     },
     77 
     78     /** @inheritDoc */
     79     canShowPage: function() {
     80       return this.canShow_;
     81     },
     82   };
     83 
     84   /**
     85    * Show an alert overlay with the given message, button titles, and
     86    * callbacks.
     87    * @param {string} title The alert title to display to the user.
     88    * @param {string} message The alert message to display to the user.
     89    * @param {string} okTitle The title of the OK button. If undefined or empty,
     90    *     no button is shown.
     91    * @param {string} cancelTitle The title of the cancel button. If undefined or
     92    *     empty, no button is shown.
     93    * @param {function} okCallback A function to be called when the user presses
     94    *     the ok button.  The alert window will be closed automatically.  Can be
     95    *     undefined.
     96    * @param {function} cancelCallback A function to be called when the user
     97    *     presses the cancel button.  The alert window will be closed
     98    *     automatically.  Can be undefined.
     99    */
    100   AlertOverlay.show = function(
    101       title, message, okTitle, cancelTitle, okCallback, cancelCallback) {
    102     if (title != undefined) {
    103       $('alertOverlayTitle').textContent = title;
    104       $('alertOverlayTitle').style.display = 'block';
    105     } else {
    106       $('alertOverlayTitle').style.display = 'none';
    107     }
    108 
    109     if (message != undefined) {
    110       $('alertOverlayMessage').textContent = message;
    111       $('alertOverlayMessage').style.display = 'block';
    112     } else {
    113       $('alertOverlayMessage').style.display = 'none';
    114     }
    115 
    116     if (okTitle != undefined && okTitle != '') {
    117       $('alertOverlayOk').textContent = okTitle;
    118       $('alertOverlayOk').style.display = 'block';
    119     } else {
    120       $('alertOverlayOk').style.display = 'none';
    121     }
    122 
    123     if (cancelTitle != undefined && cancelTitle != '') {
    124       $('alertOverlayCancel').textContent = cancelTitle;
    125       $('alertOverlayCancel').style.display = 'inline';
    126     } else {
    127       $('alertOverlayCancel').style.display = 'none';
    128     }
    129 
    130     var alertOverlay = AlertOverlay.getInstance();
    131     alertOverlay.okCallback = okCallback;
    132     alertOverlay.cancelCallback = cancelCallback;
    133     alertOverlay.canShow_ = true;
    134 
    135     // Intentionally don't show the URL in the location bar as we don't want
    136     // people trying to navigate here by hand.
    137     OptionsPage.showPageByName('alertOverlay', false);
    138   }
    139 
    140   // Export
    141   return {
    142     AlertOverlay: AlertOverlay
    143   };
    144 });
    145