From b0a326c1e22fdd7c874e654c6dc4a556343f8b6a Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Fri, 23 Aug 2024 01:28:48 +0700 Subject: [PATCH] Edit, Filter --- internal/web/api.go | 31 +++++++++++++++++- internal/web/helpers.go | 17 ++++++++++ internal/web/index.go | 2 ++ internal/web/public/css/index.css | 4 +++ internal/web/public/js/filter.js | 27 ++++++++++++++++ internal/web/public/js/index.js | 53 +++++++++++++++++++++---------- internal/web/templates/index.html | 25 +++++++++++++++ internal/web/webgui.go | 5 +-- 8 files changed, 144 insertions(+), 20 deletions(-) create mode 100644 internal/web/helpers.go create mode 100644 internal/web/public/js/filter.js diff --git a/internal/web/api.go b/internal/web/api.go index 4f5eb9b..623ea7e 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -1,14 +1,19 @@ package web import ( + // "log" "net/http" + "strconv" "github.com/gin-gonic/gin" - // "github.com/aceberg/WatchYourLAN/internal/models" + + "github.com/aceberg/WatchYourLAN/internal/db" ) func apiAll(c *gin.Context) { + allHosts = db.Select(appConfig.DBPath, "now") + c.IndentedJSON(http.StatusOK, allHosts) } @@ -16,3 +21,27 @@ func apiHistory(c *gin.Context) { c.IndentedJSON(http.StatusOK, histHosts) } + +func apiEdit(c *gin.Context) { + + idStr := c.Param("id") + name := c.Param("name") + toggleKnown := c.Param("known") + + id, _ := strconv.Atoi(idStr) + + host := getHostByID(id, allHosts) + if host.Date != "" { + host.Name = name + + if toggleKnown == "/toggle" { + host.Known = 1 - host.Known + } + // log.Println("EDIT: ", host) + + db.Update(appConfig.DBPath, "now", host) + allHosts = db.Select(appConfig.DBPath, "now") + } + + c.IndentedJSON(http.StatusOK, "OK") +} diff --git a/internal/web/helpers.go b/internal/web/helpers.go new file mode 100644 index 0000000..20f3dac --- /dev/null +++ b/internal/web/helpers.go @@ -0,0 +1,17 @@ +package web + +import ( + "github.com/aceberg/WatchYourLAN/internal/models" +) + +func getHostByID(id int, hosts []models.Host) (oneHost models.Host) { + + for _, host := range hosts { + if host.ID == id { + oneHost = host + break + } + } + + return oneHost +} diff --git a/internal/web/index.go b/internal/web/index.go index 8fa3532..020e0dc 100644 --- a/internal/web/index.go +++ b/internal/web/index.go @@ -2,6 +2,7 @@ package web import ( "net/http" + "strings" "github.com/gin-gonic/gin" @@ -11,6 +12,7 @@ import ( func indexHandler(c *gin.Context) { var guiData models.GuiData guiData.Config = appConfig + guiData.Themes = strings.Split(appConfig.Ifaces, " ") c.HTML(http.StatusOK, "header.html", guiData) c.HTML(http.StatusOK, "index.html", guiData) diff --git a/internal/web/public/css/index.css b/internal/web/public/css/index.css index 979ee5e..25657a1 100644 --- a/internal/web/public/css/index.css +++ b/internal/web/public/css/index.css @@ -6,4 +6,8 @@ } .my-btn:hover { background-color: #0000001a; +} + +.hidden { + display: none; } \ No newline at end of file diff --git a/internal/web/public/js/filter.js b/internal/web/public/js/filter.js new file mode 100644 index 0000000..6760111 --- /dev/null +++ b/internal/web/public/js/filter.js @@ -0,0 +1,27 @@ +let oldFilter = ''; +let bkpArray; + +function filterFunc(field, value) { + + if (oldFilter == field) { + addrsArray = bkpArray; + } + + switch (field) { + case 'iface': + addrsArray = addrsArray.filter((item) => item.Iface == value); + break; + case 'known': + addrsArray = addrsArray.filter((item) => item.Known == value); + break; + case 'line': + addrsArray = addrsArray.filter((item) => item.Now == value); + break; + default: + console.log("Filter error"); + } + + oldFilter = field; + + displayArrayData(addrsArray); +} \ No newline at end of file diff --git a/internal/web/public/js/index.js b/internal/web/public/js/index.js index a2901bc..196a741 100644 --- a/internal/web/public/js/index.js +++ b/internal/web/public/js/index.js @@ -1,28 +1,29 @@ -var addrsArray = {}; +let addrsArray = {}; +let edit = "0"; loadAddrs(); function createHTML(addr, i) { - let now = ''; - - if (addr.Now == 0) { - now = ``; - } else { + let now = ``; + if (addr.Now == 1) { now = ``; } let known = ''; - if (addr.Known == 1) { - known = ``; - } else { - known = ``; + known = `checked`; + } + + // Needs option to use value in js + let name = ``; + if (edit == 1) { + name = ``; } let html = `