File size: 7,288 Bytes
70ba896
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Time Filter Management for Dashboard
class TimeFilterManager {
    constructor() {
        this.currentPeriod = 'daily';
        this.currentDate = new Date().toISOString().split('T')[0];
        this.callbacks = new Set();
        this.init();
    }

    init() {
        this.setupEventListeners();
        this.initializeFilter();
    }

    setupEventListeners() {
        // Listen for period change events from the component
        document.addEventListener('period-changed', (event) => {
            const { period, date } = event.detail;
            this.handlePeriodChange(period, date);
        });

        // Listen for keyboard shortcuts
        document.addEventListener('keydown', (event) => {
            if (event.ctrlKey || event.metaKey) {
                switch (event.key) {
                    case '1':
                        event.preventDefault();
                        this.setPeriod('daily');
                        break;
                    case '2':
                        event.preventDefault();
                        this.setPeriod('weekly');
                        break;
                    case '3':
                        event.preventDefault();
                        this.setPeriod('monthly');
                        break;
                }
            }
        });
    }

    initializeFilter() {
        // Set initial state
        this.triggerCallbacks();
    }

    handlePeriodChange(period, date) {
        const previousPeriod = this.currentPeriod;
        const previousDate = this.currentDate;

        this.currentPeriod = period;
        this.currentDate = date;

        // Only trigger callbacks if something actually changed
        if (previousPeriod !== period || previousDate !== date) {
            this.triggerCallbacks();
            this.updateURL();
        }
    }

    triggerCallbacks() {
        const data = {
            period: this.currentPeriod,
            date: this.currentDate,
            timestamp: Date.now()
        };

        this.callbacks.forEach(callback => {
            try {
                callback(data);
            } catch (error) {
                console.error('Error in time filter callback:', error);
            }
        });
    }

    updateURL() {
        // Update URL parameters without page reload
        const url = new URL(window.location);
        url.searchParams.set('period', this.currentPeriod);
        url.searchParams.set('date', this.currentDate);
        
        window.history.replaceState({}, '', url);
    }

    // Public API methods
    setPeriod(period) {
        const filterComponent = document.querySelector('[x-data*="timeFilter"]');
        if (filterComponent && filterComponent._x_dataStack) {
            const alpineData = filterComponent._x_dataStack[0];
            if (alpineData && typeof alpineData.setPeriod === 'function') {
                alpineData.setPeriod(period);
            }
        }
    }

    setDate(date) {
        const filterComponent = document.querySelector('[x-data*="timeFilter"]');
        if (filterComponent && filterComponent._x_dataStack) {
            const alpineData = filterComponent._x_dataStack[0];
            if (alpineData && typeof alpineData.setDate === 'function') {
                alpineData.setDate(date);
            }
        }
    }

    getCurrentPeriod() {
        return this.currentPeriod;
    }

    getCurrentDate() {
        return this.currentDate;
    }

    getCurrentSelection() {
        return {
            period: this.currentPeriod,
            date: this.currentDate
        };
    }

    // Callback management
    onPeriodChange(callback) {
        if (typeof callback === 'function') {
            this.callbacks.add(callback);
            
            // Immediately call with current state
            callback({
                period: this.currentPeriod,
                date: this.currentDate,
                timestamp: Date.now()
            });
        }
        
        // Return unsubscribe function
        return () => {
            this.callbacks.delete(callback);
        };
    }

    // Utility methods
    getDateRange() {
        const date = new Date(this.currentDate);
        
        switch (this.currentPeriod) {
            case 'daily':
                return {
                    start: new Date(date),
                    end: new Date(date)
                };
            
            case 'weekly':
                const startOfWeek = new Date(date);
                startOfWeek.setDate(date.getDate() - date.getDay());
                const endOfWeek = new Date(startOfWeek);
                endOfWeek.setDate(startOfWeek.getDate() + 6);
                
                return {
                    start: startOfWeek,
                    end: endOfWeek
                };
            
            case 'monthly':
                const startOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
                const endOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                
                return {
                    start: startOfMonth,
                    end: endOfMonth
                };
            
            default:
                return {
                    start: new Date(date),
                    end: new Date(date)
                };
        }
    }

    formatDateForAPI() {
        return this.currentDate;
    }

    getPeriodLabel() {
        const labels = {
            daily: 'Daily',
            weekly: 'Weekly',
            monthly: 'Monthly'
        };
        
        return labels[this.currentPeriod] || 'Daily';
    }

    // Navigation methods
    goToPrevious() {
        const date = new Date(this.currentDate);
        
        switch (this.currentPeriod) {
            case 'daily':
                date.setDate(date.getDate() - 1);
                break;
            case 'weekly':
                date.setDate(date.getDate() - 7);
                break;
            case 'monthly':
                date.setMonth(date.getMonth() - 1);
                break;
        }
        
        this.setDate(date.toISOString().split('T')[0]);
    }

    goToNext() {
        const date = new Date(this.currentDate);
        
        switch (this.currentPeriod) {
            case 'daily':
                date.setDate(date.getDate() + 1);
                break;
            case 'weekly':
                date.setDate(date.getDate() + 7);
                break;
            case 'monthly':
                date.setMonth(date.getMonth() + 1);
                break;
        }
        
        this.setDate(date.toISOString().split('T')[0]);
    }

    goToToday() {
        this.setDate(new Date().toISOString().split('T')[0]);
    }
}

// Initialize time filter manager
let timeFilterManager;

document.addEventListener('DOMContentLoaded', function() {
    timeFilterManager = new TimeFilterManager();
    
    // Make it globally available
    window.timeFilterManager = timeFilterManager;
});

export { TimeFilterManager };