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" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort"
"strings" "strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -227,7 +228,12 @@ func httpError(w http.ResponseWriter, err error) {
fmt.Println(err) 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) output := make(map[string][]T)
for _, v := range input { 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) 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 return output
} }

View File

@ -41,6 +41,8 @@ func (srv *DfServer) LoadTemplates() {
return template.HTML(fmt.Sprint(value)) 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) srv.templates = templates.New(functions)
} }

View File

@ -8,16 +8,17 @@
<nav> <nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist"> <div class="nav nav-tabs" id="nav-tab" role="tablist">
{{- range $t, $v := .}} {{- 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}} {{- end}}
</div> </div>
</nav> </nav>
<div class="tab-content" id="nav-tabContent"> <div class="tab-content" id="nav-tabContent">
{{- range $t, $v := . }} {{- 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> <table>
<tr> <tr>
<th>Name</th> <th width="100%">Name</th>
<th>Race</th> <th>Race</th>
</tr> </tr>
{{- range $v }}{{- if not (eq .Name "") }} {{- range $v }}{{- if not (eq .Name "") }}

View File

@ -62,6 +62,21 @@
</nav> </nav>
<div class="container">{{block "content" .}}{{end}}</div> <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> </body>
</html> </html>

View File

@ -4,7 +4,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"reflect"
"regexp" "regexp"
"sort"
"strings" "strings"
) )
@ -109,3 +111,9 @@ func FindInMap[U any](list map[int]*U, predicate func(*U) bool) (int, *U, bool)
} }
return -1, nil, false 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
}