211 lines
5.6 KiB
JavaScript
211 lines
5.6 KiB
JavaScript
"use strict";
|
|
|
|
(function (factory) {
|
|
if (typeof module === "object" && module.exports && typeof window === "undefined") {
|
|
module.exports = factory(null, null);
|
|
return;
|
|
}
|
|
|
|
var win = typeof window !== "undefined" ? window : null;
|
|
var $ = win && typeof win.jQuery !== "undefined" ? win.jQuery : null;
|
|
factory($, win);
|
|
})(function ($, win) {
|
|
if (!win || !win.document) {
|
|
return {};
|
|
}
|
|
|
|
const DEFAULTS = {
|
|
type: "info",
|
|
dismissible: true,
|
|
timeout: 0,
|
|
classPrefix: "jq-alert",
|
|
};
|
|
|
|
function removeAlert(el) {
|
|
if (!el) {
|
|
return;
|
|
}
|
|
el.classList.remove("is-visible");
|
|
win.setTimeout(function () {
|
|
if (el.parentNode) {
|
|
el.parentNode.removeChild(el);
|
|
}
|
|
}, 180);
|
|
}
|
|
|
|
function createElement(tag, className) {
|
|
var el = win.document.createElement(tag);
|
|
if (className) {
|
|
el.className = className;
|
|
}
|
|
return el;
|
|
}
|
|
|
|
function getGlobalHost() {
|
|
var id = "jq-alert-host";
|
|
var existing = win.document.getElementById(id);
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
|
|
var host = createElement("div", "jq-alert-host");
|
|
host.id = id;
|
|
win.document.body.appendChild(host);
|
|
return host;
|
|
}
|
|
|
|
function renderAlert(host, options) {
|
|
var settings = Object.assign({}, DEFAULTS, options || {});
|
|
var alertEl = createElement(
|
|
"div",
|
|
settings.classPrefix + " " + settings.classPrefix + "--" + settings.type + " is-visible"
|
|
);
|
|
alertEl.setAttribute("role", "alert");
|
|
|
|
var content = createElement("div", settings.classPrefix + "__content");
|
|
content.textContent = String(settings.message || "");
|
|
alertEl.appendChild(content);
|
|
|
|
if (settings.dismissible) {
|
|
var close = createElement("button", settings.classPrefix + "__close");
|
|
close.type = "button";
|
|
close.setAttribute("aria-label", "Close alert");
|
|
close.textContent = "x";
|
|
close.addEventListener("click", function () {
|
|
removeAlert(alertEl);
|
|
});
|
|
alertEl.appendChild(close);
|
|
}
|
|
|
|
host.appendChild(alertEl);
|
|
|
|
if (settings.timeout > 0) {
|
|
win.setTimeout(function () {
|
|
removeAlert(alertEl);
|
|
}, settings.timeout);
|
|
}
|
|
|
|
return alertEl;
|
|
}
|
|
|
|
function show(options) {
|
|
return renderAlert(getGlobalHost(), options);
|
|
}
|
|
|
|
function closeConfirm(backdrop, resolve, value) {
|
|
if (!backdrop || backdrop.getAttribute("data-closed") === "1") {
|
|
return;
|
|
}
|
|
backdrop.setAttribute("data-closed", "1");
|
|
backdrop.classList.remove("is-visible");
|
|
win.setTimeout(function () {
|
|
if (backdrop.parentNode) {
|
|
backdrop.parentNode.removeChild(backdrop);
|
|
}
|
|
resolve(value);
|
|
}, 180);
|
|
}
|
|
|
|
function confirm(options) {
|
|
var settings = Object.assign(
|
|
{
|
|
title: "Potwierdzenie",
|
|
message: "",
|
|
confirmLabel: "Potwierdz",
|
|
cancelLabel: "Anuluj",
|
|
danger: false,
|
|
},
|
|
options || {}
|
|
);
|
|
|
|
return new Promise(function (resolve) {
|
|
var backdrop = createElement("div", "jq-alert-modal-backdrop is-visible");
|
|
var modal = createElement("div", "jq-alert-modal");
|
|
modal.setAttribute("role", "dialog");
|
|
modal.setAttribute("aria-modal", "true");
|
|
|
|
var titleId = "jq-alert-modal-title-" + Date.now();
|
|
modal.setAttribute("aria-labelledby", titleId);
|
|
|
|
var header = createElement("div", "jq-alert-modal__header");
|
|
var title = createElement("h3");
|
|
title.id = titleId;
|
|
title.textContent = String(settings.title || "");
|
|
header.appendChild(title);
|
|
|
|
var body = createElement("div", "jq-alert-modal__body");
|
|
body.textContent = String(settings.message || "");
|
|
|
|
var footer = createElement("div", "jq-alert-modal__footer");
|
|
var cancelButton = createElement("button", "btn btn--secondary");
|
|
cancelButton.type = "button";
|
|
cancelButton.textContent = String(settings.cancelLabel || "Anuluj");
|
|
var confirmButton = createElement(
|
|
"button",
|
|
settings.danger ? "btn btn--danger" : "btn btn--primary"
|
|
);
|
|
confirmButton.type = "button";
|
|
confirmButton.textContent = String(settings.confirmLabel || "Potwierdz");
|
|
footer.appendChild(cancelButton);
|
|
footer.appendChild(confirmButton);
|
|
|
|
modal.appendChild(header);
|
|
modal.appendChild(body);
|
|
modal.appendChild(footer);
|
|
backdrop.appendChild(modal);
|
|
win.document.body.appendChild(backdrop);
|
|
|
|
var onKeyDown = function (event) {
|
|
if (event.key === "Escape") {
|
|
onCancel();
|
|
}
|
|
};
|
|
var cleanup = function () {
|
|
win.document.removeEventListener("keydown", onKeyDown);
|
|
};
|
|
|
|
var onConfirm = function () {
|
|
cleanup();
|
|
if (typeof settings.onConfirm === "function") {
|
|
settings.onConfirm();
|
|
}
|
|
closeConfirm(backdrop, resolve, true);
|
|
};
|
|
var onCancel = function () {
|
|
cleanup();
|
|
if (typeof settings.onCancel === "function") {
|
|
settings.onCancel();
|
|
}
|
|
closeConfirm(backdrop, resolve, false);
|
|
};
|
|
|
|
confirmButton.addEventListener("click", onConfirm);
|
|
cancelButton.addEventListener("click", onCancel);
|
|
backdrop.addEventListener("click", function (event) {
|
|
if (event.target === backdrop) {
|
|
onCancel();
|
|
}
|
|
});
|
|
win.document.addEventListener("keydown", onKeyDown);
|
|
|
|
confirmButton.focus();
|
|
});
|
|
}
|
|
|
|
win.OrderProAlerts = {
|
|
show: show,
|
|
confirm: confirm,
|
|
};
|
|
|
|
if ($ && $.fn) {
|
|
$.fn.orderProAlert = function (options) {
|
|
var settings = $.extend({}, DEFAULTS, options);
|
|
return this.each(function () {
|
|
renderAlert(this, settings);
|
|
});
|
|
};
|
|
}
|
|
|
|
return win.OrderProAlerts;
|
|
});
|