@web3auth/ws-embed
Version:
Embed script
65 lines (61 loc) • 1.98 kB
JavaScript
'use strict';
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var baseControllers = require('@toruslabs/base-controllers');
var auth = require('@web3auth/auth');
class PopupHandler extends auth.SafeEventEmitter {
constructor({
url,
target,
features,
timeout = 30000
}) {
super();
_defineProperty(this, "url", void 0);
_defineProperty(this, "target", void 0);
_defineProperty(this, "features", void 0);
_defineProperty(this, "window", void 0);
_defineProperty(this, "windowTimer", void 0);
_defineProperty(this, "iClosedWindow", void 0);
_defineProperty(this, "timeout", void 0);
this.url = url;
this.target = target || "_blank";
this.features = features || baseControllers.getPopupFeatures(baseControllers.FEATURES_DEFAULT_POPUP_WINDOW);
this.window = undefined;
this.windowTimer = undefined;
this.iClosedWindow = false;
this.timeout = timeout;
this._setupTimer();
}
_setupTimer() {
this.windowTimer = Number(setInterval(() => {
if (this.window && this.window.closed) {
clearInterval(this.windowTimer);
setTimeout(() => {
if (!this.iClosedWindow) {
this.emit("close");
}
this.iClosedWindow = false;
this.window = undefined;
}, this.timeout);
}
if (this.window === undefined) clearInterval(this.windowTimer);
}, 500));
}
open() {
var _this$window;
this.window = window.open(this.url.href, this.target, this.features);
if ((_this$window = this.window) !== null && _this$window !== void 0 && _this$window.focus) this.window.focus();
}
close() {
this.iClosedWindow = true;
if (this.window) this.window.close();
}
redirect(locationReplaceOnRedirect) {
if (locationReplaceOnRedirect) {
window.location.replace(this.url.href);
} else {
window.location.href = this.url.href;
}
}
}
module.exports = PopupHandler;