From 47f730b76ee2a600482aec6182ba15ce103089a9 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Wed, 24 Jan 2024 22:23:48 +0100 Subject: [PATCH] Introduction of Javascript modules for frontend --- hal-core/src/se/hal/HalServer.java | 10 ++++ .../src/se/hal/intf/HalJavascriptModule.java | 50 +++++++++++++++++++ hal-core/src/se/hal/intf/HalWebPage.java | 12 +++++ .../src/se/hal/page/JavascriptModules.java | 24 +++++++++ 4 files changed, 96 insertions(+) create mode 100644 hal-core/src/se/hal/intf/HalJavascriptModule.java create mode 100644 hal-core/src/se/hal/page/JavascriptModules.java diff --git a/hal-core/src/se/hal/HalServer.java b/hal-core/src/se/hal/HalServer.java index ff46d73f..59f79041 100644 --- a/hal-core/src/se/hal/HalServer.java +++ b/hal-core/src/se/hal/HalServer.java @@ -3,6 +3,7 @@ package se.hal; import se.hal.daemon.HalExternalWebDaemon; import se.hal.intf.*; +import se.hal.intf.HalJavascriptModule.HalJsModule; import se.hal.page.StartupWebPage; import se.hal.struct.PluginConfig; import zutil.db.DBConnection; @@ -141,6 +142,15 @@ public class HalServer { registerPage(it.next()); for (Iterator it = pluginManager.getSingletonIterator(HalWebPage.class); it.hasNext(); ) registerPage(it.next()); + for (Iterator it = pluginManager.getSingletonIterator(HalJavascriptModule.class); it.hasNext(); ) { + HalJsModule[] jsModules = it.next().getJavascriptModules(); + + if (jsModules != null) { + for (HalJsModule module : jsModules) + HalWebPage.addJavascriptModule(module); + } + } + } catch (Exception e) { logger.log(Level.SEVERE, "Startup failed.", e); diff --git a/hal-core/src/se/hal/intf/HalJavascriptModule.java b/hal-core/src/se/hal/intf/HalJavascriptModule.java new file mode 100644 index 00000000..a398ae61 --- /dev/null +++ b/hal-core/src/se/hal/intf/HalJavascriptModule.java @@ -0,0 +1,50 @@ +package se.hal.intf; + +/** + * A plugin interface for configuring Javascript modules + */ +public interface HalJavascriptModule { + + /** + * @return a List of Objects defining the Javascript configuration + */ + HalJsModule[] getJavascriptModules(); + + + /** + * Defines configuration for a Javascript module to be loaded during webpage generation. + */ + class HalJsModule { + private String moduleName; + private String scriptPath; + + public HalJsModule(String moduleName, String scriptPath) { + this.moduleName = moduleName; + this.scriptPath = scriptPath; + } + + public String getModuleName() { + return moduleName; + } + + public String getScriptPath() { + return scriptPath; + } + } + + /** + * Declares a Javascript module to be assigned to a specific web address. + */ + class HalJsModulePage extends HalJsModule { + private String page; + + public HalJsModulePage(String moduleName, String scriptPath, String page) { + super(moduleName, scriptPath); + this.page = page; + } + + public String getPage() { + return page; + } + } +} diff --git a/hal-core/src/se/hal/intf/HalWebPage.java b/hal-core/src/se/hal/intf/HalWebPage.java index dc43dc68..5a147a97 100644 --- a/hal-core/src/se/hal/intf/HalWebPage.java +++ b/hal-core/src/se/hal/intf/HalWebPage.java @@ -1,6 +1,8 @@ package se.hal.intf; import se.hal.HalContext; +import se.hal.intf.HalJavascriptModule.HalJsModule; +import se.hal.intf.HalJavascriptModule.HalJsModulePage; import se.hal.struct.User; import zutil.db.DBConnection; import zutil.io.file.FileUtil; @@ -11,6 +13,7 @@ import zutil.parser.Templator; import zutil.ui.Navigation; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,6 +26,8 @@ public abstract class HalWebPage implements HttpPage{ private static Navigation rootNav = Navigation.createRootNav(); private static Navigation userNav = Navigation.createRootNav(); + private static List jsModules = new ArrayList<>(); + private static List jsPages = new ArrayList<>(); private String pageId; private boolean showSubNav; @@ -76,6 +81,8 @@ public abstract class HalWebPage implements HttpPage{ main.setAll(data); main.set("navigation", navigationTemplate); main.set("side_navigation", subNavigationTemplate); + main.set("javascriptModules", jsModules); + main.set("javascriptPages", jsPages); main.set("content", httpRespond(session, cookie, request)); out.print(main.compile()); @@ -97,7 +104,12 @@ public abstract class HalWebPage implements HttpPage{ public static Navigation getUserNav(){ return userNav; } + public static void addJavascriptModule(HalJsModule module) { + jsModules.add(module); + if (module instanceof HalJsModulePage) + jsPages.add((HalJsModulePage) module); + } public abstract Templator httpRespond( Map session, diff --git a/hal-core/src/se/hal/page/JavascriptModules.java b/hal-core/src/se/hal/page/JavascriptModules.java new file mode 100644 index 00000000..6f1bd3a9 --- /dev/null +++ b/hal-core/src/se/hal/page/JavascriptModules.java @@ -0,0 +1,24 @@ +package se.hal.page; + +import se.hal.intf.HalJavascriptModule; + +public class JavascriptModules implements HalJavascriptModule { + + @Override + public HalJsModule[] getJavascriptModules() { + return new HalJsModule[] { + new HalJsModule("AlertStore", "./js/vue/stores/AlertStore.js"), + new HalJsModule("EventStore", "./js/vue/stores/EventStore.js"), + new HalJsModule("SensorStore", "./js/vue/stores/SensorStore.js"), + + new HalJsModule("AlertComponent", "./js/vue/components/AlertComponent.js"), + new HalJsModule("AlertListComponent", "./js/vue/components/AlertListComponent.js"), + + new HalJsModule("EventActionComponent", "./js/vue/components/EventActionComponent.js"), + new HalJsModulePage("EventDetailPageComponent", "./js/vue/components/EventDetailPageComponent.js", "/event/:id"), + new HalJsModulePage("EventOverviewPageComponent", "./js/vue/components/EventOverviewPageComponent.js", "/event_overview"), + new HalJsModule("EventTableComponent", "./js/vue/components/EventTableComponent.js"), + new HalJsModule("EventTableRowComponent", "./js/vue/components/EventTableRowComponent.js"), + }; + } +}