Jake Vanderwerf
2026-05-12 16cb63b05910055c31dca821c86f2eb815da99e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
 * Shortcut class to centralize all modal functionality, utilized by the JS ecosystem
 */
class ModalController {
    constructor(modal, options){
        this.modal = modal;
        this.a11y = window.jvbA11y;
        this.options = {
            openMessage: 'Opened modal',
            closeMessage: 'Closed modal',
            open:       null,
            close:      'button.cancel',
            save:       'button[type="submit"]',
            ...options
        }
 
        this.subscribers = new Set();
 
        if (!ModalController.modalStack) {
            ModalController.modalStack = [];
        }
 
        this.boundEscapeListener = this.handleEscapeListener.bind(this);
        this.boundBackdropListener = this.handleBackdropListener.bind(this);
 
        this.init();
    }
 
    init(){
        this.isOpen = false;
        this.hasChanges = false;
        this.initElements();
        this.initEvents();
    }
 
    initElements() {
        this.elements = {
            open:   this.options.open,
            close:  this.options.close,
            save:   this.options.save,
        };
    }
 
    handleClose(){
        // Only close if this is the topmost modal
        if (ModalController.modalStack[ModalController.modalStack.length - 1] !== this) {
            return;
        }
 
        this.notify('modal-close', this.modal);
 
        this.a11y.announce(this.options.closeMessage);
        this.modal.close();
        this.isOpen = false;
 
 
        // Remove this modal from the stack
        const index = ModalController.modalStack.indexOf(this);
        if (index !== -1) {
            ModalController.modalStack.splice(index, 1);
        }
 
        this.showBody();
        // Clean up event listeners
        this.removeCloseListeners();
    }
 
    handleOpen(e){
        this.addCloseListeners();
        this.hideBody();
        this.isOpen = true;
        this.modal.showModal();
 
        if (!this.a11y) {
            this.a11y = window.jvbA11y;
        }
        this.a11y.trapFocus(this.modal);
        this.a11y.announce(this.options.openMessage);
 
        // Add this modal to the stack
        ModalController.modalStack.push(this);
 
        this.notify('modal-open', {modal: this.modal, event: e});
    }
 
    addCloseListeners() {
        document.addEventListener('keydown', this.boundEscapeListener);
        document.addEventListener('click', this.boundBackdropListener);
    }
    removeCloseListeners() {
        document.removeEventListener('keydown', this.boundEscapeListener);
        document.removeEventListener('click', this.boundBackdropListener);
    }
 
    handleEscapeListener(e) {
        if (e.key === 'Escape' && ModalController.modalStack[ModalController.modalStack.length - 1] === this) {
            e.preventDefault(); // Prevent default browser behavior
            this.handleClose();
        }
    }
    handleBackdropListener(e) {
        if (e.target === this.modal && ModalController.modalStack[ModalController.modalStack.length - 1] === this) {
            this.handleClose();
        }
    }
 
    hideBody(){
        // Only hide body if this is the first modal
        if (ModalController.modalStack.length === 0) {
            document.body.style.overflow = 'hidden';
        }
    }
    showBody(){
        if (ModalController.modalStack.length === 0) {
            document.body.style.overflow = '';
        }
    }
 
    initEvents(){
        document.addEventListener('click', this.handleClick.bind(this));
        // document.addEventListener('beforeUnload', () => this.destroy());
    }
 
    handleClick(e) {
        // Handle open triggers from anywhere
        if (this.elements.open && window.targetCheck(e, this.elements.open)) {
            this.handleOpen(e);
            return;
        }
 
        // Only handle close/save if the click is within this modal
        if (!this.modal.contains(e.target)) {
            return;
        }
 
        // if (this.elements.save && window.targetCheck(e, this.elements.save)) {
        //  this.handleClose();
        // } else
            if (this.elements.close && window.targetCheck(e, this.elements.close)) {
            // Additional check: only close if we're the top modal
            if (ModalController.modalStack[ModalController.modalStack.length - 1] === this) {
                this.handleClose();
            }
        }
    }
 
    // Static methods for managing modal stack
    static getTopModal() {
        return ModalController.modalStack[ModalController.modalStack.length - 1] || null;
    }
 
    static getAllModals() {
        return [...ModalController.modalStack];
    }
 
    static closeTopModal() {
        const topModal = ModalController.getTopModal();
        if (topModal) {
            topModal.handleClose();
        }
    }
 
    static closeAllModals() {
        // Close from top to bottom to maintain proper stack behavior
        while (ModalController.modalStack.length > 0) {
            ModalController.closeTopModal();
        }
    }
    /**
     * Event system
     */
    subscribe(callback) {
        this.subscribers.add(callback);
        return () => this.subscribers.delete(callback);
    }
 
    notify(event, data) {
        this.subscribers.forEach(cb => cb(event, data));
    }
 
    destroy() {
        this.subscribers.clear();
        ModalController.closeAllModals();
        this.showBody();
        this.removeCloseListeners();
    }
}
document.addEventListener('DOMContentLoaded', ()=> {
    window.jvbModal = ModalController;
});