nightTab/js/modal.js

138 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-12-26 07:45:53 +00:00
var modal = (function() {
var previousModal = null;
var destroy = function() {
2018-12-26 07:45:53 +00:00
var all_modal = helper.eA(".modal");
if (all_modal[0]) {
for (var i = 0; i < all_modal.length; i++) {
all_modal[i].destroy();
};
};
};
2019-01-06 15:47:08 +00:00
var render = function(override) {
var options = {
2018-12-26 07:45:53 +00:00
heading: "Modal",
content: "Body",
action: null,
actionText: "OK",
cancelText: "Cancel",
size: "medium"
};
2019-01-06 15:47:08 +00:00
if (override) {
options = helper.applyOptions(options, override);
2018-12-26 07:45:53 +00:00
};
var makeModal = function() {
var body = helper.e("body");
helper.setObject({
object: state.get(),
2019-02-25 15:00:49 +00:00
path: "modal",
newValue: true
});
2018-12-26 07:45:53 +00:00
var modalWrapper = document.createElement("div");
modalWrapper.setAttribute("class", "modal-wrapper");
var modal = document.createElement("div");
2019-01-06 15:47:08 +00:00
if (options.size == "large") {
2018-12-26 07:45:53 +00:00
modal.setAttribute("class", "modal modal-large");
2019-01-06 15:47:08 +00:00
} else if (options.size == "small") {
2018-12-26 07:45:53 +00:00
modal.setAttribute("class", "modal modal-small");
2019-01-06 15:47:08 +00:00
} else if (options.size) {
2018-12-26 07:45:53 +00:00
modal.setAttribute("class", "modal");
};
modal.destroy = function() {
if (modal.classList.contains("is-opaque")) {
helper.removeClass(modal, "is-opaque");
helper.addClass(modal, "is-transparent");
helper.addClass(modalWrapper, "is-droping-down");
} else {
modal.remove();
};
helper.setObject({
object: state.get(),
2019-02-25 15:00:49 +00:00
path: "modal",
newValue: false
});
2018-12-26 07:45:53 +00:00
};
var modalBody = document.createElement("div");
modalBody.setAttribute("class", "modal-body");
var modalControls = document.createElement("div");
modalControls.setAttribute("class", "modal-controls");
var actionButton = document.createElement("button");
actionButton.setAttribute("tabindex", "1");
actionButton.setAttribute("class", "modal-button button button-primary button-block");
2019-01-06 15:47:08 +00:00
actionButton.textContent = options.actionText;
2018-12-26 07:45:53 +00:00
var cancelButton = document.createElement("button");
cancelButton.setAttribute("tabindex", "1");
cancelButton.setAttribute("class", "modal-button button button-primary button-block");
2019-01-06 15:47:08 +00:00
cancelButton.textContent = options.cancelText;
2018-12-26 07:45:53 +00:00
modalControls.appendChild(cancelButton);
modalControls.appendChild(actionButton);
2019-01-06 15:47:08 +00:00
if (options.heading != null) {
2018-12-26 07:45:53 +00:00
var modalHeading = document.createElement("h1");
modalHeading.setAttribute("tabindex", "1");
modalHeading.setAttribute("class", "modal-heading");
2019-01-06 15:47:08 +00:00
modalHeading.textContent = options.heading;
2018-12-26 07:45:53 +00:00
modalBody.appendChild(modalHeading);
};
2019-01-06 15:47:08 +00:00
if (options.content) {
if (typeof options.content == "string") {
2018-12-26 07:45:53 +00:00
var container = document.createElement("div");
container.setAttribute("class", "container");
var para = document.createElement("p");
2019-01-06 15:47:08 +00:00
para.textContent = options.content;
2018-12-26 07:45:53 +00:00
container.appendChild(para);
modalBody.appendChild(container);
} else {
2019-01-06 15:47:08 +00:00
modalBody.appendChild(options.content);
2018-12-26 07:45:53 +00:00
};
};
modalWrapper.appendChild(modalBody);
modalWrapper.appendChild(modalControls);
modal.appendChild(modalWrapper);
modal.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(modal), false);
actionButton.addEventListener("click", function(event) {
this.destroy();
shade.destroy();
2019-01-06 15:47:08 +00:00
if (options.action) {
options.action();
2018-12-26 07:45:53 +00:00
};
}.bind(modal), false);
cancelButton.addEventListener("click", function(event) {
this.destroy();
shade.destroy();
}.bind(modal), false);
previousModal = modal;
shade.render({
action: function() {
modal.destroy();
},
includeHeader: true
});
body.appendChild(modal);
getComputedStyle(modal).opacity;
helper.removeClass(modal, "is-transparent");
helper.addClass(modal, "is-opaque");
modalHeading.focus(this);
};
if (previousModal != null) {
destroy();
};
makeModal();
};
// exposed methods
return {
destroy: destroy,
render: render
};
})();