nightTab/js/shade.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-12-26 08:45:53 +01:00
var shade = (function() {
var previousShade = null;
var destroy = function() {
var allShade = helper.eA(".shade");
if (allShade[0]) {
for (var i = 0; i < allShade.length; i++) {
allShade[i].destroy();
2018-12-26 08:45:53 +01:00
};
};
};
2019-01-06 16:47:08 +01:00
var render = function(override) {
var options = {
action: null
2018-12-26 08:45:53 +01:00
};
2019-01-06 16:47:08 +01:00
if (override) {
options = helper.applyOptions(options, override);
2018-12-26 08:45:53 +01:00
};
var _destroyPreviousShade = function() {
2018-12-26 08:45:53 +01:00
if (previousShade != null) {
destroy();
};
};
var _makeShade = function() {
2018-12-26 08:45:53 +01:00
var body = helper.e("body");
var shade = document.createElement("div");
2018-12-28 15:26:32 +01:00
shade.setAttribute("class", "shade");
2018-12-26 08:45:53 +01:00
shade.destroy = function() {
if (shade.classList.contains("is-opaque")) {
helper.removeClass(shade, "is-opaque");
helper.addClass(shade, "is-transparent");
} else {
shade.remove();
};
};
shade.addEventListener("transitionend", function(event, elapsed) {
if (event.propertyName === "opacity" && getComputedStyle(this).opacity == 0) {
this.parentElement.removeChild(this);
};
if (event.propertyName === "opacity" && getComputedStyle(this).opacity == 1) {
helper.addClass(this, "is-transition-end");
};
}.bind(shade), false);
shade.addEventListener("click", function() {
this.destroy();
if (options.action) {
options.action();
};
2018-12-26 08:45:53 +01:00
}, false);
previousShade = shade;
body.appendChild(shade);
getComputedStyle(shade).opacity;
helper.removeClass(shade, "is-transparent");
helper.addClass(shade, "is-opaque");
};
_destroyPreviousShade();
_makeShade();
2018-12-26 08:45:53 +01:00
};
// exposed methods
return {
destroy: destroy,
render: render
};
})();