obstacl
Version:
Simple multipurpose loader plugin for any frontend project
93 lines (92 loc) • 4.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Obstacl = void 0;
var functions_1 = require("./functions");
var _Obstacl = /** @class */ (function () {
function _Obstacl() {
/* keep track of target elements for easy termination */
this.stack = [];
}
_Obstacl.prototype.create = function (element, opts) {
if (opts === void 0) { opts = {}; }
var text = 'text' in opts ? opts.text : 'Wait...';
var icon = 'icon' in opts ? opts.icon : '';
var font = 'font' in opts ? opts.font : 16;
/* Better to use an Element directly if already available. Otherwise use it's DOM selector to query it. */
var elem = this.verifyElement(element);
if (elem) {
/* Prevent duplicate activation on an element */
var exist = this.stack.includes(elem);
if (!exist) {
elem.classList.add('isDisabled');
this.stack.push(elem);
/* prevent disabled elements such as buttons from been clickable while the obstacl is active */
if (!opts.onClick) {
elem.addEventListener('click', functions_1.breakClick, false);
}
/* if a default "action" is specified, activate it */
if (opts.action) {
opts.action();
}
/* if an "onClick" action is specified, activate it */
if (opts.onClick) {
elem.addEventListener('click', function (e) {
e.preventDefault();
opts.onClick();
return;
}, false);
}
if (opts.timer) {
if (opts.showCountdown) {
elem.appendChild(functions_1.overlayDelay({ timer: opts.timer, text: text, font: font }));
}
else {
elem.appendChild(functions_1.overlay({ text: text, icon: icon, font: font, animate: opts.animate }));
}
var $this_1 = this;
setTimeout(function () {
$this_1.destroy(elem);
}, opts.timer);
}
else if (opts.injectHTML) {
if (typeof opts.injectHTML !== 'string') {
console.error('property "injectHTML" expects value of type string.');
return;
}
else {
var useOverlay = 'useOverlay' in opts ? opts.useOverlay : true;
elem.appendChild(functions_1.injectCustomHtml(opts.injectHTML, useOverlay));
}
}
else {
elem.appendChild(functions_1.overlay({ text: text, icon: icon, font: font, animate: opts.animate }));
}
}
return;
}
console.error('invalid target supplied to $Obstacl creator.');
};
_Obstacl.prototype.destroy = function (element) {
/* if you have access to the button at the point of destruction, use it. Otherwise use it's 'selector' to get it from the DOM. */
var elem = this.verifyElement(element);
if (elem) {
var overlay_1 = elem.getElementsByClassName('Overlay')[0];
this.stack.splice(this.stack.indexOf(elem), 1);
elem.classList.remove('isDisabled');
elem.removeChild(overlay_1);
elem.removeEventListener('click', functions_1.breakClick, false);
return;
}
console.error('invalid target supplied to $Obstacl destroyer.');
};
_Obstacl.prototype.verifyElement = function (element) {
if (typeof element === 'string') {
return document.querySelector(element);
}
else {
return element;
}
};
return _Obstacl;
}());
exports.Obstacl = new _Obstacl();