UNPKG

3.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Error_1 = require("./actions/Error");
4var validator_1 = require("./actions/validator");
5var collection_1 = require("./util/collection");
6var env_1 = require("./util/env");
7var Context;
8(function (Context) {
9 Context["Modal"] = "Modal";
10 Context["Main"] = "Main";
11})(Context = exports.Context || (exports.Context = {}));
12/**
13 * Create a MessageTransport from an IFrame.
14 * @remarks
15 * Used on the host-side to create a postMessage MessageTransport.
16 * @beta
17 */
18function 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}
49exports.fromFrame = fromFrame;
50/**
51 * Create a MessageTransport from a parent window.
52 * @remarks
53 * Used on the client-side to create a postMessage MessageTransport.
54 * @internalremarks
55 * In frameless mode, message should be dispatched via SmartWebView.handleMessage instead of postMessage.
56 * @beta
57 */
58function 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}
92exports.fromWindow = fromWindow;