From d9e7fb1ab142751bfec4f0a91cd0fa99084a6596 Mon Sep 17 00:00:00 2001 From: Jocadbz Date: Tue, 9 Jun 2026 18:52:32 -0300 Subject: [PATCH] Device and wall-socket views now highlight connected ports Extracted shared _port_list.html partial with connected-port checking, loaded into every page's template set. DeviceView and WallSocket handlers now load connection data and pass it to the template. Removed duplicate inline port_list definition from rack.html. --- internal/handlers/devices.go | 23 ++++++++++++++++++++- internal/handlers/handlers.go | 5 ++++- internal/handlers/wall_sockets.go | 26 +++++++++++++++++++----- templates/_port_list.html | 32 ++++++++++++++++++++++++++++++ templates/device.html | 29 +-------------------------- templates/rack.html | 33 ------------------------------- templates/wall_sockets.html | 2 +- 7 files changed, 81 insertions(+), 69 deletions(-) create mode 100644 templates/_port_list.html diff --git a/internal/handlers/devices.go b/internal/handlers/devices.go index aa3d784..407b413 100644 --- a/internal/handlers/devices.go +++ b/internal/handlers/devices.go @@ -9,8 +9,8 @@ import ( type DeviceViewData struct { Device models.Device + Connections []models.Connection ConnectionTypes []models.ConnectionType - AllPorts []models.DevicePort Error string } @@ -28,6 +28,7 @@ func (h *Handlers) DeviceView(w http.ResponseWriter, r *http.Request) { return } + conns := h.connectionsForDevice(device) connTypes, _ := h.Store.ConnectionTypeGetAll() if connTypes == nil { connTypes = []models.ConnectionType{} @@ -35,10 +36,29 @@ func (h *Handlers) DeviceView(w http.ResponseWriter, r *http.Request) { h.render(w, "device.html", DeviceViewData{ Device: *device, + Connections: conns, ConnectionTypes: connTypes, }) } +func (h *Handlers) connectionsForDevice(device *models.Device) []models.Connection { + if device == nil { + return []models.Connection{} + } + var allConns []models.Connection + for _, p := range device.Ports { + conn, err := h.Store.ConnectionGetByPortID(p.ID) + if err != nil || conn == nil { + continue + } + allConns = append(allConns, *conn) + } + if allConns == nil { + return []models.Connection{} + } + return allConns +} + func (h *Handlers) DeviceCreate(w http.ResponseWriter, r *http.Request) { r.ParseForm() @@ -135,6 +155,7 @@ func (h *Handlers) renderDeviceError(w http.ResponseWriter, device models.Device } h.render(w, "device.html", DeviceViewData{ Device: device, + Connections: h.connectionsForDevice(&device), ConnectionTypes: connTypes, Error: errMsg, }) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 1cbead3..0a4ebb5 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -52,7 +52,10 @@ func New(store *db.Store) *Handlers { baseFiles := []string{filepath.Join("templates", "base.html")} for _, page := range pages { - files := append(baseFiles, filepath.Join("templates", page)) + files := append(baseFiles, + filepath.Join("templates", page), + filepath.Join("templates", "_port_list.html"), + ) t, err := template.New("base.html").Funcs(funcMap).ParseFiles(files...) if err != nil { panic(fmt.Errorf("parse %s: %w", page, err)) diff --git a/internal/handlers/wall_sockets.go b/internal/handlers/wall_sockets.go index f6a396c..3867900 100644 --- a/internal/handlers/wall_sockets.go +++ b/internal/handlers/wall_sockets.go @@ -8,9 +8,10 @@ import ( ) type WallSocketData struct { - Sockets []models.Device - Models []models.DeviceModel - Error string + Sockets []models.Device + Connections []models.Connection + Models []models.DeviceModel + Error string } func (h *Handlers) WallSockets(w http.ResponseWriter, r *http.Request) { @@ -30,9 +31,24 @@ func (h *Handlers) WallSockets(w http.ResponseWriter, r *http.Request) { wallModels = []models.DeviceModel{} } + var conns []models.Connection + for _, s := range sockets { + for _, p := range s.Ports { + conn, err := h.Store.ConnectionGetByPortID(p.ID) + if err != nil || conn == nil { + continue + } + conns = append(conns, *conn) + } + } + if conns == nil { + conns = []models.Connection{} + } + h.render(w, "wall_sockets.html", WallSocketData{ - Sockets: sockets, - Models: wallModels, + Sockets: sockets, + Connections: conns, + Models: wallModels, }) } diff --git a/templates/_port_list.html b/templates/_port_list.html new file mode 100644 index 0000000..e2f2f94 --- /dev/null +++ b/templates/_port_list.html @@ -0,0 +1,32 @@ +{{define "port_list"}} +
+
+
Front
+ {{range .Device.Ports}}{{if eq .Side "front"}} + {{$pid := .ID}} + + {{.Name}} + + {{end}}{{end}} +
+
+
Back
+ {{range .Device.Ports}}{{if eq .Side "back"}} + {{$pid := .ID}} + + {{.Name}} + + {{end}}{{end}} +
+
+{{end}} diff --git a/templates/device.html b/templates/device.html index 37f1cef..89f3232 100644 --- a/templates/device.html +++ b/templates/device.html @@ -35,32 +35,5 @@

Ports

-
-
-

Front

- {{range .Device.Ports}}{{if eq .Side "front"}} - - {{.Name}} - - {{end}}{{end}} -
-
-

Back

- {{range .Device.Ports}}{{if eq .Side "back"}} - - {{.Name}} - - {{end}}{{end}} -
-
+{{template "port_list" .}} {{end}} diff --git a/templates/rack.html b/templates/rack.html index 9b9ba51..84bc03d 100644 --- a/templates/rack.html +++ b/templates/rack.html @@ -113,36 +113,3 @@ {{end}} - -{{define "port_list"}} -
-
-
Front
- {{range .Device.Ports}}{{if eq .Side "front"}} - {{$pid := .ID}} - - {{.Name}} - - {{end}}{{end}} -
-
-
Back
- {{range .Device.Ports}}{{if eq .Side "back"}} - {{$pid := .ID}} - - {{.Name}} - - {{end}}{{end}} -
-
-{{end}} diff --git a/templates/wall_sockets.html b/templates/wall_sockets.html index 2229041..df50b23 100644 --- a/templates/wall_sockets.html +++ b/templates/wall_sockets.html @@ -13,7 +13,7 @@ {{.Comment}} {{range .Ports}} -