Added enable/disable

This commit is contained in:
2025-06-05 10:36:03 +02:00
parent 05a8baed4f
commit 37ab462e27
5 changed files with 76 additions and 9 deletions
+30 -1
View File
@@ -2,13 +2,13 @@ const logger = require("../utils/logger.util.js");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const uuid = require("uuid"); const uuid = require("uuid");
const express = require('express');
const app_conf = require("../config/app.conf.js"); const app_conf = require("../config/app.conf.js");
const map_conf = require("../config/map.conf.js"); const map_conf = require("../config/map.conf.js");
const messages = require("../config/messages.conf.js"); const messages = require("../config/messages.conf.js");
const map = require("../utils/map.util.js"); const map = require("../utils/map.util.js");
const express = require('express');
const router = express.Router(); const router = express.Router();
function errorPageRenderer(res, emoji, message) { function errorPageRenderer(res, emoji, message) {
@@ -21,6 +21,28 @@ function errorPageRenderer(res, emoji, message) {
}) })
} }
router.get("/disable", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
fs.writeFileSync(map_conf.selected_map_path, "");
res.status(200).send();
});
router.get("/enable", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
var requested_map = req.query["element"];
if (requested_map && map.mapExists(requested_map)) {
fs.writeFileSync(map_conf.selected_map_path, requested_map);
res.status(200).send();
} else {
res.status(400).send();
}
});
router.delete("/remove", async (req, res) => { router.delete("/remove", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
@@ -151,6 +173,11 @@ router.get("/", async (req, res) => {
try { try {
const maps = await map.getAvailableMaps(); const maps = await map.getAvailableMaps();
var selected_map_name = "";
if (fs.existsSync(map_conf.selected_map_path)) {
selected_map_name = fs.readFileSync(map_conf.selected_map_path, "utf-8");
}
if (Object.keys(maps).length) { if (Object.keys(maps).length) {
var table_data = ""; var table_data = "";
@@ -182,6 +209,8 @@ router.get("/", async (req, res) => {
program: app_conf.program, program: app_conf.program,
css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), css_path: path.join(app_conf.paths.css.web, "stylesheet.css"),
js_path: path.join(app_conf.paths.js.web, "index.js"), js_path: path.join(app_conf.paths.js.web, "index.js"),
map_name_msg: (selected_map_name != "" ? "Using: " + selected_map_name : ""),
disable_display: (selected_map_name != "" ? "block" : "none"),
table_data: table_data table_data: table_data
}); });
} else { } else {
+3 -1
View File
@@ -27,7 +27,9 @@
<%= program %> <%= program %>
</label> </label>
<div style="display: flex;align-items: center;"> <div style="display: flex;align-items: center;">
<label id="download" style="cursor: pointer; margin-right: 10px;"><span>&#129095; <label id="enable" style="cursor: pointer; margin-right: 10px;"><span>&#9210;
</span>Enable</label>
<label id="download" style="cursor: pointer; margin-left: 10px; margin-right: 10px;"><span>&#129095;
</span>Download</label> </span>Download</label>
<label id="remove" style="cursor: pointer; margin-left: 10px; margin-right: 10px;"><span>&#45; <label id="remove" style="cursor: pointer; margin-left: 10px; margin-right: 10px;"><span>&#45;
</span>Remove</label> </span>Remove</label>
+4 -2
View File
@@ -24,7 +24,9 @@
<%= program %> <%= program %>
</label> </label>
<div style="display: flex;align-items: center;"> <div style="display: flex;align-items: center;">
<label id="add" style="cursor: pointer; margin-right: 10px;"><span>&#43; </span>Add</label> <label id="map_name" style="margin-right: 10px;"><%= map_name_msg %></label>
<label id="disable" style="cursor: pointer; margin-left: 10px; margin-right: 10px; display: <%= disable_display %>;"><span>&#9209; </span>Disable</label>
<label id="add" style="cursor: pointer; margin-left: 10px; margin-right: 10px;"><span>&#43; </span>Add</label>
<label id="upload" style="cursor: pointer; margin-left: 10px; margin-right: 10px;"><span>&#129093; <label id="upload" style="cursor: pointer; margin-left: 10px; margin-right: 10px;"><span>&#129093;
</span>Upload</label> </span>Upload</label>
<label id="logout" style="cursor: pointer; margin-left: 10px;"><span>&#129092; </span>Logout</label> <label id="logout" style="cursor: pointer; margin-left: 10px;"><span>&#129092; </span>Logout</label>
@@ -32,7 +34,7 @@
</div> </div>
<div class="main_div"> <div class="main_div">
<center><label>Main</label></center> <center><label>Main</label></center>
<table id="sql_table"> <table id="map_table">
<tr> <tr>
<th>Filename</th> <th>Filename</th>
<th>Name</th> <th>Name</th>
+18
View File
@@ -1,4 +1,5 @@
const commit = document.getElementById("commit"); const commit = document.getElementById("commit");
const enable = document.getElementById("enable");
const download = document.getElementById("download"); const download = document.getElementById("download");
const remove = document.getElementById("remove"); const remove = document.getElementById("remove");
const motor_table = document.getElementById("motor_table"); const motor_table = document.getElementById("motor_table");
@@ -27,6 +28,23 @@ function setEventListeners(element, cb) {
}; };
} }
setEventListeners(enable, async () => {
const map_filename = (new URLSearchParams(window.location.search)).get("element");
try {
const response = await fetch(`/app/enable?element=${map_filename}`, {
method: "GET"
});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
} else {
window.location.href = "/app";
}
} catch (error) {
alert(`Could not enable map. ${error.message}`);
}
});
setEventListeners(remove, async () => { setEventListeners(remove, async () => {
const map_filename = (new URLSearchParams(window.location.search)).get("element"); const map_filename = (new URLSearchParams(window.location.search)).get("element");
+21 -5
View File
@@ -1,4 +1,5 @@
const sql_table = document.getElementById("sql_table"); const map_table = document.getElementById("map_table");
const disable = document.getElementById("disable");
const upload = document.getElementById("upload"); const upload = document.getElementById("upload");
const logout = document.getElementById("logout"); const logout = document.getElementById("logout");
const add = document.getElementById("add"); const add = document.getElementById("add");
@@ -19,6 +20,21 @@ function setEventListeners(element, cb) {
}; };
} }
setEventListeners(disable, async () => {
try {
const response = await fetch(`/app/disable`, {
method: "GET"
});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
} else {
window.location.href = "/app";
}
} catch (error) {
alert(`Could not disable map. ${error.message}`);
}
});
setEventListeners(upload, async () => { setEventListeners(upload, async () => {
const input = document.getElementById("file_input"); const input = document.getElementById("file_input");
const upload = (file) => { const upload = (file) => {
@@ -49,10 +65,10 @@ setEventListeners(logout, () => {
window.location.href = `${window.location.protocol}//logout@${window.location.host}/`; window.location.href = `${window.location.protocol}//logout@${window.location.host}/`;
}); });
const sql_rows = sql_table.rows; const map_rows = map_table.rows;
for (let i = 1; i < sql_rows.length; i++) { for (let i = 1; i < map_rows.length; i++) {
setEventListeners(sql_rows[i], () => { setEventListeners(map_rows[i], () => {
window.location.href = `/app/edit?element=${sql_rows[i].cells[0].innerText}`; window.location.href = `/app/edit?element=${map_rows[i].cells[0].innerText}`;
}); });
} }