From dbd8c6b152acc1188d2edd288488aa2da8f6310b Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 18:02:48 +0200 Subject: initial commit to better modularize frontend Far from done, but this pretty much splits everything inside `src/app/main.js` into separate files. --- src/app/js/browser.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'src/app/js/browser.js') diff --git a/src/app/js/browser.js b/src/app/js/browser.js index 06dc244..00a0933 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -98,7 +98,7 @@ var Browser = { } }, 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, @@ -119,9 +119,9 @@ 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 && ( ! mod.package || @@ -288,14 +288,14 @@ var Browser = { } 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 = normalize(mods.list().all[i].name); + let modfolder = 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(normalize(mods.list().all[i].manifest_name)); } } } @@ -405,7 +405,7 @@ function BrowserEl(properties) { let installstr = lang("gui.browser.install"); let normalized_mods = []; - for (let i = 0; i < modsobj.all; i++) { + for (let i = 0; i < mods.list().all; i++) { normalized_mods.push(normalize(mods_list[i].name)); } @@ -467,7 +467,7 @@ function add_recent_toast(name, timeout = 3000) { } ipcRenderer.on("removed-mod", (event, mod) => { - setButtons(true); + set_buttons(true); Browser.setbutton(mod.name, lang("gui.browser.install"), "downloads"); if (mod.manifest_name) { @@ -479,7 +479,7 @@ ipcRenderer.on("failed-mod", (event, modname) => { if (recent_toasts["failed" + modname]) {return} add_recent_toast("failed" + modname); - setButtons(true); + set_buttons(true); new Toast({ timeout: 10000, scheme: "error", @@ -492,7 +492,7 @@ ipcRenderer.on("legacy-duped-mod", (event, modname) => { if (recent_toasts["duped" + modname]) {return} add_recent_toast("duped" + modname); - setButtons(true); + set_buttons(true); new Toast({ timeout: 10000, scheme: "warning", @@ -502,7 +502,7 @@ ipcRenderer.on("legacy-duped-mod", (event, modname) => { }) ipcRenderer.on("no-internet", (event, modname) => { - setButtons(true); + set_buttons(true); new Toast({ timeout: 10000, scheme: "error", @@ -517,7 +517,7 @@ ipcRenderer.on("installed-mod", (event, mod) => { let name = mod.fancy_name || mod.name; - setButtons(true); + set_buttons(true); Browser.setbutton(name, lang("gui.browser.reinstall"), "redo"); if (mod.malformed) { @@ -535,13 +535,13 @@ ipcRenderer.on("installed-mod", (event, mod) => { 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(); } }) -- cgit v1.2.3 From 05b13e99a2ec632c63557c52ec22677b022e7d69 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 18:20:53 +0200 Subject: convert js/toast.js to a CommonJS module Renamed from `toast.js` to `toasts.js` as well --- src/app/index.html | 1 - src/app/js/browser.js | 10 +++---- src/app/js/toast.js | 71 ----------------------------------------------- src/app/js/toasts.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/app/main.js | 5 ++-- 5 files changed, 85 insertions(+), 79 deletions(-) delete mode 100644 src/app/js/toast.js create mode 100644 src/app/js/toasts.js (limited to 'src/app/js/browser.js') diff --git a/src/app/index.html b/src/app/index.html index 4b89999..5d5f0fc 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -333,7 +333,6 @@ - diff --git a/src/app/js/browser.js b/src/app/js/browser.js index 00a0933..9a1298a 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -480,7 +480,7 @@ ipcRenderer.on("failed-mod", (event, modname) => { add_recent_toast("failed" + modname); set_buttons(true); - new Toast({ + toasts.show({ timeout: 10000, scheme: "error", title: lang("gui.toast.title.failed"), @@ -493,7 +493,7 @@ ipcRenderer.on("legacy-duped-mod", (event, modname) => { add_recent_toast("duped" + modname); set_buttons(true); - new Toast({ + toasts.show({ timeout: 10000, scheme: "warning", title: lang("gui.toast.title.duped"), @@ -503,7 +503,7 @@ ipcRenderer.on("legacy-duped-mod", (event, modname) => { ipcRenderer.on("no-internet", (event, modname) => { set_buttons(true); - new Toast({ + toasts.show({ timeout: 10000, scheme: "error", title: lang("gui.toast.title.no_internet"), @@ -521,7 +521,7 @@ ipcRenderer.on("installed-mod", (event, mod) => { 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,7 +529,7 @@ 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") diff --git a/src/app/js/toast.js b/src/app/js/toast.js deleted file mode 100644 index e0aba6b..0000000 --- a/src/app/js/toast.js +++ /dev/null @@ -1,71 +0,0 @@ -function Toast(properties) { - let toast = { - timeout: 3000, - fg: "#FFFFFF", - bg: "var(--selbg)", - callback: () => {}, - title: "Untitled Toast", - description: "No description provided for toast", - ...properties - } - - switch(toast.scheme) { - case "error": - toast.fg = "#FFFFFF"; - toast.bg = "rgb(var(--red))"; - break - case "success": - toast.fg = "#FFFFFF"; - toast.bg = "#60D394"; - break - case "warning": - toast.fg = "#FFFFFF"; - toast.bg = "#FF9B85"; - break - } - - - let id = Date.now(); - if (document.getElementById(id)) {id = id + 1} - let el = document.createElement("div"); - - el.classList.add("toast"); - - el.style.color = toast.fg; - el.style.background = toast.bg; - - el.id = id; - el.addEventListener("click", () => { - dismissToast(id); - toast.callback(); - }) - - el.innerHTML = ` -
${toast.title}
-
${toast.description}
- ` - - if (! toast.title) { - el.querySelector(".title").remove(); - } - - if (! toast.description) { - el.querySelector(".description").remove(); - } - - toasts.appendChild(el); - - setTimeout(() => { - dismissToast(id); - }, toast.timeout) -} - -function dismissToast(id) { - id = document.getElementById(id); - if (id) { - id.classList.add("hidden"); - setTimeout(() => { - id.remove(); - }, 500) - } -} diff --git a/src/app/js/toasts.js b/src/app/js/toasts.js new file mode 100644 index 0000000..c3bba99 --- /dev/null +++ b/src/app/js/toasts.js @@ -0,0 +1,77 @@ +let toasts = {}; + +toasts.show = (properties) => { + let toast = { + timeout: 3000, + fg: "#FFFFFF", + bg: "var(--selbg)", + callback: () => {}, + title: "Untitled Toast", + description: "No description provided for toast", + ...properties + } + + switch(toast.scheme) { + case "error": + toast.fg = "#FFFFFF"; + toast.bg = "rgb(var(--red))"; + break + case "success": + toast.fg = "#FFFFFF"; + toast.bg = "#60D394"; + break + case "warning": + toast.fg = "#FFFFFF"; + toast.bg = "#FF9B85"; + break + } + + + let id = Date.now(); + if (document.getElementById(id)) {id = id + 1} + let el = document.createElement("div"); + + el.classList.add("toast"); + + el.style.color = toast.fg; + el.style.background = toast.bg; + + el.id = id; + el.addEventListener("click", () => { + toasts.dismiss(id); + toast.callback(); + }) + + el.innerHTML = ` +
${toast.title}
+
${toast.description}
+ ` + + if (! toast.title) { + el.querySelector(".title").remove(); + } + + if (! toast.description) { + el.querySelector(".description").remove(); + } + + document.getElementById("toasts").appendChild(el); + + setTimeout(() => { + toasts.dismiss(id); + }, toast.timeout) +} + +// dismissed/closes toasts with `id` as their ID +toasts.dismiss = (id) => { + id = document.getElementById(id); + + if (id) { + id.classList.add("hidden"); + setTimeout(() => { + id.remove(); + }, 500) + } +} + +module.exports = toasts; diff --git a/src/app/main.js b/src/app/main.js index fa4ec86..9ad6344 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -6,13 +6,13 @@ const { app, ipcRenderer, shell } = require("electron"); const lang = require("../lang"); ipcRenderer.on("unknown-error", (event, err) => { - new Toast({ + toasts.show({ timeout: 10000, scheme: "error", title: lang("gui.toast.title.unknown_error"), description: lang("gui.toast.desc.unknown_error"), callback: () => { - new Toast({ + toasts.show({ timeout: 15000, scheme: "error", title: "", @@ -28,6 +28,7 @@ const json = require("../modules/json"); const kill = require("./js/kill"); const mods = require("./js/mods"); +const toasts = require("./js/toasts"); const update = require("./js/update"); const events = require("./js/events"); const launch = require("./js/launch"); -- cgit v1.2.3 From ceb90ab941a159d63ea5cace1c24dde6b8b96a86 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 18:26:13 +0200 Subject: move browser.js' normalize() into mods.js --- src/app/js/browser.js | 34 ++++++++-------------------------- src/app/js/mods.js | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 27 deletions(-) (limited to 'src/app/js/browser.js') diff --git a/src/app/js/browser.js b/src/app/js/browser.js index 9a1298a..ec5a31d 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -111,7 +111,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; @@ -123,7 +123,7 @@ var Browser = { 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 @@ -266,7 +266,7 @@ var Browser = { } }, setbutton: (mod, string, icon) => { - mod = normalize(mod); + mod = mods.normalize(mod); if (browserEntries.querySelector(`#mod-${mod}`)) { let elems = browserEntries.querySelectorAll(`.el#mod-${mod}`); @@ -289,13 +289,13 @@ var Browser = { setTimeout(() => { for (let i = 0; i < mods.list().all.length; i++) { - let modname = normalize(mods.list().all[i].name); - let modfolder = normalize(mods.list().all[i].folder_name); + 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 (mods.list().all[i].manifest_name) { - make(normalize(mods.list().all[i].manifest_name)); + make(mods.normalize(mods.list().all[i].manifest_name)); } } } @@ -406,7 +406,7 @@ function BrowserEl(properties) { let normalized_mods = []; for (let i = 0; i < mods.list().all; i++) { - normalized_mods.push(normalize(mods_list[i].name)); + normalized_mods.push(mods.normalize(mods_list[i].name)); } if (properties.pkg.local_version) { @@ -421,7 +421,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 = `
@@ -545,24 +545,6 @@ ipcRenderer.on("installed-mod", (event, mod) => { } }) -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"); diff --git a/src/app/js/mods.js b/src/app/js/mods.js index caa66a7..7110d7e 100644 --- a/src/app/js/mods.js +++ b/src/app/js/mods.js @@ -29,7 +29,7 @@ mods.load = (mods_obj) => { name = mod.package.package_name; } - let normalized_name = "mod-list-" + normalize(name); + let normalized_name = "mod-list-" + mods.normalize(name); normalized_names.push(normalized_name); @@ -302,6 +302,23 @@ mods.is_installed = (modname) => { return false; } +mods.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; + } +} // updates the installed mods ipcRenderer.on("mods", (event, mods_obj) => { -- cgit v1.2.3 From 957f2d2ff52c9917960c4c8d55eab3b44f0453bb Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 20:17:48 +0200 Subject: convert js/browser.js to a CommonJS module And `js/popups.js`, but it was already technically a CommonJS module, it was just leftover in `index.html` due to `js/browser.js` not being a CommonJS module. --- src/app/index.html | 10 ++-- src/app/js/browser.js | 143 ++++++++++++++++++++++++++--------------------- src/app/js/dom_events.js | 2 +- src/app/js/mods.js | 14 ++--- src/app/main.js | 2 + 5 files changed, 92 insertions(+), 79 deletions(-) (limited to 'src/app/js/browser.js') diff --git a/src/app/index.html b/src/app/index.html index a591a26..ffc13de 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -205,10 +205,10 @@
- -
@@ -218,7 +218,7 @@