UNPKG

@aurigma/design-editor-iframe

Version:

Using this module you can embed Design Editor (a part of Customer's Canvas) to your page through the IFrame API.

248 lines 9.34 kB
var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; /** * @internal */ import { Json } from "@aurigma/design-atoms/Utils/Json"; import { Exception } from "@aurigma/design-atoms-model/Exception"; import { MessageType } from "./RpcTypes"; import { SUBSCRIBED_EVENT, SUBSCRIBE_REQUEST, DE_APP_SOURCE, IFRAME_LOADED_EVENT } from "./Consts"; var Server = /** @class */ (function () { function Server() { var _this = this; this._methods = new MethodsCollection(); this._pendingMessages = []; this._suscribers = []; this._subscribeRequestReceived = false; addEventListener("message", function (e) { return _this._handleMessage(e); }); this._processUrlHashParams(window.location.hash); this._sendLoadedEvent(); } Server.prototype._processUrlHashParams = function (hash) { var e_1, _a; var params = hash.substring(1).split('&'); if (params != null && params.length > 0) { try { for (var params_1 = __values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) { var param = params_1_1.value; var _b = __read(param.split('='), 2), name = _b[0], value = _b[1]; value = decodeURIComponent(value); if (name == 'targetOrigin') { this._subscriptionOrigin = value; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1); } finally { if (e_1) throw e_1.error; } } } }; Object.defineProperty(Server.prototype, "methods", { set: function (value) { var _this = this; this._methods.add(value); var messages = this._pendingMessages; this._pendingMessages = []; messages.forEach(function (msg) { return _this._handleMessage(msg); }); }, enumerable: true, configurable: true }); Server.prototype.notifySubscribers = function (event) { var e_2, _a; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } try { for (var _b = __values(this._suscribers), _c = _b.next(); !_c.done; _c = _b.next()) { var susbscriber = _c.value; this._sendMessage(susbscriber, { type: MessageType.Event, event: event, result: args, source: DE_APP_SOURCE }); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } }; Server.prototype._handleMessage = function (ev) { var _this = this; if (typeof ev.data !== "string") return; var parse = Json.parse.bind(Json); var incomingData; try { incomingData = parse(ev.data); } catch (ex) { return; } if (incomingData == null || typeof incomingData != "object") return; var procDescription = Json.parse(ev.data); if (typeof procDescription.id !== "number" || typeof procDescription.methodName !== "string" || !Array.isArray(procDescription.methodArgs) || procDescription.source !== DE_APP_SOURCE) return; //alien message if (procDescription.methodName === SUBSCRIBE_REQUEST) { var origin_1 = procDescription.methodArgs[0].origin; this._onSubscribeRequest(ev.source, origin_1); return; } if (this._methods == null) { this._pendingMessages.push(ev); return; } if (!this._methods.methodAvailable(procDescription.methodName)) { console.warn("Method " + procDescription.methodName + " not found!"); this._pendingMessages.push(ev); return; } var response = { type: MessageType.ProcedureResponse, id: procDescription.id }; var returnValue; try { returnValue = this._methods.apply(procDescription.methodName, procDescription.methodArgs); } catch (ex) { returnValue = Promise.reject(ex); } (returnValue instanceof Promise ? returnValue : Promise.resolve(returnValue)) .then(function (result) { response.success = true; response.result = result; response.source = DE_APP_SOURCE; }) .then(function () { return _this._sendMessage(ev.source, response); }) .catch(function (ex) { response.success = false; response.result = { msg: procDescription.methodName + " failed", exception: ex }; response.source = DE_APP_SOURCE; _this._sendMessage(ev.source, response); }); }; Server.prototype._onSubscribeRequest = function (source, origin) { if (!this._subscribeRequestReceived) { this._subscriptionSource = source; this._subscriptionOrigin = origin; this._suscribers.push(source); this._subscribeRequestReceived = true; } this._sendSubscribedEvent(); }; Server.prototype._sendMessage = function (sendTo, response) { sendTo.postMessage(Json.stringify(response), this._subscriptionOrigin); }; Server.prototype._sendLoadedEvent = function () { var _a; var data = { type: MessageType.Event, event: IFRAME_LOADED_EVENT, source: DE_APP_SOURCE }; parent.postMessage(Json.stringify(data), (_a = this._subscriptionOrigin) !== null && _a !== void 0 ? _a : "*"); }; Server.prototype._sendSubscribedEvent = function () { this._sendMessage(this._subscriptionSource, { type: MessageType.Event, event: SUBSCRIBED_EVENT, source: DE_APP_SOURCE }); }; return Server; }()); export { Server }; var MethodsCollection = /** @class */ (function () { function MethodsCollection() { this._objects = []; } MethodsCollection.prototype.add = function (objectWithMehods) { this._objects.push(objectWithMehods); }; MethodsCollection.prototype.methodAvailable = function (methodName) { var e_3, _a; try { for (var _b = __values(this._objects), _c = _b.next(); !_c.done; _c = _b.next()) { var o = _c.value; if (typeof o[methodName] === "function") return true; } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } return false; }; MethodsCollection.prototype.apply = function (methodName, args) { var e_4, _a; var targetMethod; try { for (var _b = __values(this._objects), _c = _b.next(); !_c.done; _c = _b.next()) { var o = _c.value; if (typeof o[methodName] === "function") targetMethod = o[methodName]; } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_4) throw e_4.error; } } if (targetMethod == null) throw new Exception("method " + methodName + " not found!"); return targetMethod.apply(null, args); }; return MethodsCollection; }()); //# sourceMappingURL=Server.js.map