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.
10 function MessageWindowImpl() {
12 * Used to prevent multiple responses due to the closeWindow handler.
16 this.sentReply_
= false;
18 window
.addEventListener('message', this.onMessage_
.bind(this), false);
22 * @param {Window} parentWindow The id of the window that showed the message.
23 * @param {number} messageId The identifier of the message, as supplied by the
25 * @param {number} result 0 if window was closed without pressing a button;
26 * otherwise the index of the button pressed (e.g., 1 = primary).
29 MessageWindowImpl
.prototype.sendReply_ = function(
30 parentWindow
, messageId
, result
) {
31 // Only forward the first reply that we receive.
32 if (!this.sentReply_
) {
34 source
: 'message-window',
35 command
: 'messageWindowResult',
39 parentWindow
.postMessage(message
, '*');
40 this.sentReply_
= true;
42 // Make sure that the reply we're ignoring is from the window close.
43 console
.assert(result
== 0, 'Received unexpected result ' + result
+ '.');
48 * Initializes the button with the label and the click handler.
49 * Hides the button if the label is null or undefined.
51 * @param{HTMLElement} button
52 * @param{?string} label
53 * @param{Function} clickHandler
56 MessageWindowImpl
.prototype.initButton_
=
57 function(button
, label
, clickHandler
) {
59 button
.innerText
= label
;
60 button
.addEventListener('click', clickHandler
, false);
62 button
.hidden
= !Boolean(label
);
66 * Event-handler callback, invoked when the parent window supplies the
72 MessageWindowImpl
.prototype.onMessage_ = function(event
) {
73 switch (event
.data
['command']) {
75 // Validate the message.
76 var messageId
= /** @type {number} */ (event
.data
['id']);
77 var title
= /** @type {string} */ (event
.data
['title']);
78 var message
= /** @type {string} */ (event
.data
['message']);
79 var infobox
= /** @type {string} */ (event
.data
['infobox']);
80 var buttonLabel
= /** @type {string} */ (event
.data
['buttonLabel']);
82 var cancelButtonLabel
= (event
.data
['cancelButtonLabel']);
83 var showSpinner
= /** @type {boolean} */ (event
.data
['showSpinner']);
84 if (typeof(messageId
) != 'number' ||
85 typeof(title
) != 'string' ||
86 typeof(message
) != 'string' ||
87 typeof(infobox
) != 'string' ||
88 typeof(buttonLabel
) != 'string' ||
89 typeof(showSpinner
) != 'boolean') {
90 console
.log('Bad show message:', event
.data
);
94 // Set the dialog text.
95 var button
= document
.getElementById('button-primary');
96 var cancelButton
= document
.getElementById('button-secondary');
97 var messageDiv
= document
.getElementById('message');
98 var infoboxDiv
= document
.getElementById('infobox');
100 document
.getElementById('title').innerText
= title
;
101 document
.querySelector('title').innerText
= title
;
102 messageDiv
.innerHTML
= message
;
105 messageDiv
.classList
.add('waiting');
106 messageDiv
.classList
.add('prominent');
109 infoboxDiv
.innerText
= infobox
;
111 infoboxDiv
.hidden
= true;
117 this.sendReply_
.bind(this, event
.source
, messageId
, 1));
122 this.sendReply_
.bind(this, event
.source
, messageId
, 0));
124 var buttonToFocus
= (cancelButtonLabel
) ? cancelButton
: button
;
125 buttonToFocus
.focus();
127 // Add a close handler in case the window is closed without clicking one
128 // of the buttons. This will send a 0 as the result.
129 // Note that when a button is pressed, this will result in sendReply_
130 // being called multiple times (once for the button, once for close).
131 chrome
.app
.window
.current().onClosed
.addListener(
132 this.sendReply_
.bind(this, event
.source
, messageId
, 0));
134 base
.resizeWindowToContent(true);
135 chrome
.app
.window
.current().show();
138 case 'update_message':
139 var message
= /** @type {string} */ (event
.data
['message']);
140 if (typeof(message
) != 'string') {
141 console
.log('Bad update_message message:', event
.data
);
145 var messageDiv
= document
.getElementById('message');
146 messageDiv
.innerText
= message
;
148 base
.resizeWindowToContent(true);
152 console
.error('Unexpected message:', event
.data
);
156 var messageWindow
= new MessageWindowImpl();