1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/** @constructor */
remoting.MessageWindowOptions = function() {
/** @type {string} */
this.title = '';
/** @type {string} */
this.message = '';
/** @type {string} */
this.buttonLabel = '';
/** @type {string} */
this.cancelButtonLabel = '';
/** @type {function(number):void} */
this.onResult = function() {};
/** @type {number} */
this.duration = 0;
/** @type {string} */
this.infobox = '';
/** @type {?function():void} */
this.onTimeout = function() {};
};
/**
* Create a new message window.
*
* @param {remoting.MessageWindowOptions} options Message window create options
* @constructor
*/
remoting.MessageWindow = function(options) {
var title = options.title;
var message = options.message;
var okButtonLabel = options.buttonLabel;
var cancelButtonLabel = options.cancelButtonLabel;
var onResult = options.onResult;
var duration = 0;
if (options.duration) {
duration = options.duration;
}
var infobox = '';
if (options.infobox) {
infobox = options.infobox;
}
var onTimeout = options.onTimeout;
/** @type {number} */
this.id_ = remoting.MessageWindowManager.addMessageWindow(this);
/** @type {?function(number):void} */
this.onResult_ = onResult;
/** @type {Window} */
this.window_ = null;
/** @type {number} */
this.timer_ = 0;
/** @type {Array<function():void>} */
this.pendingWindowOperations_ = [];
/**
* Callback to call when the timeout expires.
* @type {?function():void}
*/
this.onTimeout_ = onTimeout;
var message_struct = {
command: 'show',
id: this.id_,
title: title,
message: message,
infobox: infobox,
buttonLabel: okButtonLabel,
cancelButtonLabel: cancelButtonLabel,
showSpinner: (duration != 0)
};
var windowAttributes = {
bounds: {
width: 400,
height: 100
},
resizable: false
};
/** @type {remoting.MessageWindow} */
var that = this;
/** @param {AppWindow} appWindow */
var onCreate = function(appWindow) {
that.setWindow_(/** @type {Window} */(appWindow.contentWindow));
var onLoad = function() {
appWindow.contentWindow.postMessage(message_struct, '*');
};
appWindow.contentWindow.addEventListener('load', onLoad, false);
};
chrome.app.window.create('message_window.html', windowAttributes, onCreate);
if (duration != 0) {
this.timer_ = window.setTimeout(this.onTimeoutHandler_.bind(this),
duration);
}
};
/**
* Called when the timer runs out. This in turn calls the window's
* timeout handler (if any).
*/
remoting.MessageWindow.prototype.onTimeoutHandler_ = function() {
this.close();
if (this.onTimeout_) {
this.onTimeout_();
}
};
/**
* Update the message being shown in the window. This should only be called
* after the window has been shown.
*
* @param {string} message The message.
*/
remoting.MessageWindow.prototype.updateMessage = function(message) {
if (!this.window_) {
this.pendingWindowOperations_.push(this.updateMessage.bind(this, message));
return;
}
var message_struct = {
command: 'update_message',
message: message
};
this.window_.postMessage(message_struct, '*');
};
/**
* Close the message box and unregister it with the window manager.
*/
remoting.MessageWindow.prototype.close = function() {
if (!this.window_) {
this.pendingWindowOperations_.push(this.close.bind(this));
return;
}
if (this.timer_) {
window.clearTimeout(this.timer_);
}
this.timer_ = 0;
// Unregister the window with the window manager.
// After this call, events sent to this window will no longer trigger the
// onResult callback.
remoting.MessageWindowManager.deleteMessageWindow(this.id_);
this.window_.close();
this.window_ = null;
};
/**
* Dispatch a message box result to the registered callback.
*
* @param {number} result The dialog result.
*/
remoting.MessageWindow.prototype.handleResult = function(result) {
if (this.onResult_) {
this.onResult_(result);
}
}
/**
* Set the window handle and run any pending operations that require it.
*
* @param {Window} window
* @private
*/
remoting.MessageWindow.prototype.setWindow_ = function(window) {
base.debug.assert(this.window_ == null);
this.window_ = window;
for (var i = 0; i < this.pendingWindowOperations_.length; ++i) {
var pendingOperation = this.pendingWindowOperations_[i];
pendingOperation();
}
this.pendingWindowOperations_ = [];
};
/**
* Static method to create and show a confirm message box.
*
* @param {string} title The title of the message box.
* @param {string} message The message.
* @param {string} okButtonLabel The text for the primary button.
* @param {string} cancelButtonLabel The text for the secondary button.
* @param {function(number):void} onResult The callback to invoke when the
* user closes the message window.
* @return {remoting.MessageWindow}
*/
remoting.MessageWindow.showConfirmWindow = function(
title, message, okButtonLabel, cancelButtonLabel, onResult) {
var options = /** @type {remoting.MessageWindowOptions} */ ({
title: title,
message: message,
buttonLabel: okButtonLabel,
cancelButtonLabel: cancelButtonLabel,
onResult: onResult
});
return new remoting.MessageWindow(options);
};
/**
* Static method to create and show a simple message box.
*
* @param {string} title The title of the message box.
* @param {string} message The message.
* @param {string} buttonLabel The text for the primary button.
* @param {function(number):void} onResult The callback to invoke when the
* user closes the message window.
* @return {remoting.MessageWindow}
*/
remoting.MessageWindow.showMessageWindow = function(
title, message, buttonLabel, onResult) {
var options = /** @type {remoting.MessageWindowOptions} */ ({
title: title,
message: message,
buttonLabel: buttonLabel,
onResult: onResult
});
return new remoting.MessageWindow(options);
};
/**
* Static method to create and show an error message box with an "OK" button.
* The app will close when the user dismisses the message window.
*
* @param {string} title The title of the message box.
* @param {string} message The message.
* @return {remoting.MessageWindow}
*/
remoting.MessageWindow.showErrorMessage = function(title, message) {
var options = /** @type {remoting.MessageWindowOptions} */ ({
title: title,
message: message,
buttonLabel: chrome.i18n.getMessage(/*i18n-content*/'OK'),
onResult: remoting.MessageWindow.quitApp
});
return new remoting.MessageWindow(options);
};
/**
* Static method to create and show a timed message box.
*
* @param {string} title The title of the message box.
* @param {string} message The message.
* @param {string} infobox Additional information to be displayed in an infobox,
* or the empty string if there is no additional information.
* @param {string} buttonLabel The text for the primary button.
* @param {function(number):void} onResult The callback to invoke when the
* user closes the message window.
* @param {number} duration Time for wait before calling onTime
* @param {?function():void} onTimeout Callback function.
* @return {remoting.MessageWindow}
*/
remoting.MessageWindow.showTimedMessageWindow = function(
title, message, infobox, buttonLabel, onResult, duration, onTimeout) {
var options = /** @type {remoting.MessageWindowOptions} */ ({
title: title,
message: message,
infobox: infobox,
buttonLabel: buttonLabel,
onResult: onResult,
duration: duration,
onTimeout: onTimeout
});
return new remoting.MessageWindow(options);
};
/**
* Cancel the current connection and close all app windows.
*
* @param {number} result The dialog result.
*/
remoting.MessageWindow.quitApp = function(result) {
remoting.MessageWindowManager.closeAllMessageWindows();
window.close();
};
|