diff --git a/app.js b/app.js index 57f8e1a..e9cab21 100644 --- a/app.js +++ b/app.js @@ -46,10 +46,13 @@ const backupBtn = document.getElementById("backupBtn"); const restoreMergeBtn = document.getElementById("restoreMergeBtn"); const restoreReplaceBtn = document.getElementById("restoreReplaceBtn"); const restoreFileInput = document.getElementById("restoreFileInput"); +const quickSearchInput = document.getElementById("quickSearchInput"); +const quickSearchResults = document.getElementById("quickSearchResults"); const gamesList = document.getElementById("gamesList"); const gameCardTemplate = document.getElementById("gameCardTemplate"); let editingGameId = null; let pendingRestoreMode = "merge"; +let quickSearchTerm = ""; toolsToggleBtn.addEventListener("click", () => { toolsDrawer.classList.toggle("open"); @@ -78,6 +81,11 @@ document.addEventListener("keydown", (event) => { } }); +quickSearchInput.addEventListener("input", (event) => { + quickSearchTerm = event.target.value.trim(); + renderSearchResults(); +}); + platformForm.addEventListener("submit", async (event) => { event.preventDefault(); @@ -430,6 +438,7 @@ function render() { renderBrandTabs(); renderConsoleTabs(); renderGames(); + renderSearchResults(); } function renderDataMode() { @@ -482,6 +491,96 @@ function renderDataMode() { } } +function findBrandByConsole(consoleName) { + for (const [brand, consoles] of Object.entries(state.brands)) { + if (Array.isArray(consoles) && consoles.includes(consoleName)) { + return brand; + } + } + return "INCONNUE"; +} + +function collectAllGames() { + const all = []; + for (const [consoleName, games] of Object.entries(state.gamesByConsole)) { + const brand = findBrandByConsole(consoleName); + for (const game of games || []) { + all.push({ ...game, consoleName, brand }); + } + } + return all; +} + +function renderSearchResults() { + if (!quickSearchResults) { + return; + } + + const query = quickSearchTerm.trim().toLowerCase(); + if (!query) { + quickSearchResults.innerHTML = + '

Saisis un titre pour verifier si tu possedes deja le jeu.

'; + return; + } + + const normalizedQuery = query.replace(/\s+/g, " "); + const allGames = collectAllGames(); + const matches = allGames.filter((game) => { + const title = String(game.title || "").toLowerCase(); + return title.includes(normalizedQuery); + }); + + const exactMatches = allGames.filter((game) => { + const title = String(game.title || "") + .toLowerCase() + .replace(/\s+/g, " ") + .trim(); + return title === normalizedQuery; + }); + + if (!matches.length) { + quickSearchResults.innerHTML = + '

Aucun jeu trouve dans ta collection.

'; + return; + } + + const maxShown = 20; + const shown = matches.slice(0, maxShown); + const header = + exactMatches.length > 0 + ? `

Deja possede: OUI (${exactMatches.length} correspondance${exactMatches.length > 1 ? "s" : ""} exacte${exactMatches.length > 1 ? "s" : ""}).

` + : `

Jeu similaire trouve: ${matches.length} resultat${matches.length > 1 ? "s" : ""}.

`; + + const items = shown + .map((game) => { + const meta = [ + `${game.brand} / ${game.consoleName}`, + game.version ? `Version: ${game.version}` : null, + game.isDuplicate ? "Double: OUI" : null, + ] + .filter(Boolean) + .join(" | "); + return `
${escapeHtml(game.title || "")}

${escapeHtml(meta)}

`; + }) + .join(""); + + const more = + matches.length > maxShown + ? `

+${matches.length - maxShown} autre(s) resultat(s).

` + : ""; + + quickSearchResults.innerHTML = `${header}${items}${more}`; +} + +function escapeHtml(text) { + return String(text) + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """) + .replaceAll("'", "'"); +} + function renderBrandTabs() { const brands = Object.keys(state.brands); brandTabs.innerHTML = ""; diff --git a/index.html b/index.html index 9c5c933..1fd2f50 100644 --- a/index.html +++ b/index.html @@ -71,6 +71,16 @@

+
+ +
+

Saisis un titre pour verifier si tu possedes deja le jeu.

+
+
+