-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-analytics.ejs
More file actions
142 lines (124 loc) · 3.76 KB
/
admin-analytics.ejs
File metadata and controls
142 lines (124 loc) · 3.76 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
/*
* Cheng Tsz Hung (25017438D)
* Awwab Hamam (22103907D)
*/
(function () {
document.addEventListener('DOMContentLoaded', () => {
const searchField = document.getElementById('searchTerm');
const suggestionList = document.getElementById('eventSuggestions');
const form = document.getElementById('eventSearchForm');
if (!searchField || !suggestionList) {
return;
}
let activeIndex = -1;
let currentSuggestions = [];
let controller;
function hideSuggestions() {
suggestionList.hidden = true;
suggestionList.innerHTML = '';
activeIndex = -1;
currentSuggestions = [];
}
function renderSuggestions(suggestions) {
if (!suggestions.length) {
hideSuggestions();
return;
}
currentSuggestions = suggestions;
suggestionList.innerHTML = suggestions
.map(
(suggestion, index) =>
`<button type="button" class="suggestion-item" data-suggestion-index="${index}">${suggestion}</button>`
)
.join('');
suggestionList.hidden = false;
activeIndex = -1;
}
async function fetchSuggestions(term) {
const url = searchField.dataset.suggestUrl;
if (!url) {
return;
}
if (controller) {
controller.abort();
}
controller = new AbortController();
try {
const response = await fetch(`${url}?term=${encodeURIComponent(term)}`, {
signal: controller.signal
});
if (!response.ok) {
throw new Error('Failed to fetch suggestions');
}
const data = await response.json();
renderSuggestions(data.suggestions || []);
} catch (error) {
if (error.name !== 'AbortError') {
hideSuggestions();
}
}
}
searchField.addEventListener('input', (event) => {
const term = event.target.value.trim();
if (term.length < 2) {
hideSuggestions();
return;
}
fetchSuggestions(term);
});
searchField.addEventListener('keydown', (event) => {
if (suggestionList.hidden) {
return;
}
if (event.key === 'ArrowDown') {
event.preventDefault();
activeIndex = (activeIndex + 1) % currentSuggestions.length;
updateActiveSuggestion();
} else if (event.key === 'ArrowUp') {
event.preventDefault();
activeIndex = activeIndex <= 0 ? currentSuggestions.length - 1 : activeIndex - 1;
updateActiveSuggestion();
} else if (event.key === 'Enter') {
if (activeIndex >= 0 && activeIndex < currentSuggestions.length) {
event.preventDefault();
selectSuggestion(currentSuggestions[activeIndex]);
}
} else if (event.key === 'Escape') {
hideSuggestions();
}
});
function updateActiveSuggestion() {
const items = suggestionList.querySelectorAll('.suggestion-item');
items.forEach((item, index) => {
if (index === activeIndex) {
item.classList.add('active');
item.focus();
} else {
item.classList.remove('active');
}
});
}
function selectSuggestion(value) {
searchField.value = value;
hideSuggestions();
if (form) {
form.submit();
}
}
suggestionList.addEventListener('click', (event) => {
const button = event.target.closest('.suggestion-item');
if (!button) {
return;
}
const index = Number(button.dataset.suggestionIndex);
if (!Number.isNaN(index)) {
selectSuggestion(currentSuggestions[index]);
}
});
document.addEventListener('click', (event) => {
if (!suggestionList.contains(event.target) && event.target !== searchField) {
hideSuggestions();
}
});
});
})();