-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnhancedPostMessage.js
More file actions
273 lines (240 loc) · 9.3 KB
/
Copy pathEnhancedPostMessage.js
File metadata and controls
273 lines (240 loc) · 9.3 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*!
* EnhancedPostMessage
*
* A better postMessage. Allow named events, triggers and callback listeners
*
* @version 0.5.0
* @author Adam Heim - https://github.com/truckingsim
* @link https://github.com/truckingsim/EnhancedPostMessage
* @copyright 2015 Adam Heim
* @license Released under the MIT license.
*
* Contributors:
*
* Last build: 2015-03-18 11:23:14 AM EDT
*/
(function (window, undefined) {
var initialized = false;
var PrivateEnhancedPostMessage = {
_defaults: { stringify: false },
_options: {},
_events: {},
_listeners: {},
_sources: {},
initialize: function(options){
var that = this;
if(options) {
this.loadOptionsFromObject(options);
}
if(!initialized){
this._options = extend({}, this._defaults, options);
// Setup sole listener that will listen for all events.
window.addEventListener('message', function(e){
that._handleEventListener(e);
});
initialized = true;
} else {
if(options) {
if (options.sources) {
this._options.sources = extend({}, this._options.sources || (this._options.sources = {}), options.sources);
}
if (options.events) {
this._options.events = extend({}, this._options.events || (this._options.events = {}), options.events);
}
if (options.listeners) {
this._options.listeners = extend({}, this._options.listeners || (this._options.listeners = {}), options.listeners);
}
if (options.stringify) {
this._options.stringify = options.stringify;
}
}
}
},
/**
* @param {Object} options
*/
loadOptionsFromObject: function(options){
if(options.sources && keys(options.sources).length){
for(var source in options.sources){
if(options.sources.hasOwnProperty(source)){
this._addSource(source, options.sources[source]);
}
}
}
// Check for events and load them in
if(options.events && keys(options.events).length){
for(var e in options.events){
if(options.events.hasOwnProperty(e)){
this._addEvent(e, options.events[e]);
}
}
}
// Check for any listeners and add them
if(options.listeners && keys(options.listeners).length){
for(var l in options.listeners){
if(options.listeners.hasOwnProperty(l)){
this._addListener(l, options.listeners[l]);
}
}
}
},
triggerEvent: function(eventName, sourceName, data){
var sourceWindow, e;
if(sourceName === 'parent'){
sourceWindow = window.parent;
} else {
// Initially assume stored source is a window object (which is the return value of window.open)
sourceWindow = this._sources[sourceName];
// If we see contentWindow we've been passed an iframe dom element, swap out for actual window
if ('contentWindow' in sourceWindow)
sourceWindow = sourceWindow.contentWindow;
}
if(!sourceWindow){
this.log('No source by that name', true);
return false;
}
// Allow any event but if defined
e = this._events[eventName];
if(e && e !== true) {
if (typeof e === 'function') {
data = e(data);
} else if (e === false) {
this.log('Event value cannot be false', true);
return false;
} else {
data = e;
}
}
var objToSend = {
eventName: eventName,
data: data
};
sourceWindow.postMessage(this._options.stringify ? JSON.stringify(objToSend) : objToSend, '*');
},
_addEvent: function (key, value) {
this._events[key] = value;
},
_addListener: function (key, value) {
this._listeners[key] = value;
},
_addSource: function (key, value) {
this._sources[key] = value;
},
_setOptions: function (options) {
},
_setOption: function (name, value) {
this._options[name] = value;
},
_handleEventListener: function(e){
// If the data was stringified we need to parse it, this option should match on both sides.
var data = e.data;
if(this._options.stringify && typeof e.data === 'string'){
data = JSON.parse(e.data);
}
// If there is a listener for this event name, call it and pass the data
if(this._listeners[data.eventName]){
this._listeners[data.eventName](data.data);
}
}
};
PrivateEnhancedPostMessage.log = function (message, error) {
message = message instanceof Array ? message : [message];
if(window.console && this._options.debug){
(error ? console.error : console.log).apply(console, message);
}
};
// Some sites have problems with Object.keys, this is to get around that, backward support
var extend = function (obj) {
var source, prop;
for (var i = 1, length = arguments.length; i < length; i++) {
source = arguments[i];
for (prop in source) {
if (Object.prototype.hasOwnProperty.call(source, prop)) {
obj[prop] = source[prop];
}
}
}
return obj;
};
// Sometimes Object.keys doesn't work, so put in shiv just in case.
var keys = function(obj){
if(Object.keys) return Object.keys(obj);
var keys = [];
for(var key in obj){
if(obj.hasOwnProperty(key)){
keys.push(key);
}
}
return keys;
};
var PublicInstance = function (options) {
if (arguments.length > 1) {
// Handle event triggers
if(arguments[0] === 'trigger' && arguments.length >= 3){
PrivateEnhancedPostMessage.triggerEvent(arguments[1], arguments[2], arguments[3]);
}
if(arguments[0] === 'addSource' && arguments.length >= 3){
PrivateEnhancedPostMessage._addSource(arguments[1], arguments[2]);
}
if(arguments[0] === 'addEvent' && arguments.length >= 3){
PrivateEnhancedPostMessage._addEvent(arguments[1], arguments[2]);
}
if(arguments[0] === 'addListener' && arguments.length >= 3){
PrivateEnhancedPostMessage._addListener(arguments[1], arguments[2]);
}
} else if (arguments.length == 1) {
PrivateEnhancedPostMessage.initialize(options);
} else {
}
return PublicInstance;
};
/**
* Triggers event, event name can be anything, as long as the name can be used for a listener.
* @returns {PublicInstance}
*/
PublicInstance.trigger = function(){
if(arguments.length >= 2){
PrivateEnhancedPostMessage.triggerEvent(arguments[0], arguments[1], arguments[2]);
} else {
PrivateEnhancedPostMessage.log('Invalid trigger, need both an event name and a source name', true);
}
return PublicInstance;
};
/**
* Programatically adds a source to the current instance of EnhancedPostMessage.
* @param {String} key - name of source
* @param {HTMLElement} value - a single dom element to use as a source
* @returns {PublicInstance}
*/
PublicInstance.addSource = function(key, value){
// Initialize if hasn't been already
PrivateEnhancedPostMessage.initialize();
PrivateEnhancedPostMessage._addSource(key, value);
return PublicInstance;
};
/**
* Programatically add an event to the current instance of EnhancedPostMessage
* @param {String} key - name of the event
* @param {Object|String|True|Function|Number} value - pretty much anything but false
* @returns {PublicInstance}
*/
PublicInstance.addEvent = function(key, value){
// Initialize if hasn't been already
PrivateEnhancedPostMessage.initialize();
PrivateEnhancedPostMessage._addEvent(key, value);
return PublicInstance;
};
/**
* Programatically add a listener to the current instance of EnhancedPostMessage
* @param key
* @param value
* @returns {PublicInstance}
*/
PublicInstance.addListener = function(key, value){
// Initialize if hasn't been already
PrivateEnhancedPostMessage.initialize();
PrivateEnhancedPostMessage._addListener(key, value);
return PublicInstance;
};
window.EnhancedPostMessage = PublicInstance;
})(window);