forked from JesperDramsch/python-deadlines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown-simple.js
More file actions
165 lines (142 loc) · 5.78 KB
/
Copy pathcountdown-simple.js
File metadata and controls
165 lines (142 loc) · 5.78 KB
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
// Simple Countdown Timer System
// Single shared timer for all countdowns - efficient and maintainable
(function() {
'use strict';
// Ensure Luxon DateTime is available
const DateTime = window.luxon ? window.luxon.DateTime : null;
if (!DateTime) {
return; // Luxon not available, skip countdown initialization
}
let globalTimer = null;
function updateAllCountdowns() {
// Query all countdown elements and update them
document.querySelectorAll('.countdown-display').forEach(el => {
const deadlineStr = el.dataset.deadline;
const timezone = el.dataset.timezone || 'UTC-12';
if (!deadlineStr || deadlineStr === 'TBA' || deadlineStr === 'Cancelled') {
return; // Skip invalid deadlines
}
let deadline;
try {
// Try parsing as SQL format first (YYYY-MM-DD HH:mm:ss)
deadline = DateTime.fromSQL(deadlineStr, { zone: timezone });
// If invalid, try ISO format
if (deadline.invalid) {
deadline = DateTime.fromISO(deadlineStr, { zone: timezone });
}
// If still invalid, fall back to system timezone
if (deadline.invalid) {
deadline = DateTime.fromSQL(deadlineStr);
if (deadline.invalid) {
deadline = DateTime.fromISO(deadlineStr);
}
}
if (deadline.invalid) {
el.textContent = 'Invalid date';
return;
}
} catch (e) {
el.textContent = 'Error';
return;
}
// Calculate time difference
const now = DateTime.now();
const diff = deadline.diff(now);
if (diff.toMillis() <= 0) {
// Deadline has passed
el.textContent = el.classList.contains('countdown-small')
? 'Passed'
: 'Deadline passed';
el.classList.add('deadline-passed');
el.removeAttribute('data-urgency');
} else {
// Format and display countdown - get normalized components using shiftTo
const normalized = diff.shiftTo('days', 'hours', 'minutes', 'seconds');
const components = normalized.toObject();
const days = Math.floor(components.days || 0);
const hours = Math.floor(components.hours || 0);
const minutes = Math.floor(components.minutes || 0);
const seconds = Math.floor(components.seconds || 0);
if (el.classList.contains('countdown-small')) {
// Compact format for small countdown
el.textContent = `${days}d ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
} else if (el.classList.contains('conf-timer')) {
// Conference detail page format (matches jQuery countdown format)
const dayLabel = days === 1 ? 'day' : 'days';
el.textContent = `${days} ${dayLabel} ${hours}h ${minutes}m ${seconds}s`;
} else {
// Full format for regular countdown
el.textContent = `${days} days ${hours}h ${minutes}m ${seconds}s`;
}
// Set urgency level for visual feedback
if (days < 3) {
el.setAttribute('data-urgency', 'critical');
} else if (days < 7) {
el.setAttribute('data-urgency', 'high');
} else if (days < 14) {
el.setAttribute('data-urgency', 'medium');
} else {
el.removeAttribute('data-urgency');
}
el.classList.remove('deadline-passed');
}
});
}
// Initialize countdowns
function init() {
// Clear any existing timer
if (globalTimer) {
clearInterval(globalTimer);
}
// Initial update
updateAllCountdowns();
// Start the global timer (updates every second)
globalTimer = setInterval(updateAllCountdowns, 1000);
}
// Stop timer when page unloads
window.addEventListener('beforeunload', () => {
if (globalTimer) {
clearInterval(globalTimer);
globalTimer = null;
}
});
// Stop timer when page is hidden (saves battery)
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
if (globalTimer) {
clearInterval(globalTimer);
globalTimer = null;
}
} else {
// Restart timer when page becomes visible
init();
}
});
// Public API for integration with filtering
window.CountdownManager = {
// Called when conferences are filtered (compatibility with existing code)
onFilterUpdate: function() {
// No action needed - countdowns continue working regardless of visibility
// This function exists for backwards compatibility
},
// Manual refresh if needed
refresh: function() {
updateAllCountdowns();
},
// Initialize countdowns
init: init,
// Stop countdowns
destroy: function() {
if (globalTimer) {
clearInterval(globalTimer);
globalTimer = null;
}
}
};
// Auto-initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();