alert_overlay.js revision bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293
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
5cr.define('options', function() {
6
7  var OptionsPage = options.OptionsPage;
8
9  /**
10   * AlertOverlay class
11   * Encapsulated handling of a generic alert.
12   * @class
13   */
14  function AlertOverlay() {
15    OptionsPage.call(this, 'alertOverlay', '', 'alertOverlay');
16  }
17
18  cr.addSingletonGetter(AlertOverlay);
19
20  AlertOverlay.prototype = {
21    // Inherit AlertOverlay from OptionsPage.
22    __proto__: OptionsPage.prototype,
23
24    /**
25     * Initialize the page.
26     */
27    initializePage: function() {
28      // Call base class implementation to start preference initialization.
29      OptionsPage.prototype.initializePage.call(this);
30
31      var self = this;
32      $('alertOverlayOk').onclick = function(event) {
33        self.handleOK_();
34      };
35
36      $('alertOverlayCancel').onclick = function(event) {
37        self.handleCancel_();
38      };
39    },
40
41    /**
42     * Handle the 'ok' button.  Clear the overlay and call the ok callback if
43     * available.
44     * @private
45     */
46    handleOK_: function() {
47      OptionsPage.clearOverlays();
48      if (this.okCallback != undefined) {
49        this.okCallback.call();
50      }
51    },
52
53    /**
54     * Handle the 'cancel' button.  Clear the overlay and call the cancel
55     * callback if available.
56     * @private
57     */
58    handleCancel_: function() {
59      OptionsPage.clearOverlays();
60      if (this.cancelCallback != undefined) {
61        this.cancelCallback.call();
62      }
63    }
64  };
65
66  /**
67   * Show an alert overlay with the given message, button titles, and
68   * callbacks.
69   * @param {string} title The alert title to display to the user.
70   * @param {string} message The alert message to display to the user.
71   * @param {string} okTitle The title of the OK button.  Can be undefined.
72   * @param {string} cancelTitle The title of the cancel button.  Can be
73   *     undefined.
74   * @param {function} okCallback A function to be called when the user presses
75   *     the ok button.  The alert window will be closed automatically.  Can be
76   *     undefined.
77   * @param {function} cancelCallback A function to be called when the user
78   *     presses the cancel button.  The alert window will be closed
79   *     automatically.  Can be undefined.
80   */
81  AlertOverlay.show = function(title, message, okTitle, cancelTitle, okCallback,
82                               cancelCallback) {
83    if (title != undefined) {
84      $('alertOverlayTitle').textContent = title;
85      $('alertOverlayTitle').style.display = 'block';
86    } else {
87      $('alertOverlayTitle').style.display = 'none';
88    }
89    if (message != undefined) {
90      $('alertOverlayMessage').textContent = message;
91      $('alertOverlayMessage').style.display = 'block';
92    } else {
93      $('alertOverlayMessage').style.display = 'none';
94    }
95    $('alertOverlayOk').textContent =
96        (okTitle != undefined ? okTitle
97                              : localStrings.getString('ok'));
98    if (cancelTitle != '') {
99      $('alertOverlayCancel').textContent =
100          (cancelTitle != undefined ? cancelTitle
101                                    : localStrings.getString('cancel'));
102      $('alertOverlayCancel').style.display = 'inline';
103    } else {
104      $('alertOverlayCancel').style.display = 'none';
105    }
106
107    AlertOverlay.getInstance().okCallback = okCallback;
108    AlertOverlay.getInstance().cancelCallback = cancelCallback;
109
110    OptionsPage.showOverlay('alertOverlay');
111  }
112
113  // Export
114  return {
115    AlertOverlay: AlertOverlay
116  };
117
118});
119