-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6.js
executable file
·223 lines (192 loc) · 5.98 KB
/
es6.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"use strict";
/**
* steamer-cross
* github: https://github.com/SteamerTeam/steamer-cross
* npm: https://www.npmjs.com/package/steamer-cross
* version: 0.1.0
* date: 2016.08.06
*/
/**
* [Cross]
* @param {String} projectName [project name]
* @param {String} relationName [parent|window|iframe]
*/
function Cross(projectName, relationName) {
// encrypted key
this.className = "__SteamerCrossV0.1.0__";
// target iframes/windows storage
this.targets = {};
// listen callbacks
this.listeners = [];
// project name
this.projectName = projectName || '';
// current iframe/window name
this.relationName = relationName || 'parent';
this.init();
return this;
}
/**
* [check postMessage]
*/
Cross.prototype.init = function() {
if (!window.postMessage) {
throw new Error('"steamer-cross: your browser does not support postMessage"');
}
this._listen();
};
/**
* [register target and window]
* @param {String} targetName [target iframe/window name]
* @param {Window} target [window object]
* @return {Object} [this]
*/
Cross.prototype.register = function (targetName, target) {
// projectName|targetName
let key = this.projectName + '|' + targetName;
if (!this.targets.hasOwnProperty(key)) {
let t = {
target: target
};
this.targets[key] = t;
}
return this;
};
/**
* [unreigster targets]
* @param {String} targetName [target iframe/window name]
* @return {Object} [this]
*/
Cross.prototype.unregister = function(targetName = null) {
setTimeout(() => {
if (this.targets.hasOwnProperty(targetName)) {
delete this.targets[targetName];
}
// targetName = null, clear all targets
else if (!targetName) {
this.targets = {};
}
}, 0);
return this;
};
/**
* [encrypt message]
* @param {String} projectTargetName [projectName + targetName]
* @param {String} msg [msg]
* @param {String} direction [ptc|ctp ptc => parent to children, ctp => children to parent]
* @param {String} destinationName [destination target name]
* @return {String} [encrypted message]
*/
Cross.prototype.encryptMsg = function(projectTargetName, msg, direction = 'ptc', destinationName = '') {
destinationName = (destinationName) ? ('|' + destinationName) : '';
// projectName|targetName|(ptc|ctp)(|destinationTargetName)|className|msg
return projectTargetName + '|' + direction + destinationName + this.className + msg;
};
/**
* [broadcast message all iframe/window]
* @param {String} msg [message]
* @param {String} direction [ptc|ctp ptc => parent to children, ctp => children to parent]
* @return {Object} [this]
*/
// ptc => parent to children ctp => children to parent
Cross.prototype.broadcast = function (msg, direction = 'ptc', ignoreTargetName = '') {
setTimeout(() => {
// current window is parent, start broadcasting
if (this.relationName === 'parent') {
// prevent the broadcasting message sent by child sending back to this child again
let ignoreKey = this.projectName + '|' + ignoreTargetName;
for (let key in this.targets) {
if (key !== ignoreKey) {
let targetUrl = this.targets[key].target.location.href;
this.targets[key].target.postMessage(this.encryptMsg(key, msg, direction), targetUrl);
}
}
}
// if not parent, send back to parent first, then parent will handle it
else {
this.send('parent', msg, 'ctp', 'all|' + this.relationName);
}
}, 0);
return this;
};
/**
* [send message to specific iframe/window]
* @param {String} targetName [target iframe/window name]
* @param {String} msg [message]
* @param {String} direction [ptc|ctp ptc => parent to children, ctp => children to parent]
* @param {String} destinationName [destination target name]
* @return {Object} [this]
*/
Cross.prototype.send = function (targetName, msg, direction = 'ptc', destinationName = '') {
setTimeout(() => {
let key = this.projectName + '|' + targetName;
if (this.targets.hasOwnProperty(key)) {
let targetUrl = this.targets[key].target.location.href;
this.targets[key].target.postMessage(this.encryptMsg(key, msg, direction, destinationName), targetUrl);
}
// if current window is not parent, send back to parent first to handle it
else if (this.relationName !== 'parent') {
this.send('parent', msg, 'ctp', targetName);
}
else {
throw new Error("steamer-cross: targetName not exists");
}
}, 0);
return this;
};
/**
* [listen message]
* @param {Function} callback [callback]
* @return {Object} [this]
*/
Cross.prototype.listen = function (callback) {
if (typeof callback === 'function') {
this.listeners.push(callback);
}
return this;
};
Cross.prototype._triggerListeners = function(data, e) {
for (var i = 0, len = this.listeners.length; i < len; i++) {
this.listeners[i](data, e);
}
};
Cross.prototype._listen = function() {
let listenFunc = (e) => {
this.register('parent', e.source);
let msg = e.data;
// filter msg from other projects
if (!~msg.indexOf(this.projectName + '|' + this.relationName)) {
return this;
}
msg = msg.split(this.className);
let info = msg[0] || '',
data = msg[1] || '';
info = info.split('|');
// send from child to parent
// parent handle message sent from children here
if (this.relationName === 'parent' && info.length >= 4 && info[2] == 'ctp') {
if (info[3] !== 'all') {
// child A send message to child B
// info[3] => child B targetName
this.send(info[3], data);
}
// all => broadcast
else if (info[3] === 'all') {
// parent will also receive broadcasting message
this._triggerListeners(data, e);
// parent broadcast the message to other children
// info[4] => the child which broadcast message
this.broadcast(data, 'ptc', info[4]);
}
}
else {
this._triggerListeners(data, e);
}
};
if (window.addEventListener) {
window.addEventListener('message', listenFunc, false);
}
else {
window.attachEvent('onmessage', listenFunc);
}
};
exports.default = Cross;