Modal.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @class Modal
  3. * @description Modal is a class that enable to control easily a modal
  4. */
  5. export class Modal {
  6. /**
  7. *
  8. * @param {string} modalId - The ID of the element where the modal should appear.
  9. */
  10. constructor(modalId) {
  11. this.modal = document.getElementById(modalId);
  12. this.isOpen = this.modal ? this.modal.classList.contains("open") : false;
  13. }
  14. open() {
  15. if (!this.modal) return;
  16. this.modal.classList.add("open");
  17. document.body.style.overflow = "hidden";
  18. this.isOpen = true;
  19. }
  20. /**
  21. * Set text error in your modal
  22. * @param {String} text - The text error you want to show
  23. * @param {String} [type] The type of alert you want
  24. * @param {String} alertId
  25. * @returns {Void}
  26. */
  27. setError(text, type, alertId = "default", loader = false) {
  28. const nseHeader = this.modal.querySelector('.nseHeader');
  29. const nseError = nseHeader?.querySelector(`.alert#${CSS.escape(alertId)}`);
  30. if (!nseHeader || !nseError) return;
  31. if (!text) return nseError.style.display = "none";
  32. if (type) {
  33. nseError.classList.forEach((e) => {
  34. if (e === "alert") return;
  35. nseError.classList.remove(e);
  36. });
  37. nseError.classList.add(type);
  38. }
  39. nseError.textContent = text;
  40. if (loader) {
  41. const loaderSpan = document.createElement("span");
  42. loaderSpan.className = "loader";
  43. nseError.appendChild(loaderSpan);
  44. }
  45. nseError.style.display = "flex";
  46. }
  47. addAlert(type = "info") {
  48. const nseHeader = this.modal.querySelector('.nseHeader');
  49. if (!nseHeader) return -1;
  50. let alert = document.createElement("div");
  51. alert.classList.add("alert", type);
  52. alert.id = Math.random() * 1000000;
  53. nseHeader.append(alert);
  54. return alert.id;
  55. }
  56. close() {
  57. if (!this.modal) return;
  58. this.modal.classList.remove("open");
  59. document.body.style.overflow = "auto";
  60. this.isOpen = false;
  61. }
  62. /**
  63. * Sets the visibility of the modal.
  64. *
  65. * @param {boolean} visible - If true, the modal is opened; if false, it is closed.
  66. */
  67. setVisible(visible) {
  68. visible ? this.open() : this.close();
  69. }
  70. isVisible() {
  71. return this.isOpen;
  72. }
  73. /**
  74. * @returns {HTMLElement} - Get the the html element modal
  75. */
  76. getModal() {
  77. return this.modal;
  78. }
  79. /**
  80. * When background is clicked
  81. * @param {CallableFunction} callback - Function that enable when the background is clicked
  82. */
  83. onBackgroundClick(callback) {
  84. this.modal.addEventListener("click", (e) => {
  85. if (e.target === this.modal) callback();
  86. });
  87. }
  88. onValidate(callback, selector = ".validateBtn") {
  89. const btn = this.modal.querySelector(selector);
  90. if (btn) {
  91. btn.addEventListener("click", () => callback());
  92. }
  93. }
  94. onCloseButtonClick(selector = ".closeBtn") {
  95. const btn = this.modal.querySelector(selector);
  96. if (btn) {
  97. btn.addEventListener("click", () => this.setVisible(false));
  98. }
  99. }
  100. }