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