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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
 
// Base class for shared UI functionality
class UIHandler {
    constructor() {
        this.elements = {};
        this.activeComponents = new Set();
        this.componentStates = new Map();
        this.observers = new Map();
        this.handleOutsideClick = this.handleOutsideClick.bind(this);
        this.handleEscapeKey = this.handleEscapeKey.bind(this);
 
    }
 
    bindElements() {
        console.error('bindElements must be implemented by child class');
    }
 
 
 
    // Add shared event binding
    bindComponentEvents() {
 
        if (!this.handlers) return;
        Object.entries(this.handlers).forEach(([elementKey, config]) => {
            const elements = this.elements[elementKey];
            if (!elements) return;
 
            // Handle NodeList
            if (elements instanceof NodeList || Array.isArray(elements)) {
                elements.forEach(element => {
                    this.bindEventsToElement(element, config);
                });
            }
            // Handle single element
            else {
                this.bindEventsToElement(elements, config);
            }
        });
    }
 
    bindEventsToElement(element, config) {
        if (typeof config === 'function') {
            // If config is a function, bind it to click event
            element.addEventListener('click', config.bind(this));
        } else if (typeof config === 'object') {
            // Handle object with multiple events
            Object.entries(config).forEach(([event, handler]) => {
                if (event !== 'forEach' && typeof handler === 'function') {
                    element.addEventListener(event, handler);
                }
            });
        }
    }
    bindEvents() {
        document.addEventListener('click', this.handleOutsideClick);
        document.addEventListener('keydown', this.handleEscapeKey);
    }
 
    // Component State Management
    isComponentActive(componentKey) {
        return this.activeComponents.has(componentKey);
    }
 
    // Add helper method for handling component state
    setComponentState(e, t, n = {}) {
        const {
            element: s,
            toggle: i,
            activeClass: r = "open",
            focusElement: o = null,
            ariaLabel: c = null,
            ariaHidden: a = null,
            cleanup: l = null
        } = n;
 
        if (s) {
            t ? this.activeComponents.add(e) : this.activeComponents.delete(e);
            s.classList.toggle(r, t);
 
            if (i) {
                i.setAttribute("aria-expanded", t.toString());
                c && i.setAttribute("aria-label", c);
            }
 
            if (null !== a) {
                s.setAttribute("aria-hidden", (!t).toString());
            }
 
            // Add null check before calling focus()
            if (o && typeof o.focus === 'function') {
                o.focus();
            }
 
            if (!t && l) {
                l();
            }
 
            this.componentStates.set(e, {
                isActive: t,
                activeClass: r,
                options: n
            });
        }
    }
 
 
    // Add keyboard navigation management
    initializeKeyboardNavigation(config) {
        this.keyboardConfig = config;
 
        Object.entries(config).forEach(([elementKey, keyHandlers]) => {
            const element = this.elements[elementKey];
            if (!element) return;
 
            element.addEventListener('keydown', (e) => {
                const handler = keyHandlers[e.key];
                if (handler) {
                    handler.call(this, e);
                }
            });
        });
    }
 
 
    handleOutsideClick(event) {
        console.error('handleOutsideClick must be implemented by child class');
    }
    handleEscapeKey(event) {
        console.error('handleEscapeKey must be implemented by child class');
    }
 
    // Add shared handler initialization
    initializeHandlers(handlers) {
        if (!handlers || typeof handlers !== 'object') {
            console.error('Invalid handlers configuration');
            return;
        }
 
        this.handlers = Object.entries(handlers).reduce((acc, [key, value]) => {
            if (typeof value === 'function') {
                acc[key] = value.bind(this);
            } else if (value.forEach) {
                acc[key] = {
                    ...value,
                    handler: value.handler?.bind(this)
                };
            } else if (typeof value === 'object') {
                acc[key] = Object.entries(value).reduce((events, [event, handler]) => {
                    events[event] = typeof handler === 'function' ? handler.bind(this) : handler;
                    return events;
                }, {});
            }
            return acc;
        }, {});
    }
    createObserver(options, callback) {
        const defaultOptions = {
            root: null,
            rootMargin: '0px',
            threshold: 0
        };
 
        return new IntersectionObserver(
            callback,
            { ...defaultOptions, ...options }
        );
    }
 
    initializeObserver(observerId, elements, options, callback) {
        if (!elements || !elements.length) return;
 
        // Cleanup existing observer if it exists
        this.cleanupObserver(observerId);
 
        // Create and store new observer
        const observer = this.createObserver(options, callback);
        this.observers.set(observerId, {
            observer,
            elements: new Set(elements)
        });
 
        // Start observing elements
        elements.forEach(element => {
            if (element) {
                observer.observe(element);
            }
        });
 
        return observer;
    }
 
    cleanupObserver(observerId) {
        const observerData = this.observers.get(observerId);
        if (observerData) {
            const { observer, elements } = observerData;
            elements.forEach(element => {
                if (element) {
                    observer.unobserve(element);
                }
            });
            observer.disconnect();
            this.observers.delete(observerId);
        }
    }
 
    cleanupAllObservers() {
        this.observers.forEach((_, observerId) => {
            this.cleanupObserver(observerId);
        });
    }
 
    // Optional cleanup method for removing event listeners
    cleanup() {
        document.removeEventListener('click', this.handleOutsideClick);
        document.removeEventListener('keydown', this.handleEscapeKey);
        this.cleanupComponentEvents();
        this.cleanupAllObservers();
    }
    // Add shared event cleanup
    cleanupComponentEvents() {
        Object.entries(this.handlers).forEach(([elementKey, config]) => {
            const element = this.elements[elementKey];
            if (!element) return;
 
            if (config.forEach && element.forEach) {
                element.forEach(item => {
                    if (item._boundHandler) {
                        item.removeEventListener('click', item._boundHandler);
                        delete item._boundHandler;
                    }
                });
            } else if (typeof config === 'object') {
                Object.entries(config).forEach(([event, handler]) => {
                    if (event !== 'forEach') {
                        element.removeEventListener(event, handler);
                    }
                });
            }
        });
    }
 
    handleSearchCheckboxes(form) {
        if (!form) return;
 
        const allCheckbox = form.querySelector('input[type="checkbox"][value="1"]');
        const otherCheckboxes = form.querySelectorAll('input[type="checkbox"]:not([value="1"])');
 
        if (!allCheckbox) return;
 
        const updateCheckboxes = (e) => {
            const checkbox = e.target;
 
            if (checkbox === allCheckbox) {
                // If 'all' is checked, uncheck others
                if (checkbox.checked) {
                    otherCheckboxes.forEach(cb => {
                        cb.checked = false;
                    });
                }
            } else {
                // If any other checkbox is checked, uncheck 'all'
                if (checkbox.checked) {
                    allCheckbox.checked = false;
                } else {
                    // If no other checkboxes are checked, check 'all'
                    const anyOthersChecked = Array.from(otherCheckboxes)
                        .some(cb => cb.checked);
                    if (!anyOthersChecked) {
                        allCheckbox.checked = true;
                    }
                }
            }
        };
 
        // Add event listeners to all checkboxes
        form.querySelectorAll('input[type="checkbox"]')
            .forEach(checkbox => {
                checkbox.addEventListener('change', updateCheckboxes);
            });
 
        // Store cleanup function
        form._removeCheckboxListeners = () => {
            form.querySelectorAll('input[type="checkbox"]')
                .forEach(checkbox => {
                    checkbox.removeEventListener('change', updateCheckboxes);
                });
        };
    }
}
 
window.UIHandler = UIHandler;