1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __rest = (this && this.__rest) || function (s, e) {
|
14 | var t = {};
|
15 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
16 | t[p] = s[p];
|
17 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
18 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
19 | t[p[i]] = s[p[i]];
|
20 | return t;
|
21 | };
|
22 | var __importStar = (this && this.__importStar) || function (mod) {
|
23 | if (mod && mod.__esModule) return mod;
|
24 | var result = {};
|
25 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
26 | result["default"] = mod;
|
27 | return result;
|
28 | };
|
29 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
30 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
31 | };
|
32 | Object.defineProperty(exports, "__esModule", { value: true });
|
33 | var react_1 = __importStar(require("react"));
|
34 | var react_compose_1 = __importDefault(require("@shopify/react-compose"));
|
35 | var polaris_1 = require("@shopify/polaris");
|
36 | var app_bridge_1 = require("@shopify/app-bridge");
|
37 | var actions_1 = require("@shopify/app-bridge/actions");
|
38 | var appUrl_1 = require("./utilities/appUrl");
|
39 | var error_1 = require("./utilities/error");
|
40 | var modal_1 = require("../store/reducers/embeddedApp/modal");
|
41 | var withFeature_1 = __importDefault(require("../withFeature"));
|
42 | var Frame_1 = __importDefault(require("../Frame"));
|
43 | var HostProvider_1 = require("../HostProvider");
|
44 | var style = {
|
45 | border: 'none',
|
46 | width: '100%',
|
47 | flex: '1',
|
48 | display: 'flex',
|
49 | };
|
50 | var DEFAULT_IFRAME_CONTENT_HEIGHT = 400;
|
51 | var SMALL_IFRAME_CONTENT_HEIGHT = 150;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function Modal(props) {
|
59 | var hostContext = react_1.useContext(HostProvider_1.HostContext);
|
60 | if (!hostContext) {
|
61 | error_1.throwMissingHostProvider();
|
62 | return null;
|
63 | }
|
64 | var app = hostContext.app, config = hostContext.config;
|
65 | var _a = props.actions, close = _a.close, clickFooterButton = _a.clickFooterButton, _b = props.store, _c = _b.content, content = _c === void 0 ? null : _c, height = _b.height, id = _b.id, location = _b.location, open = _b.open, primaryAction = _b.primaryAction, secondaryActions = _b.secondaryActions, size = _b.size, _d = _b.title, title = _d === void 0 ? '' : _d, extraProps = __rest(props, ["actions", "store"]);
|
66 | var modalStyles = __assign({}, style, { height: (height || getIframeHeight(size)) + "px" });
|
67 | var url = react_1.useMemo(function () {
|
68 | if (!location) {
|
69 | return;
|
70 | }
|
71 | var apiKey = config.apiKey, handle = config.handle, url = config.url;
|
72 | return appUrl_1.buildAppUrl({ handle: handle, apiKey: apiKey, url: url, pathname: location, search: takeSearch(location) });
|
73 | }, [location]);
|
74 |
|
75 |
|
76 | if (!open) {
|
77 | return null;
|
78 | }
|
79 | var frameContent = url ? (react_1.default.createElement(Frame_1.default, __assign({}, extraProps, { config: config, style: modalStyles, context: app_bridge_1.Context.Modal, app: app, title: title, url: url.href, onUrlChange: updateIframeUrl }))) : (content);
|
80 | var isLarge = size === actions_1.Modal.Size.Large;
|
81 | var isSectioned = url === undefined;
|
82 | var polarisPrimaryAction = primaryAction ? createButton(primaryAction) : undefined;
|
83 | var polarisSecondaryActions = modalSecondaryActions(secondaryActions);
|
84 | function handleClose() {
|
85 | close({ id: id });
|
86 | }
|
87 | return (react_1.default.createElement(polaris_1.Modal, { large: isLarge, title: title, open: open, onClose: handleClose, primaryAction: polarisPrimaryAction, secondaryActions: polarisSecondaryActions, sectioned: isSectioned }, frameContent));
|
88 | function createButton(button) {
|
89 | var label = button.label, id = button.id, disabled = button.disabled, style = button.style, loading = button.loading;
|
90 | var onAction = function () { return clickFooterButton(id); };
|
91 | return {
|
92 | content: label,
|
93 | disabled: disabled,
|
94 | destructive: style === actions_1.Button.Style.Danger,
|
95 | onAction: onAction,
|
96 | loading: loading,
|
97 | };
|
98 | }
|
99 | function modalSecondaryActions(actions) {
|
100 | if (!Array.isArray(actions) || !actions.length) {
|
101 | return;
|
102 | }
|
103 | return actions.map(createButton);
|
104 | }
|
105 | }
|
106 | exports.Modal = Modal;
|
107 | function getIframeHeight(size) {
|
108 | var isFullScreen = size === actions_1.Modal.Size.Full;
|
109 | if (isFullScreen) {
|
110 | return;
|
111 | }
|
112 | if (size === actions_1.Modal.Size.Small) {
|
113 | return SMALL_IFRAME_CONTENT_HEIGHT;
|
114 | }
|
115 | return DEFAULT_IFRAME_CONTENT_HEIGHT;
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 | function takeSearch(url) {
|
121 | var match = url.match(/\?[^#]+/);
|
122 | if (match === null) {
|
123 | return undefined;
|
124 | }
|
125 | return match[0];
|
126 | }
|
127 | function updateIframeUrl(iframe, newUrl) {
|
128 | if (!iframe || !iframe.contentWindow) {
|
129 | return;
|
130 | }
|
131 | iframe.contentWindow.location.replace(newUrl);
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | exports.default = react_compose_1.default(withFeature_1.default(modal_1.feature))(Modal);
|