@total_onion/onion-modalcontroller 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/onion-modalcontroller.js +80 -0
- package/package.json +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export default class modalController {
|
|
2
|
+
constructor() {
|
|
3
|
+
let defaultGlobalSettings = {
|
|
4
|
+
bodyClass: "modal-active",
|
|
5
|
+
activeStateClass: "modal-active",
|
|
6
|
+
enableDebugLogs: true,
|
|
7
|
+
userInteracted: false,
|
|
8
|
+
};
|
|
9
|
+
this.scanForNewTriggers();
|
|
10
|
+
|
|
11
|
+
if (modalController.instance instanceof modalController) {
|
|
12
|
+
return modalController.instance;
|
|
13
|
+
}
|
|
14
|
+
modalController.instance = this;
|
|
15
|
+
this.globalSettings = defaultGlobalSettings;
|
|
16
|
+
this.enableDebugLogs = this.globalSettings.enableDebugLogs;
|
|
17
|
+
this.handleModalTriggers();
|
|
18
|
+
this.handleBgClicks();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
scanForNewTriggers() {
|
|
22
|
+
this.globalModalTriggers = document.body.querySelectorAll(
|
|
23
|
+
"[data-modalpopuptrigger]"
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
handleBgClicks() {
|
|
27
|
+
window.addEventListener("click", (event) => {
|
|
28
|
+
const modalPopup = document.getElementById("modal-controller-popup");
|
|
29
|
+
this.closePopupOnClickOutside(event, modalPopup);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
handleModalTriggers() {
|
|
33
|
+
this.globalModalTriggers.forEach((trigger) => {
|
|
34
|
+
trigger.addEventListener("click", () => {
|
|
35
|
+
this.triggerModalPopup(trigger.dataset.modalpopuptrigger);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
triggerModalPopup(triggerName) {
|
|
40
|
+
let modalPopup = document.getElementById("modal-controller-popup");
|
|
41
|
+
if (!modalPopup) {
|
|
42
|
+
const modalPopupHtml = `<div class="modal-controller-popup" id="modal-controller-popup">
|
|
43
|
+
<div class="modal-controller-popup__container">
|
|
44
|
+
<span class="modal-controller-popup__close-btn" id="modal-controller-popup__close-btn">×</span>
|
|
45
|
+
<div class="modal-controller-popup__content"></div>
|
|
46
|
+
</div>
|
|
47
|
+
</div>`;
|
|
48
|
+
document.body.insertAdjacentHTML("beforeend", modalPopupHtml);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const popupContent = document.querySelector(
|
|
52
|
+
".modal-controller-popup__content"
|
|
53
|
+
);
|
|
54
|
+
if (!popupContent) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
popupContent.innerHTML = "";
|
|
58
|
+
const modalPopupContent = document
|
|
59
|
+
.querySelector(`[data-modalpopupcontent=${triggerName}]`)
|
|
60
|
+
.cloneNode(true);
|
|
61
|
+
|
|
62
|
+
if (modalPopupContent) {
|
|
63
|
+
popupContent.appendChild(modalPopupContent);
|
|
64
|
+
}
|
|
65
|
+
modalPopup.classList.add("open");
|
|
66
|
+
document.documentElement.classList.add("lock-position");
|
|
67
|
+
}
|
|
68
|
+
closePopup(modalPopup) {
|
|
69
|
+
modalPopup.classList.remove("open");
|
|
70
|
+
document.documentElement.classList.remove("lock-position");
|
|
71
|
+
}
|
|
72
|
+
closePopupOnClickOutside(event, modalPopup) {
|
|
73
|
+
if (event.target === modalPopup) {
|
|
74
|
+
this.closePopup(modalPopup);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
setGlobalSettings(settings = {}) {
|
|
78
|
+
this.globalSettings = { ...this.globalSettings, ...settings };
|
|
79
|
+
}
|
|
80
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@total_onion/onion-modalcontroller",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pop up modal controller",
|
|
5
|
+
"main": "onion-modalcontroller.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/TotalOnion/onion-modalcontroller.git"
|
|
12
|
+
},
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/TotalOnion/onion-modalcontroller/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/TotalOnion/onion-modalcontroller#readme"
|
|
19
|
+
}
|