ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / remoting / webapp / base / js / message_window_helper.js
bloba4d564d68e0e1a9dbbe871864ca27be030e4ee0b
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.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /** @constructor */
11 remoting.MessageWindowOptions = function() {
12 /** @type {string} */
13 this.title = '';
15 /** @type {string} */
16 this.message = '';
18 /** @type {string} */
19 this.buttonLabel = '';
21 /** @type {string} */
22 this.cancelButtonLabel = '';
24 /** @type {function(number):void} */
25 this.onResult = function() {};
27 /** @type {number} */
28 this.duration = 0;
30 /** @type {string} */
31 this.infobox = '';
33 /** @type {?function():void} */
34 this.onTimeout = function() {};
37 /**
38 * Create a new message window.
40 * @param {remoting.MessageWindowOptions} options Message window create options
41 * @constructor
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;
49 var duration = 0;
50 if (options.duration) {
51 duration = options.duration;
53 var infobox = '';
54 if (options.infobox) {
55 infobox = options.infobox;
57 var onTimeout = options.onTimeout;
59 /** @type {number} */
60 this.id_ = remoting.MessageWindowManager.addMessageWindow(this);
62 /** @type {?function(number):void} */
63 this.onResult_ = onResult;
65 /** @type {Window} */
66 this.window_ = null;
68 /** @type {number} */
69 this.timer_ = 0;
71 /** @type {Array<function():void>} */
72 this.pendingWindowOperations_ = [];
74 /**
75 * Callback to call when the timeout expires.
76 * @type {?function():void}
78 this.onTimeout_ = onTimeout;
80 var message_struct = {
81 command: 'show',
82 id: this.id_,
83 title: title,
84 message: message,
85 infobox: infobox,
86 buttonLabel: okButtonLabel,
87 cancelButtonLabel: cancelButtonLabel,
88 showSpinner: (duration != 0)
91 var windowAttributes = {
92 bounds: {
93 width: 400,
94 height: 100
96 resizable: false
99 /** @type {remoting.MessageWindow} */
100 var that = this;
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);
113 if (duration != 0) {
114 this.timer_ = window.setTimeout(this.onTimeoutHandler_.bind(this),
115 duration);
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() {
124 this.close();
125 if (this.onTimeout_) {
126 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) {
137 if (!this.window_) {
138 this.pendingWindowOperations_.push(this.updateMessage.bind(this, message));
139 return;
142 var message_struct = {
143 command: 'update_message',
144 message: 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() {
153 if (!this.window_) {
154 this.pendingWindowOperations_.push(this.close.bind(this));
155 return;
158 if (this.timer_) {
159 window.clearTimeout(this.timer_);
161 this.timer_ = 0;
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();
168 this.window_ = null;
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
186 * @private
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];
193 pendingOperation();
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} */ ({
212 title: title,
213 message: message,
214 buttonLabel: okButtonLabel,
215 cancelButtonLabel: cancelButtonLabel,
216 onResult: onResult
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} */ ({
234 title: title,
235 message: message,
236 buttonLabel: buttonLabel,
237 onResult: onResult
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} */ ({
252 title: title,
253 message: message,
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} */ ({
277 title: title,
278 message: message,
279 infobox: infobox,
280 buttonLabel: buttonLabel,
281 onResult: onResult,
282 duration: duration,
283 onTimeout: onTimeout
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();
295 window.close();