-
Notifications
You must be signed in to change notification settings - Fork 1
/
postbox.jquery.js
376 lines (293 loc) · 9.92 KB
/
postbox.jquery.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("jquery"));
else if(typeof define === 'function' && define.amd)
define(["jquery"], factory);
else if(typeof exports === 'object')
exports["Postbox"] = factory(require("jquery"));
else
root["Postbox"] = factory(root["jquery"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var MESSAGE = 'message';
var _messageId = 0;
var $ = __webpack_require__(1);
function resolveOrigin(url) {
var a = document.createElement('a');
a.href = url;
return a.origin || a.protocol + '//' + a.hostname;
}
function resolveValue(model, property) {
var unwrappedContext = typeof model[property] === 'function' ? model[property]() : model[property];
var defer = $.Deferred();
defer.resolve(unwrappedContext);
return defer.promise();
}
function messageUID() {
return ++_messageId;
}
function distrust(e, origin) {
if (e.origin !== origin) return false;
if (_typeof(e.data) === 'object') return false;
if (!('type' in e.data)) return false;
}
var ParentAPI = function () {
function ParentAPI(info) {
var _this = this;
_classCallCheck(this, ParentAPI);
this.parent = info.parent;
this.child = info.child;
this.childOrigin = info.childOrigin;
this.frame = info.frame;
this.events = {};
this.listener = function (e) {
if (distrust(e, _this.childOrigin)) return;
if (e.data.type === 'emit') {
var _e$data$value = e.data.value,
name = _e$data$value.name,
data = _e$data$value.data;
if (name in _this.events) {
_this.events[name].call(_this, data);
}
}
};
$(this.parent).on(MESSAGE, this.listener);
}
/**
* get iframe model property
* @param property
* @returns {Promise}
*/
_createClass(ParentAPI, [{
key: 'get',
value: function get(property) {
var _this2 = this;
var defer = $.Deferred();
var uid = messageUID();
var transact = function transact(e) {
if (e.data.type === 'reply' && e.data.uid === uid) {
$(_this2.parent).off(MESSAGE, transact);
defer.resolve(e.data.value);
}
};
$(this.parent).on(MESSAGE, transact);
this.child.postMessage({
type: 'request',
property: property,
uid: uid
}, this.childOrigin);
return defer.promise();
}
/**
* invoke iframe model function property
* @param property
* @param data
*/
}, {
key: 'call',
value: function call(property, data) {
this.child.postMessage({
type: 'call',
property: property,
data: data
}, this.childOrigin);
}
/**
* add iframe event handle
* @param name
* @param callback
*/
}, {
key: 'on',
value: function on(name, callback) {
this.events[name] = callback;
}
/**
* destroy iframe
*/
}, {
key: 'destroy',
value: function destroy() {
$(this.parent).off(MESSAGE, this.listener);
this.frame.parentNode.removeChild(this.frame);
}
}]);
return ParentAPI;
}();
var ChildAPI = function () {
function ChildAPI(info) {
var _this3 = this;
_classCallCheck(this, ChildAPI);
this.parent = info.parent;
this.parentOrigin = info.parentOrigin;
this.child = info.child;
this.model = info.model;
$(this.child).on(MESSAGE, function (e) {
if (distrust(e, _this3.parentOrigin)) return;
var _e$data = e.data,
type = _e$data.type,
property = _e$data.property,
uid = _e$data.uid,
data = _e$data.data;
if (type === 'call') {
if (property in _this3.model && typeof _this3.model[property] === 'function') _this3.model[property].call(_this3, data);
} else if (type === 'request') {
resolveValue(_this3.model, property).then(function (value) {
e.source.postMessage({
type: 'reply',
value: value,
uid: uid
}, e.origin);
});
}
}, false);
}
/**
* iframe emit event and data to parent
* @param name
* @param data
*/
_createClass(ChildAPI, [{
key: 'emit',
value: function emit(name, data) {
this.parent.postMessage({
type: 'emit',
value: { name: name, data: data }
}, this.parentOrigin);
}
}]);
return ChildAPI;
}();
var Client = function () {
/**
*
* @param model iframe model
* @returns {*}
*/
function Client(model) {
_classCallCheck(this, Client);
this.child = window;
this.parent = this.child.parent;
this.model = model;
return this.sendHandshakeReply();
}
_createClass(Client, [{
key: 'sendHandshakeReply',
value: function sendHandshakeReply() {
var _this4 = this;
var defer = $.Deferred();
var shake = function shake(e) {
if (e.data.type === 'handshake') {
$(_this4.child).off(MESSAGE, shake);
_this4.parentOrigin = e.origin;
e.source.postMessage({
type: 'handshake-reply'
}, e.origin);
_extends(_this4.model, e.data.model);
defer.resolve(new ChildAPI(_this4));
} else defer.reject('Handshake reply failed.');
};
$(this.child).on(MESSAGE, shake);
return defer.promise();
}
}]);
return Client;
}();
var Postbox = function () {
/**
*
* @param options
* @param options.container element to inject iframe into
* @param options.url iframe's url
* @param options.model model send to iframe
* @returns {*}
*/
function Postbox(options) {
_classCallCheck(this, Postbox);
var container = options.container,
url = options.url,
model = options.model;
this.parent = window;
this.frame = document.createElement('iframe');
(container || document.body).appendChild(this.frame);
this.child = this.frame.contentWindow || this.frame.contentDocument.parentWindow;
this.model = model || {};
return this.sendHandshake(url);
}
_createClass(Postbox, [{
key: 'sendHandshake',
value: function sendHandshake(url) {
var _this5 = this;
var childOrigin = resolveOrigin(url);
var defer = $.Deferred();
var reply = function reply(e) {
// receive handshake reply from iframe
if (e.data.type === 'handshake-reply') {
$(_this5.parent).off(MESSAGE, reply);
_this5.childOrigin = e.origin;
defer.resolve(new ParentAPI(_this5));
} else defer.reject('Handshake failed');
};
$(this.parent).on(MESSAGE, reply);
// send handshake to iframe
var loaded = function loaded(e) {
_this5.child.postMessage({
type: 'handshake',
model: _this5.model
}, childOrigin);
};
this.frame.onload = loaded;
this.frame.src = url;
return defer.promise();
}
}]);
return Postbox;
}();
Postbox.Client = Client;
module.exports = Postbox;
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
/***/ }
/******/ ])
});
;