- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / alert_overlay.js
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 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     /** @override */
48     get nestingLevel() {
49       // AlertOverlay is special in that it is not tied to one page or overlay.
50       // Set the nesting level arbitrarily high so as to always be recognized as
51       // the top-most visible page.
52       return 99;
53     },
54
55     /**
56      * Handle the 'ok' button.  Clear the overlay and call the ok callback if
57      * available.
58      * @private
59      */
60     handleOK_: function() {
61       OptionsPage.closeOverlay();
62       if (this.okCallback != undefined) {
63         this.okCallback.call();
64       }
65     },
66
67     /**
68      * Handle the 'cancel' button.  Clear the overlay and call the cancel
69      * callback if available.
70      * @private
71      */
72     handleCancel_: function() {
73       OptionsPage.closeOverlay();
74       if (this.cancelCallback != undefined) {
75         this.cancelCallback.call();
76       }
77     },
78
79     /**
80      * The page is getting hidden. Don't let it be shown again.
81      */
82     willHidePage: function() {
83       canShow_ = false;
84     },
85
86     /** @override */
87     canShowPage: function() {
88       return this.canShow_;
89     },
90   };
91
92   /**
93    * Show an alert overlay with the given message, button titles, and
94    * callbacks.
95    * @param {string} title The alert title to display to the user.
96    * @param {string} message The alert message to display to the user.
97    * @param {string} okTitle The title of the OK button. If undefined or empty,
98    *     no button is shown.
99    * @param {string} cancelTitle The title of the cancel button. If undefined or
100    *     empty, no button is shown.
101    * @param {function} okCallback A function to be called when the user presses
102    *     the ok button.  The alert window will be closed automatically.  Can be
103    *     undefined.
104    * @param {function} cancelCallback A function to be called when the user
105    *     presses the cancel button.  The alert window will be closed
106    *     automatically.  Can be undefined.
107    */
108   AlertOverlay.show = function(
109       title, message, okTitle, cancelTitle, okCallback, cancelCallback) {
110     if (title != undefined) {
111       $('alertOverlayTitle').textContent = title;
112       $('alertOverlayTitle').style.display = 'block';
113     } else {
114       $('alertOverlayTitle').style.display = 'none';
115     }
116
117     if (message != undefined) {
118       $('alertOverlayMessage').textContent = message;
119       $('alertOverlayMessage').style.display = 'block';
120     } else {
121       $('alertOverlayMessage').style.display = 'none';
122     }
123
124     if (okTitle != undefined && okTitle != '') {
125       $('alertOverlayOk').textContent = okTitle;
126       $('alertOverlayOk').style.display = 'block';
127     } else {
128       $('alertOverlayOk').style.display = 'none';
129     }
130
131     if (cancelTitle != undefined && cancelTitle != '') {
132       $('alertOverlayCancel').textContent = cancelTitle;
133       $('alertOverlayCancel').style.display = 'inline';
134     } else {
135       $('alertOverlayCancel').style.display = 'none';
136     }
137
138     var alertOverlay = AlertOverlay.getInstance();
139     alertOverlay.okCallback = okCallback;
140     alertOverlay.cancelCallback = cancelCallback;
141     alertOverlay.canShow_ = true;
142
143     // Intentionally don't show the URL in the location bar as we don't want
144     // people trying to navigate here by hand.
145     OptionsPage.showPageByName('alertOverlay', false);
146   };
147
148   // Export
149   return {
150     AlertOverlay: AlertOverlay
151   };
152 });