optimize a lot of stuff
This commit is contained in:
240
js/loader.js
240
js/loader.js
@ -2,99 +2,85 @@ document.addEventListener("DOMContentLoaded", loadGames);
|
||||
let elements = [];
|
||||
let isDev = [];
|
||||
let pageData;
|
||||
async function loadGames() {
|
||||
if (type == "g") pageData = { path: "/resources/games.json", prefix: "semag", type: "g" };
|
||||
else if (type == "a") pageData = { path: "/resources/apps.json", prefix: "sppa", type: "a" };
|
||||
else sAlert("this is not valid");
|
||||
// taken from mdn
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
||||
let data = await (await fetch(pageData.path)).json();
|
||||
let sorted = data.sort((a, b) => {
|
||||
const n1 = a.name.toUpperCase();
|
||||
const n2 = b.name.toUpperCase();
|
||||
if (n1 < n2) {
|
||||
return -1;
|
||||
}
|
||||
if (n1 > n2) {
|
||||
return 1;
|
||||
}
|
||||
// shouldnt happen but just incase
|
||||
return 0;
|
||||
});
|
||||
let gamesElement = document.getElementById("games");
|
||||
let topGElement = document.getElementById("topGames");
|
||||
let starredGames = [];
|
||||
|
||||
let gamesFragment = document.createDocumentFragment();
|
||||
let topFragment = document.createDocumentFragment();
|
||||
let starredGames = JSON.parse(localStorage.getItem("selenite.starred") || "[]");
|
||||
sorted.forEach((element) => {
|
||||
isStarred = starredGames.indexOf(element.directory) != -1;
|
||||
let newElement = document.createElement("a");
|
||||
newElement.setAttribute("data-target", element.directory);
|
||||
newElement.setAttribute("data-image", element.image);
|
||||
newElement.setAttribute("class", "game");
|
||||
newElement.setAttribute("href", `/loader.html?title=${encodeURIComponent(element.name)}&dir=${element.directory}&img=${element.image}&type=${pageData.type}`);
|
||||
let image = document.createElement("img");
|
||||
async function loadGames() {
|
||||
if (type == "g") pageData = { path: "/resources/games-tagged.json", prefix: "semag", type: "g" };
|
||||
else if (type == "a") pageData = { path: "/resources/apps.json", prefix: "sppa", type: "a" };
|
||||
else { sAlert("this is not valid"); return; }
|
||||
|
||||
let data = await (await fetch(pageData.path)).json();
|
||||
|
||||
data.forEach(g => g._key = g.name.toUpperCase());
|
||||
data.sort((a, b) => a._key < b._key ? -1 : a._key > b._key ? 1 : 0);
|
||||
|
||||
const gamesElement = document.getElementById("games");
|
||||
const topGElement = document.getElementById("topGames");
|
||||
const gamesFragment = document.createDocumentFragment();
|
||||
const topFragment = document.createDocumentFragment();
|
||||
const disableTop = localStorage.getItem("selenite.disableTopGames") != "false";
|
||||
|
||||
starredGames = JSON.parse(localStorage.getItem("selenite.starred") || "[]");
|
||||
const starredSet = new Set(starredGames);
|
||||
|
||||
data.forEach((element) => {
|
||||
const isStarred = starredSet.has(element.directory);
|
||||
const newElement = document.createElement("a");
|
||||
newElement.dataset.target = element.directory;
|
||||
newElement.dataset.image = element.image;
|
||||
newElement.dataset.name = element._key;
|
||||
newElement.className = "game";
|
||||
newElement.href = `/loader.html?title=${encodeURIComponent(element.name)}&dir=${element.directory}&img=${element.image}&type=${pageData.type}`;
|
||||
|
||||
const image = document.createElement("img");
|
||||
image.src = `/resources/${pageData.prefix}/${element.directory}/${element.image}`;
|
||||
image.loading = `lazy`;
|
||||
image.onerror = function () {
|
||||
this.src = "/img/missing.svg";
|
||||
};
|
||||
let holder = document.createElement("div");
|
||||
image.loading = "lazy";
|
||||
image.onerror = function() { this.src = "/img/missing.svg"; };
|
||||
|
||||
const holder = document.createElement("div");
|
||||
holder.id = "holder";
|
||||
let title = document.createElement("h1");
|
||||
const title = document.createElement("h1");
|
||||
title.innerText = element.name;
|
||||
let star = document.createElement("img");
|
||||
star.id = "star";
|
||||
star.classList = "star";
|
||||
star.src = isStarred ? "/img/star-fill.svg" : "/img/star.svg";
|
||||
let warnings = document.createElement("warnings");
|
||||
newElement.appendChild(image);
|
||||
holder.appendChild(title);
|
||||
|
||||
const star = document.createElement("img");
|
||||
star.id = "star";
|
||||
star.className = "star";
|
||||
star.src = isStarred ? "/img/star-fill.svg" : "/img/star.svg";
|
||||
|
||||
const warnings = document.createElement("warnings");
|
||||
newElement.appendChild(image);
|
||||
newElement.appendChild(holder);
|
||||
newElement.appendChild(warnings);
|
||||
|
||||
if (element.tags) {
|
||||
if (element.tags.includes("18+")) {
|
||||
let thirteenplus = document.createElement("warning");
|
||||
thirteenplus.innerText = "18+";
|
||||
thirteenplus.classList = "thirteen";
|
||||
warnings.appendChild(thirteenplus);
|
||||
[["18+", "thirteen", "18+"], ["13+", "thirteen", "13+"],
|
||||
["horror", "horror", "😱"], ["gore", "gore", "🩸"]].forEach(([tag, cls, text]) => {
|
||||
if (element.tags.includes(tag)) {
|
||||
const w = document.createElement("warning");
|
||||
w.innerText = text;
|
||||
w.className = cls;
|
||||
warnings.appendChild(w);
|
||||
}
|
||||
});
|
||||
if (element.tags.includes("wip")) {
|
||||
isDev.push(element.directory);
|
||||
const w = document.createElement("warning");
|
||||
w.innerText = "WIP";
|
||||
w.className = "thirteen";
|
||||
warnings.appendChild(w);
|
||||
}
|
||||
if (element.tags.includes("13+")) {
|
||||
let thirteenplus = document.createElement("warning");
|
||||
thirteenplus.innerText = "13+";
|
||||
thirteenplus.classList = "thirteen";
|
||||
warnings.appendChild(thirteenplus);
|
||||
}
|
||||
if (element.tags.includes("horror")) {
|
||||
let horror = document.createElement("warning");
|
||||
horror.innerText = "😱";
|
||||
horror.classList = "horror";
|
||||
warnings.appendChild(horror);
|
||||
}
|
||||
if (element.tags.includes("gore")) {
|
||||
let gore = document.createElement("warning");
|
||||
gore.innerText = "🩸";
|
||||
gore.classList = "gore";
|
||||
warnings.appendChild(gore);
|
||||
}
|
||||
if (element.tags.includes("top") && localStorage.getItem("selenite.disableTopGames") != "false") {
|
||||
if (element.tags.includes("top") && disableTop) {
|
||||
topFragment.appendChild(newElement);
|
||||
} else {
|
||||
gamesFragment.appendChild(newElement);
|
||||
holder.appendChild(star);
|
||||
}
|
||||
if (element.tags.includes("wip")) {
|
||||
isDev.push(element.directory);
|
||||
let thirteenplus = document.createElement("warning");
|
||||
thirteenplus.innerText = "WIP";
|
||||
thirteenplus.classList = "thirteen";
|
||||
warnings.appendChild(thirteenplus);
|
||||
}
|
||||
} else {
|
||||
gamesFragment.appendChild(newElement);
|
||||
holder.appendChild(star);
|
||||
}
|
||||
|
||||
elements.push(newElement);
|
||||
star.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
@ -102,96 +88,78 @@ async function loadGames() {
|
||||
starEvent(e);
|
||||
});
|
||||
});
|
||||
|
||||
gamesElement.appendChild(gamesFragment);
|
||||
topGElement.appendChild(topFragment);
|
||||
document.getElementById("gameCount").innerText = `${data.length} games loaded!`;
|
||||
document.getElementById("loadingMsg").style.display = "none";
|
||||
document.getElementById("allHeader").style.display = "block";
|
||||
if (localStorage.getItem("selenite.disableTopGames") != "false") {
|
||||
document.getElementById("topHeader").style.display = "block";
|
||||
}
|
||||
if (isDev.length > 0) {
|
||||
sAlert("wip games", "dev message, shouldnt be seen");
|
||||
console.log(isDev.join(","));
|
||||
}
|
||||
if (disableTop) document.getElementById("topHeader").style.display = "block";
|
||||
if (isDev.length > 0) console.log(isDev.join(","));
|
||||
|
||||
starredGames = JSON.parse(localStorage.getItem("selenite.starred") || "[]");
|
||||
if (starredGames.length > 0) {
|
||||
document.getElementById("starredHeader").style.display = "block";
|
||||
starredGames.forEach((e) => {
|
||||
let element = document.querySelector(`#games a.game[data-target='${e}']`);
|
||||
let newElement = element.cloneNode(true);
|
||||
document.getElementById("starredgames").appendChild(newElement);
|
||||
});
|
||||
_rebuildStarred();
|
||||
document.querySelectorAll("#starredgames #star").forEach((e) => {
|
||||
e.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
starEvent(e);
|
||||
});
|
||||
e.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); starEvent(e); });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let searchRaf = null;
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
document.getElementById("gamesearch").addEventListener("input", () => {
|
||||
let input = document.getElementById("gamesearch").value.toUpperCase();
|
||||
let total = 0;
|
||||
if (elements.length > 0) {
|
||||
elements.forEach((element) => {
|
||||
let title = element.childNodes[1].childNodes[0].innerText.toUpperCase();
|
||||
if (title.includes(input)) {
|
||||
element.style.display = "flex";
|
||||
if (searchRaf) cancelAnimationFrame(searchRaf);
|
||||
searchRaf = requestAnimationFrame(() => {
|
||||
const input = document.getElementById("gamesearch").value.toUpperCase();
|
||||
let hidden = 0;
|
||||
elements.forEach((el) => {
|
||||
if (el.dataset.name.includes(input)) {
|
||||
el.style.display = "flex";
|
||||
} else {
|
||||
element.style.display = "none";
|
||||
total++;
|
||||
el.style.display = "none";
|
||||
hidden++;
|
||||
}
|
||||
});
|
||||
}
|
||||
document.getElementById("noResults").style.display = total >= elements.length ? "flex" : "none";
|
||||
document.getElementById("noResults").style.display = hidden >= elements.length ? "flex" : "none";
|
||||
searchRaf = null;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function _rebuildStarred() {
|
||||
const container = document.getElementById("starredgames");
|
||||
const gamesEl = document.getElementById("games");
|
||||
container.innerHTML = "";
|
||||
const fragment = document.createDocumentFragment();
|
||||
starredGames.forEach((dir) => {
|
||||
const el = gamesEl.querySelector(`a.game[data-target='${dir}']`);
|
||||
if (el) fragment.appendChild(el.cloneNode(true));
|
||||
});
|
||||
container.appendChild(fragment);
|
||||
}
|
||||
|
||||
function starEvent(e) {
|
||||
let game = e.target.parentNode.parentNode.getAttribute("data-target");
|
||||
const game = e.target.parentNode.parentNode.getAttribute("data-target");
|
||||
starredGames = JSON.parse(localStorage.getItem("selenite.starred") || "[]");
|
||||
const header = document.getElementById("starredHeader");
|
||||
|
||||
if (starredGames.indexOf(game) == -1) {
|
||||
starredGames.push(game);
|
||||
starredGames.sort();
|
||||
if (starredGames.length > 0) {
|
||||
document.getElementById("starredHeader").style.display = "block";
|
||||
} else {
|
||||
document.getElementById("starredHeader").style.display = "none";
|
||||
}
|
||||
localStorage.setItem("selenite.starred", JSON.stringify(starredGames));
|
||||
header.style.display = "block";
|
||||
e.target.src = "/img/star-fill.svg";
|
||||
starredgames.innerHTML = "";
|
||||
starredGames.forEach((e) => {
|
||||
let element = document.querySelector(`#games a.game[data-target='${e}']`);
|
||||
let newElement = element.cloneNode(true);
|
||||
document.getElementById("starredgames").appendChild(newElement);
|
||||
});
|
||||
} else {
|
||||
starredGames.splice(starredGames.indexOf(game), 1);
|
||||
document.querySelectorAll(`a.game[data-target='${game}'] #star`).forEach((e) => {
|
||||
e.src = "/img/star.svg";
|
||||
});
|
||||
if (starredGames.length > 0) {
|
||||
document.getElementById("starredHeader").style.display = "block";
|
||||
} else {
|
||||
document.getElementById("starredHeader").style.display = "none";
|
||||
}
|
||||
localStorage.setItem("selenite.starred", JSON.stringify(starredGames));
|
||||
starredgames.innerHTML = "";
|
||||
starredGames.forEach((e) => {
|
||||
let element = document.querySelector(`#games a.game[data-target='${e}']`);
|
||||
let newElement = element.cloneNode(true);
|
||||
document.getElementById("starredgames").appendChild(newElement);
|
||||
});
|
||||
document.querySelectorAll(`a.game[data-target='${game}'] #star`).forEach((s) => { s.src = "/img/star.svg"; });
|
||||
header.style.display = starredGames.length > 0 ? "block" : "none";
|
||||
}
|
||||
|
||||
localStorage.setItem("selenite.starred", JSON.stringify(starredGames));
|
||||
_rebuildStarred();
|
||||
|
||||
document.querySelectorAll("#starredgames #star").forEach((e) => {
|
||||
e.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
starEvent(e);
|
||||
});
|
||||
e.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); starEvent(e); });
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user