-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindowManager.js
174 lines (155 loc) · 6.09 KB
/
windowManager.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Created on 27/02/2017.
// (C) Ironfly Technologies 2016
/**
* windowManager
* @module
*/
function createWindowManager () {
var _worker = null;
var _nextId = 0;
var _counter = 0;
var _childWindows = {};
var _isPrimary = !window.opener;
var _workerListeners = {};
var _parentWindowListeners = {};
var _onWindowClosed = null;
var _windowId = _isPrimary ? 'parent' : null;
var _origin = window.location.origin;
init();
function openWindow () {
if (!_isPrimary) { return null; }
var windowId = 'child_' + (++_nextId);
var newWindow = window.open('.', windowId, 'width=600,height=400');
var messageChannel = new MessageChannel();
_childWindows[windowId] = { window: newWindow, channel: messageChannel, listeners: {} };
sendToWorker('addWindow', { window: windowId }, [messageChannel.port1]);
onChildWindowMessage(windowId, 'ready', function () {
sendToChildWindow(windowId, 'messagePort', { window: windowId }, [messageChannel.port2]);
});
newWindow.addEventListener('unload', function () {
delete _childWindows[windowId];
if (_onWindowClosed) { _onWindowClosed(windowId);}
});
// setupChildWindowListener(windowId);
var interval = setInterval(function () {
if (_childWindows[windowId]) {
sendToWorker('toChild', { window: windowId, message: _windowId + ' | my counter is ' + ++_counter });
}
else {
clearInterval(interval);
}
}, 5000);
return windowId;
}
function onWindowClosed (callback) {
_onWindowClosed = callback;
}
function closeWindow (windowId) {
if (_childWindows[windowId]) {
_childWindows[windowId].window.close();
delete _childWindows[windowId];
sendToWorker('removeWindow', { window: windowId });
}
}
function closeAllWindows () {
var keys = Object.keys(_childWindows);
for (var i = 0; i < keys.length; i++) {
var windowId = keys[i];
closeWindow(windowId);
}
}
function init () {
if (startsWith(window.name, 'child_')) {
_windowId = window.name;
}
_worker = new Worker('worker.js');
sendToWorker('init', { primary: _isPrimary });
setupWorkerListener();
setupWindowListener();
onParentWindowMessage('messagePort', function (data, ports) {
// pass the port on to the worker. Theoretically, the child window worker can now talk to the parent window worker directly.
sendToWorker('parentPort', {}, ports);
});
onWorkerMessage('fromChild', function (data) {
console.log('WINDOW | fromChild | ' + data.window + ' | ' + data.message);
});
onWorkerMessage('fromParent', function (data) {
console.log('WINDOW | fromParent | ' + data.message);
});
if (window.opener) { sendToParentWindow('ready'); }
if (!_isPrimary) {
setInterval(function () {
if (_windowId) {
sendToWorker('toParent', { window: _windowId, message: _windowId + ' | my counter is ' + (++_counter) });
}
}, 5000);
}
}
function sendToWorker (type, data, transfer) {
console.log('WINDOW | sendToWorker | exists: ' + !!_worker + ' | ' + type);
if (_worker) {
_worker.postMessage({ type: type, data: data }, transfer);
}
}
function sendToChildWindow (windowId, type, data, transfer) {
console.log('WINDOW | sendToChildWindow | ' + windowId + ' | exists: ' + !!_childWindows[windowId] + ' | ' + type);
if (_childWindows[windowId]) {
_childWindows[windowId].window.postMessage({ type: type, source: _windowId, data: data }, window.location.origin, transfer);
}
}
function sendToParentWindow (type, data, transfer) {
console.log('WINDOW | sendToParentWindow | exists: ' + !!window.opener + ' | ' + type);
if (window.opener) {
window.opener.postMessage({ type: type, source: _windowId, data: data }, window.location.origin, transfer);
}
}
function onWorkerMessage (type, callback) {
_workerListeners[type] = callback;
}
function onChildWindowMessage (windowId, type, callback) {
if (_childWindows[windowId]) {
_childWindows[windowId].listeners[type] = callback;
}
}
function onParentWindowMessage (type, callback) {
if (window.opener) {
_parentWindowListeners[type] = callback;
}
}
function setupWorkerListener () {
if (_worker) {
_worker.addEventListener('message', function (e) {
if (_workerListeners[e.data.type]) {
_workerListeners[e.data.type](e.data.data, e.ports);
}
});
}
}
function setupWindowListener () {
window.addEventListener('message', function (e) {
if (!_isPrimary && e.data.source === 'parent') {
if (_parentWindowListeners[e.data.type]) {
_parentWindowListeners[e.data.type](e.data.data, e.ports);
}
}
else if (_isPrimary && _childWindows[e.data.source]) {
if (_childWindows[e.data.source].listeners[e.data.type]) {
_childWindows[e.data.source].listeners[e.data.type](e.data.data, e.ports);
}
}
});
if (_isPrimary) {
window.addEventListener('unload', closeAllWindows);
}
}
function startsWith (str, checkStr) {
if (!str || !checkStr) { return false; }
return str.slice(0, checkStr.length) === checkStr;
}
return {
openWindow: openWindow,
onWindowClosed: onWindowClosed,
closeWindow: closeWindow,
closeAllWindows: closeAllWindows
};
}