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/index.html | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'src/app/index.html') diff --git a/src/app/index.html b/src/app/index.html index 016b708..87a1029 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -12,7 +12,7 @@
-
+
@@ -34,11 +34,11 @@
-
@@ -147,9 +147,9 @@
- - - + + +
@@ -181,12 +181,12 @@
- - - - - - + + + + + +
@@ -247,7 +247,7 @@
| - %%gui.setpath%% + %%gui.setpath%%
@@ -277,10 +277,10 @@
- +
@@ -298,7 +298,7 @@ %%gui.mods.toggle_all%% - @@ -320,10 +320,11 @@
- +
@@ -331,16 +332,13 @@
- - - -- cgit v1.2.3 From 7f011d34a2d5e13ba312b44ce6871e48a495ea8f Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 18:10:06 +0200 Subject: replace js/misc.js file with js/version.js module --- src/app/index.html | 1 - src/app/js/misc.js | 44 -------------------------------------------- src/app/js/version.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/app/main.js | 1 + 4 files changed, 45 insertions(+), 45 deletions(-) delete mode 100644 src/app/js/misc.js create mode 100644 src/app/js/version.js (limited to 'src/app/index.html') diff --git a/src/app/index.html b/src/app/index.html index 87a1029..1297d0a 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -333,7 +333,6 @@ - diff --git a/src/app/js/misc.js b/src/app/js/misc.js deleted file mode 100644 index b35f239..0000000 --- a/src/app/js/misc.js +++ /dev/null @@ -1,44 +0,0 @@ -version = { - is_newer: (version1, version2) => { - version1 = version.format(version1, true).split("."); - version2 = version.format(version2, true).split("."); - - for (let i = 0; i < version1.length; i++) { - - - let nums = [ - parseInt(version1[i]) || 0, - parseInt(version2[i]) || 0 - ]; - if (nums[0] > nums[1]) { - return true; - } else if (nums[0] < nums[1]) { - return false; - } - } - - return false; - }, - format: (version_number, no_leading_v) => { - version_number = version_number.trim(); - - if (no_leading_v) { - if (version_number[0] == "v") { - return version_number.slice(1, version_number.length); - } - - return version_number; - if (no_leading_v) { - return version_number - } - - return "v" + version_number; - } else { - if (version_number[0] != "v") { - return "v" + version_number; - } - } - - return version_number; - } -} diff --git a/src/app/js/version.js b/src/app/js/version.js new file mode 100644 index 0000000..97223f9 --- /dev/null +++ b/src/app/js/version.js @@ -0,0 +1,44 @@ +module.exports = { + is_newer: (version1, version2) => { + version1 = version.format(version1, true).split("."); + version2 = version.format(version2, true).split("."); + + for (let i = 0; i < version1.length; i++) { + + + let nums = [ + parseInt(version1[i]) || 0, + parseInt(version2[i]) || 0 + ]; + if (nums[0] > nums[1]) { + return true; + } else if (nums[0] < nums[1]) { + return false; + } + } + + return false; + }, + format: (version_number, no_leading_v) => { + version_number = version_number.trim(); + + if (no_leading_v) { + if (version_number[0] == "v") { + return version_number.slice(1, version_number.length); + } + + return version_number; + if (no_leading_v) { + return version_number + } + + return "v" + version_number; + } else { + if (version_number[0] != "v") { + return "v" + version_number; + } + } + + return version_number; + } +} diff --git a/src/app/main.js b/src/app/main.js index 2ed3053..11f5853 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -30,6 +30,7 @@ const kill = require("./js/kill"); const update = require("./js/update"); const events = require("./js/events"); const launch = require("./js/launch"); +const version = require("./js/version"); const request = require("./js/request"); const process = require("./js/process"); const settings = require("./js/settings"); -- cgit v1.2.3 From a8e0f5f0435ed6cedb4ff389ee31c210dab26100 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 18:12:13 +0200 Subject: convert js/mods.js to a CommonJS module --- src/app/index.html | 1 - src/app/js/mods.js | 13 +++++++++++-- src/app/main.js | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) (limited to 'src/app/index.html') diff --git a/src/app/index.html b/src/app/index.html index 1297d0a..4b89999 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -333,7 +333,6 @@ - diff --git a/src/app/js/mods.js b/src/app/js/mods.js index e105c7c..caa66a7 100644 --- a/src/app/js/mods.js +++ b/src/app/js/mods.js @@ -1,3 +1,10 @@ +const ipcRenderer = require("electron").ipcRenderer; + +const lang = require("../../lang"); + +const version = require("./version"); +const set_buttons = require("./set_buttons"); + let mods = {}; let mods_list = { @@ -15,7 +22,7 @@ mods.load = (mods_obj) => { modcount.innerHTML = `${lang("gui.mods.count")} ${mods_obj.all.length}`; let normalized_names = []; - + let set_mod = (mod) => { let name = mod.name; if (mod.package) { @@ -185,7 +192,7 @@ mods.load = (mods_obj) => { mods.toggle(mod_versions[mod].local_name); } }) - + mod_els[i].remove(); modsdiv.querySelector(".line").after(mod_el); } else { @@ -303,3 +310,5 @@ ipcRenderer.on("mods", (event, mods_obj) => { mods.load(mods_obj); }) + +module.exports = mods; diff --git a/src/app/main.js b/src/app/main.js index 11f5853..fa4ec86 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -27,6 +27,7 @@ ipcRenderer.on("unknown-error", (event, err) => { const json = require("../modules/json"); const kill = require("./js/kill"); +const mods = require("./js/mods"); const update = require("./js/update"); const events = require("./js/events"); const launch = require("./js/launch"); -- 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/index.html') 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 0577977182e7f4be2722f54ae50ff23aa8fe9032 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 18:36:27 +0200 Subject: convert js/launcher.js to a CommonJS module --- src/app/index.html | 19 +++++++++---------- src/app/js/gamepath.js | 3 ++- src/app/js/launcher.js | 40 ++++++++++++++++++++++------------------ src/app/main.js | 1 + 4 files changed, 34 insertions(+), 29 deletions(-) (limited to 'src/app/index.html') diff --git a/src/app/index.html b/src/app/index.html index 5d5f0fc..d6df23e 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -230,17 +230,17 @@
    -
  • %%viper.menu.main%%
  • -
  • %%viper.menu.release%%
  • -
  • %%viper.menu.info%%
  • +
  • %%viper.menu.main%%
  • +
  • %%viper.menu.release%%
  • +
  • %%viper.menu.info%%
@@ -269,9 +269,9 @@
    -
  • %%ns.menu.main%%
  • -
  • %%ns.menu.mods%%
  • -
  • %%ns.menu.release%%
  • +
  • %%ns.menu.main%%
  • +
  • %%ns.menu.mods%%
  • +
  • %%ns.menu.release%%
@@ -336,6 +336,5 @@ - diff --git a/src/app/js/gamepath.js b/src/app/js/gamepath.js index 80e3158..c3e5c2b 100644 --- a/src/app/js/gamepath.js +++ b/src/app/js/gamepath.js @@ -2,6 +2,7 @@ const ipcRenderer = require("electron").ipcRenderer; const lang = require("../../lang"); const process = require("./process"); +const launcher = require("./launcher"); const settings = require("./settings"); // frontend part of settings a new game path @@ -16,7 +17,7 @@ ipcRenderer.on("newpath", (_, newpath) => { // a previously valid gamepath no longer exists, and is therefore lost ipcRenderer.on("gamepath-lost", () => { - page(0); + launcher.change_page(0); set_buttons(false, true); alert(lang("gui.gamepath.lost")); }) diff --git a/src/app/js/launcher.js b/src/app/js/launcher.js index 6fe1686..913762b 100644 --- a/src/app/js/launcher.js +++ b/src/app/js/launcher.js @@ -1,11 +1,13 @@ const markdown = require("marked").parse; +let launcher = {}; + var servercount; var playercount; var masterserver; // changes the main page, this is the tabs in the sidebar -function page(page) { +launcher.change_page = (page) => { let btns = document.querySelectorAll(".gamesContainer button"); let pages = document.querySelectorAll(".mainContainer .contentContainer"); @@ -20,9 +22,9 @@ function page(page) { pages[page].classList.remove("hidden"); btns[page].classList.remove("inactive"); bgHolder.setAttribute("bg", page); -}; page(1) +}; launcher.change_page(1) -function formatRelease(notes) { +launcher.format_release = (notes) => { if (! notes) {return ""} let content = ""; @@ -61,7 +63,7 @@ function formatRelease(notes) { // sets content of `div` to a single release block with centered text // inside it, the text being `lang(lang_key)` -let set_error_content = (div, lang_key) => { +launcher.error = (div, lang_key) => { div.innerHTML = "
" + "

" + @@ -73,39 +75,39 @@ let set_error_content = (div, lang_key) => { // updates the Viper release notes ipcRenderer.on("vp-notes", (event, response) => { if (! response) { - return set_error_content( + return launcher.error( vpReleaseNotes, "request.no_vp_release_notes" ) } - vpReleaseNotes.innerHTML = formatRelease(response); + vpReleaseNotes.innerHTML = launcher.format_release(response); }); // updates the Northstar release notes ipcRenderer.on("ns-notes", (event, response) => { if (! response) { - return set_error_content( + return launcher.error( nsRelease, "request.no_ns_release_notes" ) } - nsRelease.innerHTML = formatRelease(response); + nsRelease.innerHTML = launcher.format_release(response); }); -async function loadVpReleases() { +launcher.load_vp_notes = async () => { ipcRenderer.send("get-vp-notes"); -}; loadVpReleases(); +}; launcher.load_vp_notes(); -async function loadNsReleases() { +launcher.load_ns_notes = async () => { ipcRenderer.send("get-ns-notes"); -}; loadNsReleases(); +}; launcher.load_ns_notes(); // TODO: We gotta make this more automatic instead of switch statements // it's both not pretty, but adding more sections requires way too much // effort, compared to how it should be. -function showVpSection(section) { +launcher.show_vp = (section) => { if (!["main", "release", "info", "credits"].includes(section)) throw new Error("unknown vp section"); vpMainBtn.removeAttribute("active"); vpReleaseBtn.removeAttribute("active"); @@ -131,8 +133,8 @@ function showVpSection(section) { } } -function showNsSection(section) { - if (!["main", "release", "mods"].includes(section)) { +launcher.show_ns = (section) => { + if (! ["main", "release", "mods"].includes(section)) { throw new Error("unknown ns section"); } @@ -161,7 +163,7 @@ function showNsSection(section) { } } -async function loadServers() { +launcher.check_servers = async () => { serverstatus.classList.add("checking"); try { @@ -205,9 +207,11 @@ async function loadServers() { serverstatus.innerHTML = lang("gui.server.offline"); } -}; loadServers() +}; launcher.check_servers() // refreshes every 5 minutes setInterval(() => { - loadServers(); + launcher.check_servers(); }, 300000) + +module.exports = launcher; diff --git a/src/app/main.js b/src/app/main.js index 9ad6344..b72c1f9 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -37,6 +37,7 @@ const request = require("./js/request"); const process = require("./js/process"); const settings = require("./js/settings"); const gamepath = require("./js/gamepath"); +const launcher = require("./js/launcher"); const is_running = require("./js/is_running"); const set_buttons = require("./js/set_buttons"); -- cgit v1.2.3 From d4d95da09a04cc8b07fbcf6e179f4edb5e662b98 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 8 Jun 2024 18:38:11 +0200 Subject: convert js/tooltip.js to a CommonJS module It's not actually used anywhere outside of itself, but oh well. --- src/app/index.html | 1 - src/app/js/tooltip.js | 2 ++ src/app/main.js | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) (limited to 'src/app/index.html') diff --git a/src/app/index.html b/src/app/index.html index d6df23e..a591a26 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -334,7 +334,6 @@ - diff --git a/src/app/js/tooltip.js b/src/app/js/tooltip.js index 13dcd67..f2d97a0 100644 --- a/src/app/js/tooltip.js +++ b/src/app/js/tooltip.js @@ -128,3 +128,5 @@ document.addEventListener("mousedown", tooltip_event); document.addEventListener("mousemove", tooltip_event); document.addEventListener("mouseenter", tooltip_event); document.addEventListener("mouseleave", tooltip_event); + +module.exports = tooltip; diff --git a/src/app/main.js b/src/app/main.js index b72c1f9..04055a8 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -32,6 +32,7 @@ const toasts = require("./js/toasts"); const update = require("./js/update"); const events = require("./js/events"); const launch = require("./js/launch"); +const tooltip = require("./js/tooltip"); const version = require("./js/version"); const request = require("./js/request"); const process = require("./js/process"); -- 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/index.html') 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 @@