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

Side by Side Diff: remoting/webapp/background/message_window.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 /**
6 * @constructor
7 */
8 function MessageWindowImpl() {
9 /**
10 * Used to prevent multiple responses due to the closeWindow handler.
11 *
12 * @type {boolean}
13 * @private
14 */
15 this.sentReply_ = false;
16
17 window.addEventListener('message', this.onMessage_.bind(this), false);
18 };
19
20 /**
21 * @param {Window} parentWindow The id of the window that showed the message.
22 * @param {string} messageId The identifier of the message, as supplied by the
23 * parent.
24 * @param {number} result 0 if window was closed without pressing a button;
25 * otherwise the index of the button pressed (e.g., 1 = primary).
26 * @private
27 */
28 MessageWindowImpl.prototype.sendReply_ = function(
29 parentWindow, messageId, result) {
30 // Only forward the first reply that we receive.
31 if (!this.sentReply_) {
32 var message = {
33 command: 'messageWindowResult',
34 id: messageId,
35 result: result
36 };
37 parentWindow.postMessage(message, '*');
38 this.sentReply_ = true;
39 } else {
40 // Make sure that the reply we're ignoring is from the window close.
41 base.debug.assert(result == 0);
42 }
43 };
44
45 /**
46 * Size the window to its content vertically.
47 * @private
48 */
49 MessageWindowImpl.prototype.updateSize_ = function() {
50 var borderY = window.outerHeight - window.innerHeight;
51 window.resizeTo(window.outerWidth, document.body.clientHeight + borderY);
52 };
53
54 /**
55 * Initializes the button with the label and the click handler.
56 * Hides the button if the label is null or undefined.
57 *
58 * @param{HTMLElement} button
59 * @param{?string} label
60 * @param{Function} clickHandler
61 * @private
62 */
63 MessageWindowImpl.prototype.initButton_ =
64 function(button, label, clickHandler) {
65 if (label) {
66 button.innerText = label;
67 button.addEventListener('click', clickHandler, false);
68 }
69 button.hidden = !Boolean(label);
70 };
71
72 /**
73 * Event-handler callback, invoked when the parent window supplies the
74 * message content.
75 *
76 * @param{Event} event
77 * @private
78 */
79 MessageWindowImpl.prototype.onMessage_ = function(event) {
80 switch (event.data['command']) {
81 case 'show':
82 // Validate the message.
83 var messageId = /** @type {number} */ (event.data['id']);
84 var title = /** @type {string} */ (event.data['title']);
85 var message = /** @type {string} */ (event.data['message']);
86 var infobox = /** @type {string} */ (event.data['infobox']);
87 var buttonLabel = /** @type {string} */ (event.data['buttonLabel']);
88 /** @type {string} */
89 var cancelButtonLabel = (event.data['cancelButtonLabel']);
90 var showSpinner = /** @type {boolean} */ (event.data['showSpinner']);
91 if (typeof(messageId) != 'number' ||
92 typeof(title) != 'string' ||
93 typeof(message) != 'string' ||
94 typeof(infobox) != 'string' ||
95 typeof(buttonLabel) != 'string' ||
96 typeof(showSpinner) != 'boolean') {
97 console.log('Bad show message:', event.data);
98 break;
99 }
100
101 // Set the dialog text.
102 var button = document.getElementById('button-primary');
103 var cancelButton = document.getElementById('button-secondary');
104 var messageDiv = document.getElementById('message');
105 var infoboxDiv = document.getElementById('infobox');
106 document.getElementById('title').innerText = title;
107 document.querySelector('title').innerText = title;
108 messageDiv.innerText = message;
109 if (showSpinner) {
110 messageDiv.classList.add('waiting');
111 messageDiv.classList.add('prominent');
112 }
113 if (infobox != '') {
114 infoboxDiv.innerText = infobox;
115 } else {
116 infoboxDiv.hidden = true;
117 }
118
119 this.initButton_(
120 button,
121 buttonLabel,
122 this.sendReply_.bind(this, event.source, messageId, 1));
123
124 this.initButton_(
125 cancelButton,
126 cancelButtonLabel,
127 this.sendReply_.bind(this, event.source, messageId, 0));
128
129 var buttonToFocus = (cancelButtonLabel) ? cancelButton : button;
130 buttonToFocus.focus();
131
132 // Add a close handler in case the window is closed without clicking one
133 // of the buttons. This will send a 0 as the result.
134 // Note that when a button is pressed, this will result in sendReply_
135 // being called multiple times (once for the button, once for close).
136 chrome.app.window.current().onClosed.addListener(
137 this.sendReply_.bind(this, event.source, messageId, 0));
138
139 this.updateSize_();
140 chrome.app.window.current().show();
141 break;
142
143 case 'update_message':
144 var message = /** @type {string} */ (event.data['message']);
145 if (typeof(message) != 'string') {
146 console.log('Bad update_message message:', event.data);
147 break;
148 }
149
150 var messageDiv = document.getElementById('message');
151 messageDiv.innerText = message;
152
153 this.updateSize_();
154 break;
155
156 default:
157 console.error('Unexpected message:', event.data);
158 }
159 };
160
161 var messageWindow = new MessageWindowImpl();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698