-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
286 lines (235 loc) · 9.82 KB
/
search.js
File metadata and controls
286 lines (235 loc) · 9.82 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
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
// Assists in creating elements
function create_element(elementTag='div', elementClass=null) {
let newElement = document.createElement(elementTag);
if (elementClass) {
newElement.className = elementClass;
}
return newElement;
}
document.addEventListener('DOMContentLoaded', function() {
const users = document.querySelector('[name="users"]');
const users_all = document.querySelector('[name="users_all"]');
const users_friends= document.querySelector('[name="users_friends"]');
const posts = document.querySelector('[name="posts"]');
const posts_all = document.querySelector('[name="posts_all"]');
const posts_mine = document.querySelector('[name="posts_mine"]');
const posts_friends= document.querySelector('[name="posts_friends"]');
const search_bar = document.getElementById('search_bar');
const search_button = document.getElementById('search_button');
const warning = document.getElementById('warning');
const resultContainer = document.getElementById('resultContainer');
const no_results = document.getElementById('no_results');
// Search target will be initially users and option will be all
let sTarget = "users";
let sOption = "users_all";
// Add results from search query with target users
function add_user_result(result) {
const resultDiv = create_element('div', 'search_result');
resultDiv.style.display = 'none';
const userDiv = create_element('div', 'user');
const infoDiv = create_element('div', 'user-info');
const pfpDiv = create_element('div', 'profile-pic');
pfpDiv.innerHTML = '<img src="/' + result.pfp_location + '">';
infoDiv.appendChild(pfpDiv);
const nameDiv = create_element('div');
nameDiv.style = 'margin-left: 0.5vw; text-align: left;';
nameDiv.innerHTML = '<div class="name">' + result.fullname + '</div><div class="username">@' + result.username + '</div>';
infoDiv.appendChild(nameDiv);
const actionDiv = create_element('div', 'actions');
const anchor = create_element('a');
anchor.href = '/profile/' + result.username;
const button = create_element('button', 'action-button');
button.innerHTML = '<img src="/static/images/profile.png">';
anchor.appendChild(button);
actionDiv.appendChild(anchor);
userDiv.appendChild(infoDiv);
userDiv.appendChild(actionDiv);
resultDiv.appendChild(userDiv);
resultContainer.appendChild(resultDiv);
resultDiv.style.display = 'flex';
}
// Add results from search query with target posts
function add_post_result(result) {
const resultDiv = create_element('div', 'search_result');
resultDiv.style.display = 'none';
const postDiv = create_element('div', 'post');
const countDiv = create_element('div', 'counts');
countDiv.innerHTML = '<b>Likes</b><br>' + result.likes + '<br><b>Comments</b><br>' + result.comments;
postDiv.appendChild(countDiv);
const imgDiv = create_element('div', 'post_image');
if (result.imagelocation) {
imgDiv.innerHTML = '<img src="/' + result.imagelocation + '"</img>';
}
postDiv.appendChild(imgDiv);
const tagsDiv = create_element('div', 'tags');
for (let tag of result.tags) {
const tagDiv = create_element('div', 'tag');
tagDiv.innerHTML = tag.tag; // tags is a list of dictionaries with only one key - 'tag'
tagsDiv.appendChild(tagDiv);
}
postDiv.appendChild(tagsDiv);
const infoDiv = create_element('div', 'post_info');
infoDiv.innerHTML = '<div class="title"><a href="/post/' + result.id + '">' + result.title + '</a></div><p class="contents">' + result.contents + '</p>';
postDiv.appendChild(infoDiv);
const anchor2 = create_element('a');
anchor2.href = '/post/' + result.id;
anchor2.style.width = '100%';
anchor2.appendChild(postDiv);
resultDiv.appendChild(anchor2);
resultContainer.appendChild(resultDiv);
resultDiv.style.display = 'flex';
}
// Search targets
document.getElementById('search_targets').addEventListener('click', function(event) {
const target = event.target;
// Change target to users
if (target.name == 'users') {
sTarget = 'users';
users.classList.add('btn-prof');
posts.classList.remove('btn-prof');
// Make options visible
document.getElementById('posts_search_options').style.display = 'none';
document.getElementById('users_search_options').style.display = 'block';
// Default to all
if (sOption != 'users_all') {
users_all.click();
}
}
// Change target to posts
if (target.name == 'posts') {
sTarget = 'posts';
posts.classList.add('btn-prof');
users.classList.remove('btn-prof');
// Make options visible
document.getElementById('users_search_options').style.display = 'none';
document.getElementById('posts_search_options').style.display = 'block';
// Default to all
if (sOption != 'posts_all') {
posts_all.click();
}
}
});
// Search options
document.getElementById('search_options').addEventListener('click', function(event) {
const target = event.target;
// All option for users target
if (target.name == 'users_all') {
sOption = 'users_all';
users_all.classList.add('btn-prof');
users_friends.classList.remove('btn-prof');
}
// Friends option for users target
if (target.name == 'users_friends') {
sOption = 'users_friends';
users_friends.classList.add('btn-prof');
users_all.classList.remove('btn-prof');
}
// All optionsfor posts target
if (target.name == 'posts_all') {
sOption = 'posts_all';
posts_all.classList.add('btn-prof');
posts_mine.classList.remove('btn-prof');
posts_friends.classList.remove('btn-prof');
}
// Mine option for posts target
if (target.name == 'posts_mine') {
sOption = 'posts_mine';
posts_mine.classList.add('btn-prof');
posts_all.classList.remove('btn-prof');
posts_friends.classList.remove('btn-prof');
}
// Friends option for posts target
if (target.name == 'posts_friends') {
sOption = 'posts_friends';
posts_friends.classList.add('btn-prof');
posts_mine.classList.remove('btn-prof');
posts_all.classList.remove('btn-prof');
}
});
search_bar.onkeyup = function(event) {
if (event.key == 'Enter') {
search_button.click();
}
}
// Search
search_button.onclick = async function() {
if (search_bar.value.length < 1) {
warning.innerHTML = 'Empty search query!';
warning.style.display = 'block';
setTimeout(function() {
warning.style.display = 'none';
}, 3000);
return;
} else if (search_bar.value.length > 640) {
warning.innerHTML = 'Query exceeds maximum length!';
warning.style.display = 'block';
setTimeout(function() {
warning.style.display = 'none';
}, 3000);
return;
}
if (!sTarget || !sOption) {
warning.innerHTML = 'Choose search target and options!';
warning.style.display = 'block';
setTimeout(function() {
warning.style.display = 'none';
}, 3000);
return;
}
try {
const url = '/api/search';
const data = {
'target': sTarget,
'option': sOption,
'query': search_bar.value
};
let results = document.querySelectorAll('.search_result');
for (let result of results) {
result.remove();
}
no_results.innerHTML = 'Loading...';
no_results.style.display = 'block';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error('Error sending data!');
}
no_results.style.display = 'none';
const responseData = await response.json();
if (!responseData.result) {
throw new Error('Error processing data!');
return;
}
if (!responseData.found) {
no_results.innerHTML = 'No results were found!';
no_results.style.display = 'block';
return;
}
if (responseData.target == 'users') {
for (let result of responseData.search_results) {
add_user_result(result);
}
} else {
for (let result of responseData.search_results) {
add_post_result(result);
}
}
} catch(error) {
console.log(error);
warning.innerHTML = 'Error performing search!'
warning.style.display = 'block';
setTimeout(function() {
warning.style.display = 'none';
}, 3000);
no_results.innerHTML = 'Your search results will appear here!';
no_results.style.display = 'block';
return;
}
};
});