mirror of
https://github.com/Fluffy-Bean/GameExpo23.git
synced 2025-05-19 09:44:52 +00:00
Fix Commit history
This commit is contained in:
parent
a29f06dfee
commit
d26d8cb021
70 changed files with 674 additions and 477 deletions
88
TFR/server/static/js/search.js
Normal file
88
TFR/server/static/js/search.js
Normal file
|
@ -0,0 +1,88 @@
|
|||
function showHint() {
|
||||
let search = document.querySelector('.search > input');
|
||||
let searchPos = search.getBoundingClientRect();
|
||||
let hint = document.querySelector('.search-hint');
|
||||
|
||||
hint.style.width = search.offsetWidth + 'px';
|
||||
hint.style.left = searchPos.left + 'px';
|
||||
hint.style.top = searchPos.bottom + 'px';
|
||||
|
||||
hint.style.display = 'flex';
|
||||
}
|
||||
|
||||
|
||||
function hideHint() {
|
||||
let hint = document.querySelector('.search-hint');
|
||||
hint.style.display = 'none';
|
||||
}
|
||||
|
||||
|
||||
function updateHint() {
|
||||
let search = document.querySelector('.search > input');
|
||||
let searchPos = search.getBoundingClientRect();
|
||||
let hint = document.querySelector('.search-hint');
|
||||
|
||||
hint.style.width = search.offsetWidth + 'px';
|
||||
hint.style.left = searchPos.left + 'px';
|
||||
hint.style.top = searchPos.bottom + 'px';
|
||||
}
|
||||
|
||||
|
||||
function getSearch() {
|
||||
let search = document.querySelector('.search > input').value;
|
||||
let hint = document.querySelector('.search-hint');
|
||||
|
||||
if (search.length === 0) {
|
||||
hint.innerHTML = '<p>Start typing to see results...</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
fetch('/api/users?search=' + search.toString(), {
|
||||
method: 'GET',
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.length === 0) {
|
||||
hint.innerHTML = '<p>No results found...</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
hint.innerHTML = '';
|
||||
|
||||
data.forEach(user => {
|
||||
let el = document.createElement('button');
|
||||
el.innerHTML = user;
|
||||
el.onmousedown = function (event) {
|
||||
event.preventDefault();
|
||||
search = document.querySelector('.search > input');
|
||||
search.value = user.toString();
|
||||
}
|
||||
hint.appendChild(el);
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
hint.innerHTML = '<p>Something went wrong...</p>';
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
window.onload = () => {
|
||||
let typingTimer;
|
||||
let search = document.querySelector('.search > input');
|
||||
|
||||
if (search === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('resize', updateHint);
|
||||
|
||||
search.addEventListener('focus', showHint);
|
||||
search.addEventListener('blur', hideHint);
|
||||
search.addEventListener('keydown', () => {
|
||||
clearTimeout(typingTimer);
|
||||
});
|
||||
search.addEventListener('keyup', () => {
|
||||
clearTimeout(typingTimer);
|
||||
typingTimer = setTimeout(getSearch, 250);
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue