From e51b3042758309095c4c660679bfd2e326646f70 Mon Sep 17 00:00:00 2001 From: Anders Pitman Date: Fri, 16 Oct 2020 09:40:46 -0600 Subject: [PATCH] Make tunnel delete buttons pure CSS --- database.go | 1 + ui_handler.go | 28 ++++++++++++++++++++++++++-- webui/index.tmpl | 36 +++++++++++++++++++++++++++++------- webui/styles.css | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/database.go b/database.go index 6d7f6eb..29adc95 100644 --- a/database.go +++ b/database.go @@ -33,6 +33,7 @@ type Tunnel struct { TunnelPrivateKey string `json:"tunnel_private_key"` ClientName string `json:"client_name"` ClientPort int `json:"client_port"` + CssId string `json:"css_id"` } func NewDatabase() (*Database, error) { diff --git a/ui_handler.go b/ui_handler.go index 72be815..960fb25 100644 --- a/ui_handler.go +++ b/ui_handler.go @@ -55,6 +55,10 @@ type HeadData struct { Styles template.CSS } +type StylesData struct { + Tunnels map[string]Tunnel +} + type MenuData struct { IsAdmin bool } @@ -82,6 +86,8 @@ func NewWebUiHandler(config *BoringProxyConfig, db *Database, api *Api, auth *Au func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request) { + homePath := "/#/tunnel" + token, err := extractToken("access_token", r) if err != nil { h.sendLoginPage(w, r, 401) @@ -112,6 +118,24 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request return } + stylesTmpl, err := template.New("styles").Parse(stylesText) + if err != nil { + w.WriteHeader(500) + h.alertDialog(w, r, err.Error(), homePath) + return + } + + tunnels := h.api.GetTunnels(tokenData) + + for domain, tun := range tunnels { + // TODO: might yield non-unique names + tun.CssId = strings.ReplaceAll(domain, ".", "-") + tunnels[domain] = tun + } + + var stylesBuilder strings.Builder + stylesTmpl.Execute(&stylesBuilder, StylesData{Tunnels: tunnels}) + headTmplStr, err := box.String("head.tmpl") if err != nil { w.WriteHeader(500) @@ -128,7 +152,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request } var headBuilder strings.Builder - headTmpl.Execute(&headBuilder, HeadData{Styles: template.CSS(stylesText)}) + headTmpl.Execute(&headBuilder, HeadData{Styles: template.CSS(stylesBuilder.String())}) h.headHtml = template.HTML(headBuilder.String()) @@ -190,7 +214,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request indexData := IndexData{ Head: h.headHtml, - Tunnels: h.api.GetTunnels(tokenData), + Tunnels: tunnels, Tokens: tokens, Users: users, IsAdmin: user.IsAdmin, diff --git a/webui/index.tmpl b/webui/index.tmpl index 6ab417d..1196d31 100644 --- a/webui/index.tmpl +++ b/webui/index.tmpl @@ -25,9 +25,10 @@
{{$domain}}:{{$tunnel.TunnelPort}} -> {{$tunnel.ClientName}}:{{$tunnel.ClientPort}}
- - - + + {{end}} @@ -35,11 +36,11 @@
- + - + - +
@@ -103,7 +104,7 @@
- + @@ -113,5 +114,26 @@
{{end}} + + {{range $domain, $tunnel:= .Tunnels}} + +
+ + +
+

+ Are you sure you want to delete {{$domain}}? +

+
+ + + + +
+
+
+ {{end}} + diff --git a/webui/styles.css b/webui/styles.css index 49ddda1..b38a6e5 100644 --- a/webui/styles.css +++ b/webui/styles.css @@ -74,6 +74,10 @@ main { text-decoration: none; user-select: none; cursor: pointer; + font-family: -system-ui, sans-serif; + font-size: 1em; + line-height: 1.2; + white-space: nowrap; } .button:hover { @@ -132,6 +136,37 @@ main *:target { background: #000; } +.confirm-dialog { + display: none; +} +.confirm-dialog__overlay { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background-color: rgba(0, 0, 0, .5); + z-index: 1000; +} +.confirm-dialog__content { + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + background-color: var(--background-color); + padding: 1em; + border: 1px solid #ccc; + z-index: 1010; +} + +{{range $domain, $tunnel:= .Tunnels}} +#toggle-tunnel-delete-{{$tunnel.CssId}} { + display: none; +} +#toggle-tunnel-delete-{{$tunnel.CssId}}:checked + .confirm-dialog { + display: block; +} +{{end}} @media (min-width: 900px) {