squarelink
Version:
Squarelink's Web3 library
94 lines • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var config_1 = require("./config");
var Iframe = /** @class */ (function () {
/**
* @param {string} url
*/
function Iframe(url, params) {
this.url = url;
this.params = params;
this.open = true;
this._createIframe();
this._addCloseListeners();
this._addMessageListeners();
}
Iframe.prototype.close = function () {
if (!this.open)
return;
this.container.parentNode.removeChild(this.container);
if (this.onClosed)
this.onClosed(this.error);
this.open = false;
};
Iframe.prototype._addCloseListeners = function () {
var _this = this;
this.container.addEventListener('click', function () { _this.close(); });
var self = this;
document.onkeydown = function (evt) {
evt = evt || window.event;
if (evt.keyCode == 27 && self.open) {
self.close();
}
};
};
Iframe.prototype._addMessageListeners = function () {
var _this = this;
var self = this;
var paramsSent = false;
window.addEventListener('message', function (e) {
var _a = e.data, origin = _a.origin, height = _a.height, type = _a.type, error = _a.error;
if (origin === 'squarelink-iframe') {
if (type === 'resize') {
self.iframe.style = styles.iframe(height + "px", 'none');
return;
}
else if (type === 'error') {
self.error = error;
self.close();
}
else if (type === 'onload' && !paramsSent) {
paramsSent = true;
_this.iframe.contentWindow.postMessage({
origin: 'squarelink-web3-sdk',
params: _this.params
}, '*');
}
}
}, false);
};
Iframe.prototype._createIframe = function () {
/* INITIALIZE IFRAME CONTAINER */
var container = document.createElement('div');
container.id = "squarelink-iframe-container";
container.style = styles.container;
/* INITIALIZE PRELOADER */
var preloader = "<div class=\"squarelink-preloader\" id=\"squarelink-preloader\"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>";
container.innerHTML = preloader;
/* INITIALIZE IFRAME */
var iframe = document.createElement('iframe');
iframe.src = config_1.IFRAME_URL + "/?url=" + encodeURI(this.url);
iframe.id = "squarelink-iframe";
iframe.style = styles.iframe();
iframe.onload = function () {
var pl = document.getElementById('squarelink-preloader');
pl.parentNode.removeChild(pl);
};
container.appendChild(iframe);
this.iframe = iframe;
this.container = container;
/* LOAD IFRAME CONTAINER */
document.body.appendChild(container);
};
return Iframe;
}());
exports.default = Iframe;
var styles = {
iframe: function (height, border) {
if (height === void 0) { height = '200px'; }
if (border === void 0) { border = "3px solid #fff"; }
return "\n position: absolute;\n height: " + height + ";\n width: 360px;\n top: 50%;\n left: 50%;\n transform:\n translate(-50%, -50%);\n border: 0px transparent;\n border-radius: 10px;\n -webkit-border-radius: 10px;\n -moz-border-radius: 10px;\n border-radius: 10px;\n -khtml-border-radius: 10px;\n border: " + border + ";\n z-index: 2147483647;\n box-shadow: 0 10px 30px 4px rgba(0,0,0,.33);\n background: none;\n ";
},
container: "\n position: fixed;\n height: 100%;\n width: 100%;\n left: 0;\n right: 0;\n top: 0;\n bottom: 0;\n z-index: 2147483647;\n background: rgba(0,0,0,0.5);\n "
};
//# sourceMappingURL=iframe.js.map