ultimate_turbo_modal 1.7.0 → 2.0.1

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.
@@ -0,0 +1,108 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+ import { enter, leave } from 'el-transition';
3
+
4
+ export default class extends Controller {
5
+ static targets = ["container", "content"]
6
+ static values = {
7
+ advanceUrl: String,
8
+ allowedClickOutsideSelector: String
9
+ }
10
+
11
+ connect() {
12
+ let _this = this;
13
+ this.showModal();
14
+
15
+ this.turboFrame = this.element.closest('turbo-frame');
16
+
17
+ // hide modal when back button is pressed
18
+ window.addEventListener('popstate', function (event) {
19
+ if (_this.#hasHistoryAdvanced()) _this.#resetModalElement();
20
+ });
21
+
22
+ window.modal = this;
23
+ }
24
+
25
+ disconnect() {
26
+ window.modal = undefined;
27
+ }
28
+
29
+ showModal() {
30
+ enter(this.containerTarget);
31
+
32
+ if (this.advanceUrlValue && !this.#hasHistoryAdvanced()) {
33
+ this.#setHistoryAdvanced();
34
+ history.pushState({}, "", this.advanceUrlValue);
35
+ }
36
+ }
37
+
38
+ // if we advanced history, go back, which will trigger
39
+ // hiding the model. Otherwise, hide the modal directly.
40
+ hideModal() {
41
+ // Prevent multiple calls to hideModal.
42
+ // Sometimes some events are double-triggered.
43
+ if (this.hidingModal) return
44
+ this.hidingModal = true;
45
+
46
+ let event = new Event('modal:closing', { cancelable: true });
47
+ this.turboFrame.dispatchEvent(event);
48
+ if (event.defaultPrevented) return
49
+
50
+ this.#resetModalElement();
51
+
52
+ event = new Event('modal:closed', { cancelable: false });
53
+ this.turboFrame.dispatchEvent(event);
54
+
55
+ if (this.#hasHistoryAdvanced())
56
+ history.back();
57
+ }
58
+
59
+ hide() {
60
+ this.hideModal();
61
+ }
62
+
63
+ refreshPage() {
64
+ window.Turbo.visit(window.location.href, { action: "replace" });
65
+ }
66
+
67
+ // hide modal on successful form submission
68
+ // action: "turbo:submit-end->modal#submitEnd"
69
+ submitEnd(e) {
70
+ if (e.detail.success) this.hideModal();
71
+ }
72
+
73
+ // hide modal when clicking ESC
74
+ // action: "keyup@window->modal#closeWithKeyboard"
75
+ closeWithKeyboard(e) {
76
+ if (e.code == "Escape") this.hideModal();
77
+ }
78
+
79
+ // hide modal when clicking outside of modal
80
+ // action: "click@window->modal#outsideModalClicked"
81
+ outsideModalClicked(e) {
82
+ let clickedInsideModal = this.contentTarget.contains(e.target) || this.contentTarget == e.target;
83
+ let clickedAllowedSelector = this.allowedClickOutsideSelectorValue && this.allowedClickOutsideSelectorValue !== '' && e.target.closest(this.allowedClickOutsideSelectorValue) != null;
84
+
85
+ if (!clickedInsideModal && !clickedAllowedSelector)
86
+ this.hideModal();
87
+ }
88
+
89
+ #resetModalElement() {
90
+ leave(this.containerTarget).then(() => {
91
+ this.turboFrame.removeAttribute("src");
92
+ this.containerTarget.remove();
93
+ this.#resetHistoryAdvanced();
94
+ });
95
+ }
96
+
97
+ #hasHistoryAdvanced() {
98
+ return document.body.getAttribute("data-turbo-modal-history-advanced") == "true"
99
+ }
100
+
101
+ #setHistoryAdvanced() {
102
+ return document.body.setAttribute("data-turbo-modal-history-advanced", "true")
103
+ }
104
+
105
+ #resetHistoryAdvanced() {
106
+ document.body.removeAttribute("data-turbo-modal-history-advanced");
107
+ }
108
+ }