const Fuse = require("fuse.js"); var fuse; var packages = []; var packagecount = 0; var Browser = { maxentries: 50, filters: { getpkgs: () => { let pkgs = []; for (let i in packages) { if (! Browser.filters.isfiltered(packages[i].categories)) { pkgs.push(packages[i]); } } return pkgs; }, get: () => { let filtered = []; let unfiltered = []; let checks = browser.querySelectorAll("#filters .check"); for (let i = 0; i < checks.length; i++) { if (! checks[i].classList.contains("checked")) { filtered.push(checks[i].getAttribute("value")); } else { unfiltered.push(checks[i].getAttribute("value")); } } return { filtered, unfiltered }; }, isfiltered: (categories) => { let filtered = Browser.filters.get().filtered; let unfiltered = Browser.filters.get().unfiltered; let state = false; if (categories.length == 0) {return true} for (let i = 0; i < categories.length; i++) { if (filtered.includes(categories[i])) { state = true; continue } else if (unfiltered.includes(categories[i])) { state = false; continue } state = true; } return state; }, toggle: (state) => { if (state == false) { filters.classList.remove("shown"); return } filters.classList.toggle("shown"); let filterRect = filter.getBoundingClientRect(); let spacing = parseInt(getComputedStyle(filters).getPropertyValue("--spacing")); filters.style.top = filterRect.bottom - spacing; filters.style.right = filterRect.right - filterRect.left + filterRect.width - (spacing / 2); }, }, toggle: (state) => { if (state) { browser.scrollTo(0, 0); overlay.classList.add("shown") browser.classList.add("shown") if (browserEntries.querySelectorAll(".el").length == 0) { Browser.loadfront(); } return } else if (! state) { if (state != undefined) { Browser.filters.toggle(false); overlay.classList.remove("shown") browser.classList.remove("shown") return } } browser.scrollTo(0, 0); overlay.classList.toggle("shown") browser.classList.toggle("shown") }, loadfront: async () => { Browser.loading(); packagecount = 0; if (packages.length < 1) { packages = await (await fetch("https://northstar.thunderstore.io/api/v1/package/")).json(); fuse = new Fuse(packages, { keys: ["full_name"] }) } let pkgs = Browser.filters.getpkgs(); for (let i in pkgs) { if (packagecount >= Browser.maxentries) { Browser.endoflist(); break } new BrowserElFromObj(pkgs[i]); packagecount++; } }, loading: (string) => { if (Browser.filters.get().unfiltered.length == 0) { string = lang("gui.browser.noresults"); } if (string) { browserEntries.innerHTML = `