From 0b0bf23a3f63d334d56dd889b1d0d5b1ada6cdbc Mon Sep 17 00:00:00 2001 From: Yader Velasquez Date: Wed, 14 Feb 2024 11:26:15 -0600 Subject: [PATCH] Use url.Value object to store advanced search queries. Use template func to print and add query params in templates. References: https://todo.code.netlandish.com/~netlandish/links/47 --- cmd/links/main.go | 1 + core/routes.go | 9 +++++++-- helpers.go | 11 +++++++++++ static/js/advancedsearch.js | 4 ++-- templates/link_list.html | 4 +--- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cmd/links/main.go b/cmd/links/main.go index d425a81..6c926ee 100644 --- a/cmd/links/main.go +++ b/cmd/links/main.go @@ -238,6 +238,7 @@ func run() error { "isTagUsedInFilter": func(tag string, activeTags string) bool { return strings.Contains(activeTags, tag) }, + "addQueryElement": links.AddQueryElement, }) err = srv.LoadTemplatesFS(links.TemplateFS, "templates/*.html", "templates/*.txt") if err != nil { diff --git a/core/routes.go b/core/routes.go index 6746385..72dec00 100644 --- a/core/routes.go +++ b/core/routes.go @@ -1310,19 +1310,23 @@ func (s *Service) OrgLinksList(c echo.Context) error { excludeTag, search string ) + queries := make(url.Values) if c.QueryParam("tag") != "" { tag = c.QueryParam("tag") op.Var("tag", tag) + queries.Add("tag", tag) } if c.QueryParam("exclude") != "" { excludeTag = c.QueryParam("exclude") op.Var("excludeTag", excludeTag) + queries.Add("exclude", excludeTag) } if c.QueryParam("q") != "" { search = c.QueryParam("q") op.Var("search", search) + queries.Add("q", search) } err := links.Execute(links.LangContext(c), op, &result) @@ -1375,14 +1379,15 @@ func (s *Service) OrgLinksList(c echo.Context) error { "org": org, "isOrgLink": isOrgLink, "canRead": canRead, - "tagFilter": tag, - "excludeTagFilter": excludeTag, + "tagFilter": strings.Replace(tag, ",", ", ", -1), + "excludeTagFilter": strings.Replace(excludeTag, ",", ", ", -1), "navFlag": navFlag, "advancedSearch": advancedSearch, "hasUnreadFilter": hasUnreadFilter, "hasStarredFilter": hasStarredFilter, "hasAllFilter": hasAllFilter, "currURL": currURL, + "queries": queries, } if search != "" { diff --git a/helpers.go b/helpers.go index 1e880b6..5977c6b 100644 --- a/helpers.go +++ b/helpers.go @@ -791,3 +791,14 @@ func (t TagQuery) GetSubQuery(inputTag, inputExcludeTag *string) (string, []inte return subQ.ToSql() } + +func AddQueryElement(q url.Values, param, val string) template.URL { + curVal := q.Get(param) + if curVal == "" { + curVal = val + } else { + curVal += fmt.Sprintf(",%s", val) + } + q.Set(param, curVal) + return template.URL(q.Encode()) +} diff --git a/static/js/advancedsearch.js b/static/js/advancedsearch.js index cc3100c..28ab0e8 100644 --- a/static/js/advancedsearch.js +++ b/static/js/advancedsearch.js @@ -79,7 +79,7 @@ form.addEventListener("submit", function(e) { form.elements.tag.disabled = true; } else { - tagValue = tagValue.split(",").map(slugify).join(", "); + tagValue = tagValue.split(",").map(slugify).join(","); if (tagValue.endsWith(",")) { tagValue = tagValue.slice(0, -1); } @@ -90,7 +90,7 @@ form.addEventListener("submit", function(e) { form.elements.exclude.disabled = true; } else { - excludeValue = excludeValue.split(",").map(slugify).join(", "); + excludeValue = excludeValue.split(",").map(slugify).join(","); if (excludeValue.endsWith(",")) { excludeValue = excludeValue.slice(0, -1); } diff --git a/templates/link_list.html b/templates/link_list.html index 7f85483..1c7240d 100644 --- a/templates/link_list.html +++ b/templates/link_list.html @@ -151,9 +151,7 @@ {{if isTagUsedInFilter .Slug $.tagFilter}} #{{.Name}} {{else}} - - #{{.Name}} + #{{.Name}} {{end}} {{end}} -- 2.45.2