1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Error_1 = require("./actions/Error");
|
4 | var validator_1 = require("./actions/validator");
|
5 | var collection_1 = require("./util/collection");
|
6 | var env_1 = require("./util/env");
|
7 | var Context;
|
8 | (function (Context) {
|
9 | Context["Modal"] = "Modal";
|
10 | Context["Main"] = "Main";
|
11 | })(Context = exports.Context || (exports.Context = {}));
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function fromFrame(frame, localOrigin, context) {
|
19 | var handlers = [];
|
20 | if (typeof frame === 'undefined' || !frame.ownerDocument || !frame.ownerDocument.defaultView) {
|
21 | throw Error_1.fromAction('App frame is undefined', Error_1.AppActionType.WINDOW_UNDEFINED);
|
22 | }
|
23 | var parent = frame.ownerDocument.defaultView;
|
24 | parent.addEventListener('message', function (event) {
|
25 | if (event.origin !== localOrigin || !validator_1.isAppMessage(event)) {
|
26 | return;
|
27 | }
|
28 | for (var _i = 0, handlers_1 = handlers; _i < handlers_1.length; _i++) {
|
29 | var handler = handlers_1[_i];
|
30 | handler(event);
|
31 | }
|
32 | });
|
33 | return {
|
34 | context: context,
|
35 | localOrigin: localOrigin,
|
36 | frameWindow: frame.contentWindow,
|
37 | hostFrame: parent,
|
38 | dispatch: function (message) {
|
39 | var contentWindow = frame.contentWindow;
|
40 | if (contentWindow) {
|
41 | contentWindow.postMessage(message, localOrigin);
|
42 | }
|
43 | },
|
44 | subscribe: function (handler) {
|
45 | return collection_1.addAndRemoveFromCollection(handlers, handler);
|
46 | },
|
47 | };
|
48 | }
|
49 | exports.fromFrame = fromFrame;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function fromWindow(contentWindow, localOrigin) {
|
59 | var handlers = [];
|
60 | if (typeof window !== undefined) {
|
61 | window.addEventListener('message', function (event) {
|
62 | if ((window === contentWindow && !env_1.isFrameless) ||
|
63 | event.source !== contentWindow ||
|
64 | !(validator_1.isAppBridgeAction(event.data.payload) || validator_1.isAppMessage(event))) {
|
65 | return;
|
66 | }
|
67 | for (var _i = 0, handlers_2 = handlers; _i < handlers_2.length; _i++) {
|
68 | var handler = handlers_2[_i];
|
69 | handler(event);
|
70 | }
|
71 | });
|
72 | }
|
73 | return {
|
74 | localOrigin: localOrigin,
|
75 | hostFrame: contentWindow,
|
76 | dispatch: function (message) {
|
77 | if (!message.source || !message.source.shopOrigin) {
|
78 | return;
|
79 | }
|
80 | if (env_1.isFrameless && window && window.SmartWebView) {
|
81 | window.SmartWebView.handleMessage('frameless://fromClient', JSON.stringify(message));
|
82 | return;
|
83 | }
|
84 | var messageOrigin = "https://" + message.source.shopOrigin;
|
85 | contentWindow.postMessage(message, messageOrigin);
|
86 | },
|
87 | subscribe: function (handler) {
|
88 | return collection_1.addAndRemoveFromCollection(handlers, handler);
|
89 | },
|
90 | };
|
91 | }
|
92 | exports.fromWindow = fromWindow;
|