tab lists

This commit is contained in:
Robert Janetzko 2022-04-28 18:14:33 +00:00
parent 51d4ae08a7
commit 1c26cd2183
5 changed files with 41 additions and 5 deletions

View file

@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/gorilla/mux"
@ -227,7 +228,12 @@ func httpError(w http.ResponseWriter, err error) {
fmt.Println(err)
}
func grouped[T model.Typed](input map[int]T) map[string][]T {
type namedTyped interface {
model.Named
model.Typed
}
func grouped[T namedTyped](input map[int]T) map[string][]T {
output := make(map[string][]T)
for _, v := range input {
@ -235,5 +241,9 @@ func grouped[T model.Typed](input map[int]T) map[string][]T {
output[k] = append(output[k], v)
}
for _, v := range output {
sort.Slice(v, func(i, j int) bool { return v[i].Name() < v[j].Name() })
}
return output
}

View file

@ -40,7 +40,9 @@ func (srv *DfServer) LoadTemplates() {
"html": func(value any) template.HTML {
return template.HTML(fmt.Sprint(value))
},
"bytes": func(s int64) string { return humanize.Bytes(uint64(s)) },
"bytes": func(s int64) string { return humanize.Bytes(uint64(s)) },
"first": util.FirstInMap,
"ifFirst": func(m any, k string, r string) string { return util.If(util.FirstInMap(m, k), r, "") },
}
srv.templates = templates.New(functions)
}

View file

@ -8,16 +8,17 @@
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
{{- range $t, $v := .}}
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#nav-{{$t}}" type="button" role="tab">{{$t}}</button>
<button class="nav-link{{ ifFirst $ $t " active" }}" data-bs-toggle="tab" data-bs-target="#nav-{{$t}}" type="button"
role="tab">{{$t}}</button>
{{- end}}
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
{{- range $t, $v := . }}
<div class="tab-pane" id="nav-{{$t}}" role="tabpanel" aria-labelledby="nav-home-tab">
<div class="tab-pane{{ ifFirst $ $t " active" }}" id="nav-{{$t}}" role="tabpanel" aria-labelledby="nav-home-tab">
<table>
<tr>
<th>Name</th>
<th width="100%">Name</th>
<th>Race</th>
</tr>
{{- range $v }}{{- if not (eq .Name "") }}

View file

@ -62,6 +62,21 @@
</nav>
<div class="container">{{block "content" .}}{{end}}</div>
<script>
var hash = document.location.hash;
console.log("hah", hash)
if (hash && hash.startsWith("#nav-")) {
var hashPieces = hash.split('?');
activeTab = $('.nav-link[data-bs-target="' + hashPieces[0] + '"]');
console.log(hashPieces[0], activeTab);
activeTab && activeTab.click()
}
$('.nav-tabs button').on('click', function (e) {
window.location.hash = $(this).data("bs-target")
});
</script>
</body>
</html>

View file

@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"html/template"
"reflect"
"regexp"
"sort"
"strings"
)
@ -109,3 +111,9 @@ func FindInMap[U any](list map[int]*U, predicate func(*U) bool) (int, *U, bool)
}
return -1, nil, false
}
func FirstInMap(a any, b string) bool {
ks := reflect.ValueOf(a).MapKeys()
sort.Slice(ks, func(i, j int) bool { return ks[i].String() < ks[j].String() })
return ks[0].String() == b
}