UNPKG

@venly/connect

Version:

Venly Connect SDK

140 lines • 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PopupWindowAsync = void 0; var tslib_1 = require("tslib"); var Utils_1 = tslib_1.__importDefault(require("../utils/Utils")); var EventTypes_1 = require("../types/EventTypes"); var PopupWindow_1 = require("./PopupWindow"); var PopupWindowAsync = /** @class */ (function () { function PopupWindowAsync(url, target, features, useOverlay, correlationId, replace) { this.id = "id-" + Utils_1.default.uuidv4(); this.url = url; this.target = target; this.features = features; this.useOverlay = typeof useOverlay !== 'undefined' ? useOverlay : true; this.correlationID = correlationId; this.replace = replace; this.openOverlay(); } PopupWindowAsync.openNew = function (url, correlationID, options) { return tslib_1.__awaiter(this, void 0, void 0, function () { var mergedOptions, left, top, features, popupWindow; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: mergedOptions = Object.assign({ title: 'Venly Connect', w: 350, h: 700, useOverlay: true }, options); left = (screen.width / 2) - (mergedOptions.w / 2); top = (screen.height / 2) - (mergedOptions.h / 2); features = 'popup=true, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, ' + ("width=" + mergedOptions.w + ", height=" + mergedOptions.h + ", top=" + top + ", left=" + left); url = Utils_1.default.http().addRequestParams(url, { cid: correlationID }); popupWindow = new PopupWindowAsync(url, mergedOptions.title, features, mergedOptions.useOverlay, correlationID); return [4 /*yield*/, popupWindow.open()]; case 1: _a.sent(); return [2 /*return*/, popupWindow]; } }); }); }; PopupWindowAsync.prototype.open = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a; var _this = this; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _a = this; return [4 /*yield*/, new Promise(function (resolve) { _this.popupMountedListener = _this.createPopupMountedListener(_this.correlationID, resolve); window.addEventListener('message', _this.popupMountedListener); try { window.open(_this.url, _this.target, _this.features); } catch (e) { console.debug('popup open failed', e); } }).then(function (win) { _this.setCloseInterval(); return win; })]; case 1: _a.win = _b.sent(); return [2 /*return*/]; } }); }); }; PopupWindowAsync.prototype.createPopupMountedListener = function (correlationID, resolve) { var _this = this; return function (message) { if (Utils_1.default.messages().hasValidOrigin(message) && Utils_1.default.messages().isOfType(message, EventTypes_1.EventTypes.POPUP_MOUNTED)) { if (_this.popupMountedListener) { window.removeEventListener('message', _this.popupMountedListener); delete _this.popupMountedListener; } resolve(message.source); } }; }; PopupWindowAsync.prototype.setCloseInterval = function () { var _this = this; this.closeInterval = window.setInterval(function () { if (!_this.win || _this.win.closed) { _this.close(); } }, 100); }; PopupWindowAsync.prototype.clearCloseInterval = function () { window.clearInterval(this.closeInterval); }; PopupWindowAsync.prototype.close = function () { this.closePopup(); this.closeOverlay(); this.clearCloseInterval(); this.removePopupMountedListener(); }; PopupWindowAsync.prototype.closePopup = function () { if (this.win) { this.win.close(); } }; PopupWindowAsync.prototype.removePopupMountedListener = function () { if (this.popupMountedListener) { window.removeEventListener('message', this.popupMountedListener); delete this.popupMountedListener; } }; Object.defineProperty(PopupWindowAsync.prototype, "closed", { get: function () { if (this.win) { return this.win.closed; } else { return true; } }, enumerable: false, configurable: true }); PopupWindowAsync.prototype.focus = function () { if (this.win) { this.win.focus(); } }; PopupWindowAsync.prototype.closeOverlay = function () { PopupWindow_1.PopupWindow.closeOverlay(this.id, this.useOverlay); }; PopupWindowAsync.prototype.openOverlay = function () { PopupWindow_1.PopupWindow.openOverlay(this.id, this.useOverlay, this.focus, this.close); }; return PopupWindowAsync; }()); exports.PopupWindowAsync = PopupWindowAsync; //# sourceMappingURL=PopupWindowAsync.js.map