alert_overlay.js revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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
5cr.define('alertOverlay', function() {
6  /**
7   * The confirm <button>.
8   * @type {HTMLElement}
9   */
10  var okButton;
11
12  /**
13   * The cancel <button>.
14   * @type {HTMLElement}
15   */
16  var cancelButton;
17
18  function initialize(e) {
19    okButton = $('alertOverlayOk');
20    cancelButton = $('alertOverlayCancel');
21
22    // The callbacks are set to the callbacks provided in show(). Clear them
23    // out when either is clicked.
24    okButton.addEventListener('click', function(e) {
25      assert(okButton.clickCallback);
26
27      okButton.clickCallback(e);
28      okButton.clickCallback = null;
29      cancelButton.clickCallback = null;
30    });
31    cancelButton.addEventListener('click', function(e) {
32      assert(cancelButton.clickCallback);
33
34      cancelButton.clickCallback(e);
35      okButton.clickCallback = null;
36      cancelButton.clickCallback = null;
37    });
38  };
39
40  /**
41   * Updates the alert overlay with the given message, button titles, and
42   * callbacks.
43   * @param {string} title The alert title to display to the user.
44   * @param {string} message The alert message to display to the user.
45   * @param {string=} opt_okTitle The title of the OK button. If undefined or
46   *     empty, no button is shown.
47   * @param {string=} opt_cancelTitle The title of the cancel button. If
48   *     undefined or empty, no button is shown.
49   * @param {function()=} opt_okCallback A function to be called when the user
50   *     presses the ok button. Can be undefined if |opt_okTitle| is falsey.
51   * @param {function()=} opt_cancelCallback A function to be called when the
52   *     user presses the cancel button. Can be undefined if |opt_cancelTitle|
53   *     is falsey.
54   */
55  function setValues(title, message, opt_okTitle, opt_cancelTitle,
56                     opt_okCallback, opt_cancelCallback) {
57    if (typeof title != 'undefined')
58      $('alertOverlayTitle').textContent = title;
59    $('alertOverlayTitle').hidden = typeof title == 'undefined';
60
61    if (typeof message != 'undefined')
62      $('alertOverlayMessage').textContent = message;
63    $('alertOverlayMessage').hidden = typeof message == 'undefined';
64
65    if (opt_okTitle)
66      okButton.textContent = opt_okTitle;
67    okButton.hidden = !opt_okTitle;
68    okButton.clickCallback = opt_okCallback;
69
70    if (opt_cancelTitle)
71      cancelButton.textContent = opt_cancelTitle;
72    cancelButton.hidden = !opt_cancelTitle;
73    cancelButton.clickCallback = opt_cancelCallback;
74  };
75
76  // Export
77  return {
78    initialize: initialize,
79    setValues: setValues
80  };
81});
82
83document.addEventListener('DOMContentLoaded', alertOverlay.initialize);
84