diff options
author | 0neGal <mail@0negal.com> | 2024-06-09 18:15:45 +0200 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2024-06-09 18:23:29 +0200 |
commit | 760031c079ce830755ba4fea029e149f4140e00b (patch) | |
tree | 8dbb9f8d5bb4e19cdaffb40e8a091b457a597769 /src/app/js/browser.js | |
parent | 1c1e8fb730b9974cd9b8060499773b0f37ff28d2 (diff) | |
parent | 04b0e9fcea6c60257d7bc68994103eacb340a82b (diff) | |
download | Viper-760031c079ce830755ba4fea029e149f4140e00b.tar.gz Viper-760031c079ce830755ba4fea029e149f4140e00b.zip |
Merge branch 'main' into linux-launch-v2linux-launch-v2
Diffstat (limited to 'src/app/js/browser.js')
-rw-r--r-- | src/app/js/browser.js | 225 |
1 files changed, 110 insertions, 115 deletions
diff --git a/src/app/js/browser.js b/src/app/js/browser.js index 06dc244..21bdf3f 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -1,18 +1,28 @@ +const Fuse = require("fuse.js"); +const ipcRenderer = require("electron").ipcRenderer; + +const lang = require("../../lang"); + +const popups = require("./popups"); +const toasts = require("./toasts"); +const request = require("./request"); + var browser_fuse; var packages = []; var packagecount = 0; -var mod_versions = {}; +var browser_el = document.getElementById("browser") -var Browser = { +var browser = { maxentries: 50, + mod_versions: {}, filters: { getpkgs: () => { let pkgs = []; let other = []; for (let i in packages) { - if (! Browser.filters.isfiltered(packages[i].categories)) { + if (! browser.filters.isfiltered(packages[i].categories)) { pkgs.push(packages[i]); } else { other.push(packages[i]); @@ -24,7 +34,7 @@ var Browser = { get: () => { let filtered = []; let unfiltered = []; - let checks = browser.querySelectorAll("#filters .check"); + let checks = browser_el.querySelectorAll("#filters .check"); for (let i = 0; i < checks.length; i++) { if (! checks[i].classList.contains("checked")) { @@ -40,8 +50,8 @@ var Browser = { }; }, isfiltered: (categories) => { - let filtered = Browser.filters.get().filtered; - let unfiltered = Browser.filters.get().unfiltered; + let filtered = browser.filters.get().filtered; + let unfiltered = browser.filters.get().unfiltered; let state = false; let filters = [ @@ -86,19 +96,19 @@ var Browser = { }, }, toggle: (state) => { - browser.scrollTo(0, 0); + browser_el.scrollTo(0, 0); popups.set("#browser", state); if (state) { if (browserEntries.querySelectorAll(".el").length == 0) { - Browser.loadfront(); + browser.loadfront(); } } else if (state === false) { - Browser.filters.toggle(false); + browser.filters.toggle(false); } }, install: (package_obj, clear_queue = false) => { - return installFromURL( + return mods.install_from_url( package_obj.download || package_obj.versions[0].download_url, package_obj.dependencies || package_obj.versions[0].dependencies, clear_queue, @@ -111,7 +121,7 @@ var Browser = { add_pkg_properties: () => { for (let i = 0; i < packages.length; i++) { let properties = packages[i]; - let normalized = normalize(packages[i].name); + let normalized = mods.normalize(packages[i].name); let has_update = false; let local_name = false; @@ -119,11 +129,11 @@ var Browser = { let remote_version = packages[i].versions[0].version_number; remote_version = version.format(remote_version); - if (modsobj) { - for (let ii = 0; ii < modsobj.all.length; ii++) { - let mod = modsobj.all[ii]; + if (mods.list()) { + for (let ii = 0; ii < mods.list().all.length; ii++) { + let mod = mods.list().all[ii]; - if (normalize(mod.name) !== normalized && ( + if (mods.normalize(mod.name) !== normalized && ( ! mod.package || mod.package.author + "-" + mod.package.package_name !== packages[i].full_name @@ -140,7 +150,7 @@ var Browser = { } let install = () => { - return Browser.install({...properties}); + return browser.install({...properties}); } packages[i].install = install; @@ -148,7 +158,7 @@ var Browser = { packages[i].local_version = local_version; if (local_version) { - mod_versions[normalized] = { + browser.mod_versions[normalized] = { install: install, has_update: has_update, local_name: local_name, @@ -160,7 +170,7 @@ var Browser = { } }, loadfront: async () => { - Browser.loading(); + browser.loading(); packagecount = 0; @@ -181,26 +191,26 @@ var Browser = { console.error(err) } - Browser.add_pkg_properties(); + browser.add_pkg_properties(); browser_fuse = new Fuse(packages, { keys: ["full_name"] }) } - let pkgs = Browser.filters.getpkgs(); + let pkgs = browser.filters.getpkgs(); for (let i in pkgs) { - if (packagecount >= Browser.maxentries) { - Browser.endoflist(); + if (packagecount >= browser.maxentries) { + browser.endoflist(); break } - new BrowserElFromObj(pkgs[i]); + browser.mod_el_from_obj(pkgs[i]); packagecount++; } }, loading: (string) => { - if (Browser.filters.get().unfiltered.length == 0) { + if (browser.filters.get().unfiltered.length == 0) { string = lang("gui.browser.no_results"); } @@ -214,7 +224,7 @@ var Browser = { }, endoflist: (is_end) => { let pkgs = []; - let filtered = Browser.filters.getpkgs(); + let filtered = browser.filters.getpkgs(); for (let i = 0; i < filtered.length; i++) { if (filtered[packagecount + i]) { pkgs.push(filtered[packagecount + i]); @@ -228,26 +238,26 @@ var Browser = { } if (pkgs.length == 0 || is_end) { - Browser.msg(`${lang('gui.browser.endoflist')}`); + browser.msg(`${lang('gui.browser.endoflist')}`); return } - Browser.msg(`<button id="loadmore">` + + browser.msg(`<button id="loadmore">` + `<img src="icons/down.png">` + `<span>${lang("gui.browser.load_more")}</span>` + `</button>`); loadmore.addEventListener("click", () => { - Browser.loadpkgs(pkgs); - Browser.endoflist(! pkgs.length); + browser.loadpkgs(pkgs); + browser.endoflist(! pkgs.length); }) }, search: (string) => { - Browser.loading(); + browser.loading(); let res = browser_fuse.search(string); if (res.length < 1) { - Browser.loading(lang("gui.browser.no_results")); + browser.loading(lang("gui.browser.no_results")); return } @@ -255,18 +265,18 @@ var Browser = { let count = 0; for (let i = 0; i < res.length; i++) { - if (count >= Browser.maxentries) {break} - if (Browser.filters.isfiltered(res[i].item.categories)) {continue} - new BrowserElFromObj(res[i].item); + if (count >= browser.maxentries) {break} + if (browser.filters.isfiltered(res[i].item.categories)) {continue} + browser.mod_el_from_obj(res[i].item); count++; } if (count < 1) { - Browser.loading(lang("gui.browser.no_results")); + browser.loading(lang("gui.browser.no_results")); } }, setbutton: (mod, string, icon) => { - mod = normalize(mod); + mod = mods.normalize(mod); if (browserEntries.querySelector(`#mod-${mod}`)) { let elems = browserEntries.querySelectorAll(`.el#mod-${mod}`); @@ -281,21 +291,21 @@ var Browser = { } else { let make = (str) => { if (browserEntries.querySelector(`#mod-${str}`)) { - return Browser.setbutton(str, string, icon); + return browser.setbutton(str, string, icon); } else { return false; } } setTimeout(() => { - for (let i = 0; i < modsobj.all.length; i++) { - let modname = normalize(modsobj.all[i].name); - let modfolder = normalize(modsobj.all[i].folder_name); + for (let i = 0; i < mods.list().all.length; i++) { + let modname = mods.normalize(mods.list().all[i].name); + let modfolder = mods.normalize(mods.list().all[i].folder_name); if (mod.includes(modname)) { if (! make(modname)) { - if (modsobj.all[i].manifest_name) { - make(normalize(modsobj.all[i].manifest_name)); + if (mods.list().all[i].manifest_name) { + make(mods.normalize(mods.list().all[i].manifest_name)); } } } @@ -313,17 +323,17 @@ var Browser = { let count = 0; for (let i in pkgs) { - if (count >= Browser.maxentries) { + if (count >= browser.maxentries) { if (pkgs[i] === undefined) { - Browser.endoflist(true); + browser.endoflist(true); } - Browser.endoflist(); + browser.endoflist(); break } try { - new BrowserElFromObj(pkgs[i]); + browser.mod_el_from_obj(pkgs[i]); }catch(e) {} count++; @@ -339,18 +349,14 @@ var Browser = { } } -setInterval(Browser.add_pkg_properties, 1500); +setInterval(browser.add_pkg_properties, 1500); if (navigator.onLine) { - Browser.loadfront(); -} - -function openExternal(url) { - require("electron").shell.openExternal(url); + browser.loadfront(); } var view = document.querySelector(".popup#preview webview"); -var Preview = { +browser.preview = { show: () => { preview.classList.add("shown"); }, @@ -358,16 +364,21 @@ var Preview = { preview.classList.remove("shown"); }, set: (url, autoshow) => { - if (autoshow != false) {Preview.show()} + if (autoshow != false) {browser.preview.show()} + view.src = url; - document.querySelector("#preview #external").setAttribute("onclick", `openExternal("${url}")`); + + document.querySelector("#preview #external").setAttribute( + "onclick", + `require("electron").shell.openExternal("${url}")` + ) } } -function BrowserElFromObj(obj) { +browser.mod_el_from_obj = (obj) => { let pkg = {...obj, ...obj.versions[0]}; - new BrowserEl({ + browser.mod_el({ pkg: pkg, title: pkg.name, image: pkg.icon, @@ -381,8 +392,8 @@ function BrowserElFromObj(obj) { }) } -function BrowserEl(properties) { - if (Browser.filters.isfiltered(properties.categories)) {return} +browser.mod_el = (properties) => { + if (browser.filters.isfiltered(properties.categories)) {return} properties = { title: "No name", @@ -405,8 +416,8 @@ function BrowserEl(properties) { let installstr = lang("gui.browser.install"); let normalized_mods = []; - for (let i = 0; i < modsobj.all; i++) { - normalized_mods.push(normalize(mods_list[i].name)); + for (let i = 0; i < mods.list().all; i++) { + normalized_mods.push(mods.normalize(mods_list[i].name)); } if (properties.pkg.local_version) { @@ -421,7 +432,7 @@ function BrowserEl(properties) { let entry = document.createElement("div"); entry.classList.add("el"); - entry.id = `mod-${normalize(properties.title)}`; + entry.id = `mod-${mods.normalize(properties.title)}`; entry.innerHTML = ` <div class="image"> @@ -435,7 +446,7 @@ function BrowserEl(properties) { <img src="icons/${installicon}.png"> <span>${installstr}</span> </button> - <button class="info" onclick="Preview.set('${properties.url}')"> + <button class="info" onclick="browser.preview.set('${properties.url}')"> <img src="icons/open.png"> <span>${lang('gui.browser.view')}</span> </button> @@ -449,7 +460,7 @@ function BrowserEl(properties) { ` entry.querySelector("button.install").addEventListener("click", () => { - Browser.install(properties); + browser.install(properties); }) browserEntries.appendChild(entry); @@ -466,21 +477,21 @@ function add_recent_toast(name, timeout = 3000) { }, timeout) } -ipcRenderer.on("removed-mod", (event, mod) => { - setButtons(true); - Browser.setbutton(mod.name, lang("gui.browser.install"), "downloads"); +ipcRenderer.on("removed-mod", (_, mod) => { + set_buttons(true); + browser.setbutton(mod.name, lang("gui.browser.install"), "downloads"); if (mod.manifest_name) { - Browser.setbutton(mod.manifest_name, lang("gui.browser.install"), "downloads"); + browser.setbutton(mod.manifest_name, lang("gui.browser.install"), "downloads"); } }) -ipcRenderer.on("failed-mod", (event, modname) => { +ipcRenderer.on("failed-mod", (_, modname) => { if (recent_toasts["failed" + modname]) {return} add_recent_toast("failed" + modname); - setButtons(true); - new Toast({ + set_buttons(true); + toasts.show({ timeout: 10000, scheme: "error", title: lang("gui.toast.title.failed"), @@ -488,12 +499,12 @@ ipcRenderer.on("failed-mod", (event, modname) => { }) }) -ipcRenderer.on("legacy-duped-mod", (event, modname) => { +ipcRenderer.on("legacy-duped-mod", (_, modname) => { if (recent_toasts["duped" + modname]) {return} add_recent_toast("duped" + modname); - setButtons(true); - new Toast({ + set_buttons(true); + toasts.show({ timeout: 10000, scheme: "warning", title: lang("gui.toast.title.duped"), @@ -501,9 +512,9 @@ ipcRenderer.on("legacy-duped-mod", (event, modname) => { }) }) -ipcRenderer.on("no-internet", (event, modname) => { - setButtons(true); - new Toast({ +ipcRenderer.on("no-internet", () => { + set_buttons(true); + toasts.show({ timeout: 10000, scheme: "error", title: lang("gui.toast.title.no_internet"), @@ -511,17 +522,17 @@ ipcRenderer.on("no-internet", (event, modname) => { }) }) -ipcRenderer.on("installed-mod", (event, mod) => { +ipcRenderer.on("installed-mod", (_, mod) => { if (recent_toasts["installed" + mod.name]) {return} add_recent_toast("installed" + mod.name); let name = mod.fancy_name || mod.name; - setButtons(true); - Browser.setbutton(name, lang("gui.browser.reinstall"), "redo"); + set_buttons(true); + browser.setbutton(name, lang("gui.browser.reinstall"), "redo"); if (mod.malformed) { - new Toast({ + toasts.show({ timeout: 8000, scheme: "warning", title: lang("gui.toast.title.malformed"), @@ -529,56 +540,38 @@ ipcRenderer.on("installed-mod", (event, mod) => { }) } - new Toast({ + toasts.show({ scheme: "success", title: lang("gui.toast.title.installed"), description: name + " " + lang("gui.toast.desc.installed") }) - if (installqueue.length != 0) { - installFromURL( - "https://thunderstore.io/package/download/" + installqueue[0].pkg, - false, false, installqueue[0].author, installqueue[0].package_name, installqueue[0].version + if (mods.install_queue.length != 0) { + mods.install_from_url( + "https://thunderstore.io/package/download/" + mods.install_queue[0].pkg, + false, false, mods.install_queue[0].author, mods.install_queue[0].package_name, mods.install_queue[0].version ) - installqueue.shift(); + mods.install_queue.shift(); } }) -function normalize(items) { - let main = (string) => { - return string.replaceAll(" ", "") - .replaceAll(".", "").replaceAll("-", "") - .replaceAll("_", "").toLowerCase(); - } - if (typeof items == "string") { - return main(items); - } else { - let newArray = []; - for (let i = 0; i < items.length; i++) { - newArray.push(main(items[i])); - } - - return newArray; - } -} - let searchtimeout; let searchstr = ""; let search = document.querySelector("#browser .search"); search.addEventListener("keyup", () => { - Browser.filters.toggle(false); + browser.filters.toggle(false); clearTimeout(searchtimeout); if (searchstr != search.value) { if (search.value.replaceAll(" ", "") == "") { searchstr = ""; - Browser.loadfront(); + browser.loadfront(); return } searchtimeout = setTimeout(() => { - Browser.search(search.value); + browser.search(search.value); searchstr = search.value; }, 500) } @@ -586,24 +579,24 @@ search.addEventListener("keyup", () => { let mouse_events = ["scroll", "mousedown", "touchdown"]; mouse_events.forEach((event) => { - document.body.addEventListener(event, (e) => { + document.body.addEventListener(event, () => { let mouse_at = document.elementsFromPoint(mouseX, mouseY); if (! mouse_at.includes(document.querySelector("#preview"))) { - Preview.hide(); + browser.preview.hide(); } if (! mouse_at.includes(document.querySelector("#filter")) && ! mouse_at.includes(document.querySelector(".overlay"))) { - Browser.filters.toggle(false); + browser.filters.toggle(false); } }) }); view.addEventListener("dom-ready", () => { let css = [ - fs.readFileSync(__dirname + "/css/theming.css", "utf8"), - fs.readFileSync(__dirname + "/css/webview.css", "utf8") + fs.readFileSync(__dirname + "/../css/theming.css", "utf8"), + fs.readFileSync(__dirname + "/../css/webview.css", "utf8") ] view.insertCSS(css.join(" ")); @@ -623,7 +616,7 @@ view.addEventListener("did-start-loading", () => { let mouseY = 0; let mouseX = 0; -browser.addEventListener("mousemove", (event) => { +browser_el.addEventListener("mousemove", (event) => { mouseY = event.clientY; mouseX = event.clientX; }) @@ -631,6 +624,8 @@ browser.addEventListener("mousemove", (event) => { let checks = document.querySelectorAll(".check"); for (let i = 0; i < checks.length; i++) { checks[i].setAttribute("onclick", - "this.classList.toggle('checked');Browser.loadfront();search.value = ''" + "this.classList.toggle('checked');browser.loadfront();search.value = ''" ) } + +module.exports = browser; |