Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: remoting/webapp/background/message_window_helper.js

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

Powered by Google App Engine
This is Rietveld 408576698