| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /**
- * @class Modal
- * @description Modal is a class that enable to control easily a modal
- */
- export class Modal {
- /**
- *
- * @param {string} modalId - The ID of the element where the modal should appear.
- */
- constructor(modalId) {
- this.modal = document.getElementById(modalId);
- this.isOpen = this.modal ? this.modal.classList.contains("open") : false;
- }
- open() {
- if (!this.modal) return;
- this.modal.classList.add("open");
- document.body.style.overflow = "hidden";
- this.isOpen = true;
- }
- /**
- * Set text error in your modal
- * @param {String} text - The text error you want to show
- * @param {String} [type] The type of alert you want
- * @param {String} alertId
- * @returns {Void}
- */
- setError(text, type, alertId = "default", loader = false) {
- const nseHeader = this.modal.querySelector('.nseHeader');
- const nseError = nseHeader?.querySelector(`.alert#${CSS.escape(alertId)}`);
- if (!nseHeader || !nseError) return;
-
- if (!text) return nseError.style.display = "none";
-
- if (type) {
- nseError.classList.forEach((e) => {
- if (e === "alert") return;
- nseError.classList.remove(e);
- });
- nseError.classList.add(type);
- }
-
- nseError.textContent = text;
-
- if (loader) {
- const loaderSpan = document.createElement("span");
- loaderSpan.className = "loader";
- nseError.appendChild(loaderSpan);
- }
-
- nseError.style.display = "flex";
- }
-
- addAlert(type = "info") {
- const nseHeader = this.modal.querySelector('.nseHeader');
- if (!nseHeader) return -1;
- let alert = document.createElement("div");
- alert.classList.add("alert", type);
- alert.id = Math.random() * 1000000;
- nseHeader.append(alert);
- return alert.id;
- }
- close() {
- if (!this.modal) return;
- this.modal.classList.remove("open");
- document.body.style.overflow = "auto";
- this.isOpen = false;
- }
- /**
- * Sets the visibility of the modal.
- *
- * @param {boolean} visible - If true, the modal is opened; if false, it is closed.
- */
- setVisible(visible) {
- visible ? this.open() : this.close();
- }
- isVisible() {
- return this.isOpen;
- }
- /**
- * @returns {HTMLElement} - Get the the html element modal
- */
- getModal() {
- return this.modal;
- }
- /**
- * When background is clicked
- * @param {CallableFunction} callback - Function that enable when the background is clicked
- */
- onBackgroundClick(callback) {
- this.modal.addEventListener("click", (e) => {
- if (e.target === this.modal) callback();
- });
- }
- onValidate(callback, selector = ".validateBtn") {
- const btn = this.modal.querySelector(selector);
- if (btn) {
- btn.addEventListener("click", () => callback());
- }
- }
- onCloseButtonClick(selector = ".closeBtn") {
- const btn = this.modal.querySelector(selector);
- if (btn) {
- btn.addEventListener("click", () => this.setVisible(false));
- }
- }
- }
|