-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmessage_window.js
156 lines (137 loc) · 4.8 KB
/
message_window.js
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
// 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';
/**
* @constructor
*/
function MessageWindowImpl() {
/**
* Used to prevent multiple responses due to the closeWindow handler.
*
* @type {boolean}
* @private
*/
this.sentReply_ = false;
window.addEventListener('message', this.onMessage_.bind(this), false);
};
/**
* @param {Window} parentWindow The id of the window that showed the message.
* @param {number} messageId The identifier of the message, as supplied by the
* parent.
* @param {number} result 0 if window was closed without pressing a button;
* otherwise the index of the button pressed (e.g., 1 = primary).
* @private
*/
MessageWindowImpl.prototype.sendReply_ = function(
parentWindow, messageId, result) {
// Only forward the first reply that we receive.
if (!this.sentReply_) {
var message = {
command: 'messageWindowResult',
id: messageId,
result: result
};
parentWindow.postMessage(message, '*');
this.sentReply_ = true;
} else {
// Make sure that the reply we're ignoring is from the window close.
base.debug.assert(result == 0);
}
};
/**
* Initializes the button with the label and the click handler.
* Hides the button if the label is null or undefined.
*
* @param{HTMLElement} button
* @param{?string} label
* @param{Function} clickHandler
* @private
*/
MessageWindowImpl.prototype.initButton_ =
function(button, label, clickHandler) {
if (label) {
button.innerText = label;
button.addEventListener('click', clickHandler, false);
}
button.hidden = !Boolean(label);
};
/**
* Event-handler callback, invoked when the parent window supplies the
* message content.
*
* @param{Event} event
* @private
*/
MessageWindowImpl.prototype.onMessage_ = function(event) {
switch (event.data['command']) {
case 'show':
// Validate the message.
var messageId = /** @type {number} */ (event.data['id']);
var title = /** @type {string} */ (event.data['title']);
var message = /** @type {string} */ (event.data['message']);
var infobox = /** @type {string} */ (event.data['infobox']);
var buttonLabel = /** @type {string} */ (event.data['buttonLabel']);
/** @type {string} */
var cancelButtonLabel = (event.data['cancelButtonLabel']);
var showSpinner = /** @type {boolean} */ (event.data['showSpinner']);
if (typeof(messageId) != 'number' ||
typeof(title) != 'string' ||
typeof(message) != 'string' ||
typeof(infobox) != 'string' ||
typeof(buttonLabel) != 'string' ||
typeof(showSpinner) != 'boolean') {
console.log('Bad show message:', event.data);
break;
}
// Set the dialog text.
var button = document.getElementById('button-primary');
var cancelButton = document.getElementById('button-secondary');
var messageDiv = document.getElementById('message');
var infoboxDiv = document.getElementById('infobox');
document.getElementById('title').innerText = title;
document.querySelector('title').innerText = title;
messageDiv.innerHTML = message;
if (showSpinner) {
messageDiv.classList.add('waiting');
messageDiv.classList.add('prominent');
}
if (infobox != '') {
infoboxDiv.innerText = infobox;
} else {
infoboxDiv.hidden = true;
}
this.initButton_(
button,
buttonLabel,
this.sendReply_.bind(this, event.source, messageId, 1));
this.initButton_(
cancelButton,
cancelButtonLabel,
this.sendReply_.bind(this, event.source, messageId, 0));
var buttonToFocus = (cancelButtonLabel) ? cancelButton : button;
buttonToFocus.focus();
// Add a close handler in case the window is closed without clicking one
// of the buttons. This will send a 0 as the result.
// Note that when a button is pressed, this will result in sendReply_
// being called multiple times (once for the button, once for close).
chrome.app.window.current().onClosed.addListener(
this.sendReply_.bind(this, event.source, messageId, 0));
base.resizeWindowToContent();
chrome.app.window.current().show();
break;
case 'update_message':
var message = /** @type {string} */ (event.data['message']);
if (typeof(message) != 'string') {
console.log('Bad update_message message:', event.data);
break;
}
var messageDiv = document.getElementById('message');
messageDiv.innerText = message;
base.resizeWindowToContent();
break;
default:
console.error('Unexpected message:', event.data);
}
};
var messageWindow = new MessageWindowImpl();