-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathspotlightsearch.js
195 lines (112 loc) · 3.6 KB
/
spotlightsearch.js
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
searchInput.openSearch = () => {
// clear search input
searchInput.innerText = '';
// hide clear button
searchClear.classList.remove('visible');
// update add button
addButton.classList.remove('clear-button-visible');
header.classList.add('searching');
// focus search input
searchInput.focus();
}
// open search screen on click of button
searchButton.addEventListener('click', searchInput.openSearch);
// open search on focus
searchInput.addEventListener('focus', () => {
// if not already searching
if (!header.classList.contains('searching')) {
// open search
searchInput.openSearch();
}
})
// close search screen on click of button
searchInput.closeSearch = () => {
// show all files
let files = fileWrapper.querySelectorAll('.item[style="display: none;"]');
files.forEach(file => { file.style.display = '' });
header.classList.remove('searching');
if (document.activeElement === searchInput) {
searchInput.blur();
}
}
searchBack.addEventListener('click', searchInput.closeSearch);
searchInput.addEventListener('blur', () => {
// if query is empty
if (searchInput.textContent === '') {
// close search
searchInput.closeSearch();
}
})
searchInput.search = () => {
if (searchInput.innerHTML === '<br>') {
searchInput.textContent = '';
}
let query = searchInput.textContent.toLowerCase().replaceAll('\n', '');
// exclude 'more' button from search
let files = fileWrapper.querySelectorAll('.item:not(.more)');
// search files
files.forEach(file => {
let name = file.querySelector('.name').textContent;
if (!name.toLowerCase().includes(query)) {
file.style.display = 'none';
}
else {
file.style.display = '';
}
})
// if search query exists
if (searchInput.textContent !== '') {
// show clear button
searchClear.classList.add('visible');
// rotate add button
addButton.classList.add('clear-button-visible');
} else {
// hide clear button
searchClear.classList.remove('visible');
// update add button
addButton.classList.remove('clear-button-visible');
}
const moreButton = fileWrapper.querySelector('.item.more');
// if more button exists
// and is not disabled (loading more)
if (moreButton &&
!moreButton.classList.contains('disabled')) {
// if more button is in view
if (elInView(moreButton)) {
// load more items
clickedOnMoreButtonHTML(moreButton);
}
}
}
// search when typed in input
searchInput.addEventListener('input', searchInput.search);
searchInput.addEventListener('keydown', (e) => {
// disable enter key in search input
if (e.key === 'Enter') {
e.preventDefault();
// if query exists
if (searchInput.textContent !== '') {
searchInput.blur();
}
}
if (e.key === 'Escape') {
// close search
e.preventDefault();
searchInput.closeSearch();
}
})
searchInput.clearSearch = () => {
// show all files
let files = fileWrapper.querySelectorAll('.item[style="display: none;"]');
files.forEach(file => { file.style.display = '' });
// clear search input
searchInput.innerText = '';
// hide clear button
searchClear.classList.remove('visible');
// update add button
addButton.classList.remove('clear-button-visible');
// focus search input
searchInput.focus();
}
// clear search input when clicked on button
searchClear.addEventListener('click', searchInput.clearSearch);