Skip to content

Commit 0ae8925

Browse files
refactor: Enhance username search functionality and improve suggestion box positioning
1 parent 318a33e commit 0ae8925

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/XtremeIdiots.Portal.Web/ApiControllers/ConnectedPlayersController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public async Task<IActionResult> SearchPlayers([FromQuery] string? term, [FromQu
6868
break;
6969
}
7070

71-
usernameMatches.AddRange(items.Where(x => x.Username.Contains(searchTerm, StringComparison.OrdinalIgnoreCase)));
71+
usernameMatches.AddRange(items.Where(x =>
72+
x.GameType == gameType &&
73+
x.Username.Contains(searchTerm, StringComparison.OrdinalIgnoreCase)));
7274
skip += items.Count;
7375
}
7476

src/XtremeIdiots.Portal.Web/wwwroot/js/connected-players-index.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,23 @@ $(document).ready(function () {
284284
box.style.zIndex = '1050';
285285
box.style.maxHeight = '240px';
286286
box.style.overflowY = 'auto';
287+
box.style.overflowX = 'hidden';
287288
box.style.display = 'none';
289+
box.style.position = 'absolute';
290+
box.style.left = '0px';
291+
box.style.top = '0px';
288292

289-
input.insertAdjacentElement('afterend', box);
293+
document.body.appendChild(box);
290294

291295
let timer = null;
296+
let requestCounter = 0;
297+
298+
function positionSuggestions() {
299+
const rect = input.getBoundingClientRect();
300+
box.style.width = rect.width + 'px';
301+
box.style.left = (window.scrollX + rect.left) + 'px';
302+
box.style.top = (window.scrollY + rect.bottom) + 'px';
303+
}
292304

293305
function clearSuggestions() {
294306
box.innerHTML = '';
@@ -303,7 +315,7 @@ $(document).ready(function () {
303315
empty.className = 'list-group-item text-muted py-1 px-2';
304316
empty.textContent = cfg.mapEmptyMessage();
305317
box.appendChild(empty);
306-
box.style.width = input.offsetWidth + 'px';
318+
positionSuggestions();
307319
box.style.display = 'block';
308320
return;
309321
}
@@ -325,7 +337,7 @@ $(document).ready(function () {
325337
box.appendChild(button);
326338
});
327339

328-
box.style.width = input.offsetWidth + 'px';
340+
positionSuggestions();
329341
box.style.display = 'block';
330342
}
331343

@@ -335,6 +347,7 @@ $(document).ready(function () {
335347
return;
336348
}
337349

350+
const currentRequest = ++requestCounter;
338351
const params = new URLSearchParams(cfg.queryParams(term));
339352
params.set('term', term);
340353

@@ -351,6 +364,10 @@ $(document).ready(function () {
351364
}
352365

353366
const data = await response.json();
367+
if (currentRequest !== requestCounter) {
368+
return;
369+
}
370+
354371
setSuggestions(data);
355372
} catch {
356373
clearSuggestions();
@@ -364,6 +381,28 @@ $(document).ready(function () {
364381
timer = setTimeout(function () { doSearch(term); }, cfg.delay);
365382
});
366383

384+
input.addEventListener('focus', function () {
385+
if (box.style.display !== 'none') {
386+
positionSuggestions();
387+
}
388+
});
389+
390+
window.addEventListener('resize', function () {
391+
if (box.style.display !== 'none') {
392+
positionSuggestions();
393+
}
394+
});
395+
396+
window.addEventListener('scroll', function () {
397+
if (box.style.display !== 'none') {
398+
positionSuggestions();
399+
}
400+
}, true);
401+
402+
box.addEventListener('wheel', function (event) {
403+
event.stopPropagation();
404+
}, { passive: true });
405+
367406
document.addEventListener('click', function (event) {
368407
const withinInput = input.contains(event.target);
369408
const withinBox = box.contains(event.target);
@@ -377,6 +416,7 @@ $(document).ready(function () {
377416
input.value = '';
378417
hidden.value = '';
379418
clearSuggestions();
419+
requestCounter++;
380420
}
381421
};
382422
}

0 commit comments

Comments
 (0)