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 {HTMLButtonElement}
9   */
10  var okButton;
11
12  /**
13   * The cancel <button>.
14   * @type {HTMLButtonElement}
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=} okTitle The title of the OK button. If undefined or empty,
46   *     no button is shown.
47   * @param {string=} cancelTitle The title of the cancel button. If undefined
48   *     or empty, no button is shown.
49   * @param {function=} okCallback A function to be called when the user presses
50   *     the ok button. Can be undefined if |okTitle| is falsey.
51   * @param {function=} cancelCallback A function to be called when the user
52   *     presses the cancel button. Can be undefined if |cancelTitle| is falsey.
53   */
54  function setValues(
55      title, message, okTitle, cancelTitle, okCallback, cancelCallback) {
56    if (typeof title != 'undefined')
57      $('alertOverlayTitle').textContent = title;
58    $('alertOverlayTitle').hidden = typeof title == 'undefined';
59
60    if (typeof message != 'undefined')
61      $('alertOverlayMessage').textContent = message;
62    $('alertOverlayMessage').hidden = typeof message == 'undefined';
63
64    if (okTitle)
65      okButton.textContent = okTitle;
66    okButton.hidden = !okTitle;
67    okButton.clickCallback = okCallback;
68
69    if (cancelTitle)
70      cancelButton.textContent = cancelTitle;
71    cancelButton.hidden = !cancelTitle;
72    cancelButton.clickCallback = cancelCallback;
73  };
74
75  // Export
76  return {
77    initialize: initialize,
78    setValues: setValues
79  };
80});
81
82document.addEventListener('DOMContentLoaded', alertOverlay.initialize);
83