1 // Copyright 2014 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.
7 /** @suppress {duplicate} */
8 var remoting
= remoting
|| {};
11 remoting
.MessageWindowOptions = function() {
19 this.buttonLabel
= '';
22 this.cancelButtonLabel
= '';
24 /** @type {function(number):void} */
25 this.onResult = function() {};
33 /** @type {?function():void} */
34 this.onTimeout = function() {};
38 * Create a new message window.
40 * @param {remoting.MessageWindowOptions} options Message window create options
43 remoting
.MessageWindow = function(options
) {
44 var title
= options
.title
;
45 var message
= options
.message
;
46 var okButtonLabel
= options
.buttonLabel
;
47 var cancelButtonLabel
= options
.cancelButtonLabel
;
48 var onResult
= options
.onResult
;
50 if (options
.duration
) {
51 duration
= options
.duration
;
54 if (options
.infobox
) {
55 infobox
= options
.infobox
;
57 var onTimeout
= options
.onTimeout
;
60 this.id_
= remoting
.MessageWindowManager
.addMessageWindow(this);
62 /** @type {?function(number):void} */
63 this.onResult_
= onResult
;
71 /** @type {Array<function():void>} */
72 this.pendingWindowOperations_
= [];
75 * Callback to call when the timeout expires.
76 * @type {?function():void}
78 this.onTimeout_
= onTimeout
;
80 var message_struct
= {
86 buttonLabel
: okButtonLabel
,
87 cancelButtonLabel
: cancelButtonLabel
,
88 showSpinner
: (duration
!= 0)
91 var windowAttributes
= {
99 /** @type {remoting.MessageWindow} */
102 /** @param {AppWindow} appWindow */
103 var onCreate = function(appWindow
) {
104 that
.setWindow_(/** @type {Window} */(appWindow
.contentWindow
));
105 var onLoad = function() {
106 appWindow
.contentWindow
.postMessage(message_struct
, '*');
108 appWindow
.contentWindow
.addEventListener('load', onLoad
, false);
111 chrome
.app
.window
.create('message_window.html', windowAttributes
, onCreate
);
114 this.timer_
= window
.setTimeout(this.onTimeoutHandler_
.bind(this),
120 * Called when the timer runs out. This in turn calls the window's
121 * timeout handler (if any).
123 remoting
.MessageWindow
.prototype.onTimeoutHandler_ = function() {
125 if (this.onTimeout_
) {
131 * Update the message being shown in the window. This should only be called
132 * after the window has been shown.
134 * @param {string} message The message.
136 remoting
.MessageWindow
.prototype.updateMessage = function(message
) {
138 this.pendingWindowOperations_
.push(this.updateMessage
.bind(this, message
));
142 var message_struct
= {
143 command
: 'update_message',
146 this.window_
.postMessage(message_struct
, '*');
150 * Close the message box and unregister it with the window manager.
152 remoting
.MessageWindow
.prototype.close = function() {
154 this.pendingWindowOperations_
.push(this.close
.bind(this));
159 window
.clearTimeout(this.timer_
);
163 // Unregister the window with the window manager.
164 // After this call, events sent to this window will no longer trigger the
165 // onResult callback.
166 remoting
.MessageWindowManager
.deleteMessageWindow(this.id_
);
167 this.window_
.close();
172 * Dispatch a message box result to the registered callback.
174 * @param {number} result The dialog result.
176 remoting
.MessageWindow
.prototype.handleResult = function(result
) {
177 if (this.onResult_
) {
178 this.onResult_(result
);
183 * Set the window handle and run any pending operations that require it.
185 * @param {Window} window
188 remoting
.MessageWindow
.prototype.setWindow_ = function(window
) {
189 base
.debug
.assert(this.window_
== null);
190 this.window_
= window
;
191 for (var i
= 0; i
< this.pendingWindowOperations_
.length
; ++i
) {
192 var pendingOperation
= this.pendingWindowOperations_
[i
];
195 this.pendingWindowOperations_
= [];
199 * Static method to create and show a confirm message box.
201 * @param {string} title The title of the message box.
202 * @param {string} message The message.
203 * @param {string} okButtonLabel The text for the primary button.
204 * @param {string} cancelButtonLabel The text for the secondary button.
205 * @param {function(number):void} onResult The callback to invoke when the
206 * user closes the message window.
207 * @return {remoting.MessageWindow}
209 remoting
.MessageWindow
.showConfirmWindow = function(
210 title
, message
, okButtonLabel
, cancelButtonLabel
, onResult
) {
211 var options
= /** @type {remoting.MessageWindowOptions} */ ({
214 buttonLabel
: okButtonLabel
,
215 cancelButtonLabel
: cancelButtonLabel
,
218 return new remoting
.MessageWindow(options
);
222 * Static method to create and show a simple message box.
224 * @param {string} title The title of the message box.
225 * @param {string} message The message.
226 * @param {string} buttonLabel The text for the primary button.
227 * @param {function(number):void} onResult The callback to invoke when the
228 * user closes the message window.
229 * @return {remoting.MessageWindow}
231 remoting
.MessageWindow
.showMessageWindow = function(
232 title
, message
, buttonLabel
, onResult
) {
233 var options
= /** @type {remoting.MessageWindowOptions} */ ({
236 buttonLabel
: buttonLabel
,
239 return new remoting
.MessageWindow(options
);
243 * Static method to create and show an error message box with an "OK" button.
244 * The app will close when the user dismisses the message window.
246 * @param {string} title The title of the message box.
247 * @param {string} message The message.
248 * @return {remoting.MessageWindow}
250 remoting
.MessageWindow
.showErrorMessage = function(title
, message
) {
251 var options
= /** @type {remoting.MessageWindowOptions} */ ({
254 buttonLabel
: chrome
.i18n
.getMessage(/*i18n-content*/'OK'),
255 onResult
: remoting
.MessageWindow
.quitApp
257 return new remoting
.MessageWindow(options
);
261 * Static method to create and show a timed message box.
263 * @param {string} title The title of the message box.
264 * @param {string} message The message.
265 * @param {string} infobox Additional information to be displayed in an infobox,
266 * or the empty string if there is no additional information.
267 * @param {string} buttonLabel The text for the primary button.
268 * @param {function(number):void} onResult The callback to invoke when the
269 * user closes the message window.
270 * @param {number} duration Time for wait before calling onTime
271 * @param {?function():void} onTimeout Callback function.
272 * @return {remoting.MessageWindow}
274 remoting
.MessageWindow
.showTimedMessageWindow = function(
275 title
, message
, infobox
, buttonLabel
, onResult
, duration
, onTimeout
) {
276 var options
= /** @type {remoting.MessageWindowOptions} */ ({
280 buttonLabel
: buttonLabel
,
285 return new remoting
.MessageWindow(options
);
289 * Cancel the current connection and close all app windows.
291 * @param {number} result The dialog result.
293 remoting
.MessageWindow
.quitApp = function(result
) {
294 remoting
.MessageWindowManager
.closeAllMessageWindows();