mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 09:15:33 +00:00
BUGFIX: Paginierung Bände
This commit is contained in:
@@ -7,6 +7,30 @@ import (
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_PAGESIZE_BAENDE = 40
|
||||
)
|
||||
|
||||
type BaendeFilterParameters struct {
|
||||
Agent string
|
||||
Year string
|
||||
Place string
|
||||
State string
|
||||
}
|
||||
|
||||
func NewBaendeFilterParameters(ev *core.RequestEvent) BaendeFilterParameters {
|
||||
agent := ev.Request.URL.Query().Get("agent")
|
||||
year := ev.Request.URL.Query().Get("year")
|
||||
place := ev.Request.URL.Query().Get("place")
|
||||
state := ev.Request.URL.Query().Get("state")
|
||||
return BaendeFilterParameters{
|
||||
Agent: agent,
|
||||
Year: year,
|
||||
Place: place,
|
||||
State: state,
|
||||
}
|
||||
}
|
||||
|
||||
type SearchResultBaende struct {
|
||||
Queries []dbmodels.FTS5QueryRequest
|
||||
|
||||
@@ -21,6 +45,8 @@ type SearchResultBaende struct {
|
||||
EntriesSeries map[string][]*dbmodels.REntriesSeries // <- Key: Entry ID
|
||||
SeriesEntries map[string][]*dbmodels.REntriesSeries // <- Key: Series ID
|
||||
EntriesAgents map[string][]*dbmodels.REntriesAgents // <- Key: Entry ID
|
||||
|
||||
Pages []int
|
||||
}
|
||||
|
||||
func EmptyResultBaende() *SearchResultBaende {
|
||||
@@ -133,16 +159,29 @@ func NewSearchBaende(app core.App, params SearchParameters) (*SearchResultBaende
|
||||
}
|
||||
|
||||
hits := []string{}
|
||||
pages := []int{}
|
||||
if params.Sort == "series" {
|
||||
dbmodels.Sort_Series_Title(series)
|
||||
for _, s := range series {
|
||||
hits = append(hits, s.Id)
|
||||
}
|
||||
pages = PagesMap(hits, invrelationsmap, DEFAULT_PAGESIZE_BAENDE)
|
||||
} else {
|
||||
dbmodels.Sort_Entries_Year_Title(entries)
|
||||
for _, e := range entries {
|
||||
hits = append(hits, e.Id)
|
||||
}
|
||||
pages = PagesArray(hits, DEFAULT_PAGESIZE_BAENDE)
|
||||
}
|
||||
|
||||
if params.Page < 1 || params.Page > len(pages) {
|
||||
params.Page = 1
|
||||
}
|
||||
|
||||
if params.Page == len(pages) {
|
||||
hits = hits[pages[params.Page-1]:]
|
||||
} else {
|
||||
hits = hits[pages[params.Page-1]:pages[params.Page]]
|
||||
}
|
||||
|
||||
return &SearchResultBaende{
|
||||
@@ -154,10 +193,15 @@ func NewSearchBaende(app core.App, params SearchParameters) (*SearchResultBaende
|
||||
EntriesSeries: relationsmap,
|
||||
SeriesEntries: invrelationsmap,
|
||||
EntriesAgents: relationsagentsmap,
|
||||
Pages: pages,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (r SearchResultBaende) PagesCount() int {
|
||||
return len(r.Pages) - 1
|
||||
}
|
||||
|
||||
func (r SearchResultBaende) Count() int {
|
||||
return len(r.Entries)
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ func NewSearchBeitraege(app core.App, params SearchParameters, filters Beitraege
|
||||
hits = append(hits, e.Id)
|
||||
}
|
||||
|
||||
pages := PagesEntries(hits, contentsmap, DEFAULT_PAGESIZE)
|
||||
pages := PagesMap(hits, contentsmap, DEFAULT_PAGESIZE)
|
||||
if params.Page < 1 || params.Page > len(pages) {
|
||||
params.Page = 1
|
||||
}
|
||||
@@ -304,7 +304,7 @@ func (p *SearchResultBeitraege) PagesCount() int {
|
||||
return len(p.Pages) - 1
|
||||
}
|
||||
|
||||
func PagesEntries[T any](hits []string, hitmap map[string][]*T, pagesize int) []int {
|
||||
func PagesMap[T any](hits []string, hitmap map[string][]*T, pagesize int) []int {
|
||||
ret := []int{0}
|
||||
m := 0
|
||||
for i, hit := range hits {
|
||||
@@ -321,3 +321,21 @@ func PagesEntries[T any](hits []string, hitmap map[string][]*T, pagesize int) []
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func PagesArray[T any](hits []T, pagesize int) []int {
|
||||
ret := []int{0}
|
||||
m := 0
|
||||
for i := range hits {
|
||||
m++
|
||||
if m >= pagesize {
|
||||
ret = append(ret, i)
|
||||
m = 0
|
||||
}
|
||||
}
|
||||
|
||||
if m > 0 {
|
||||
ret = append(ret, len(hits))
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -172,6 +172,10 @@ func (p SearchParameters) includedParams(q string) []string {
|
||||
return res
|
||||
}
|
||||
|
||||
func (p SearchParameters) SortToQueryParams() string {
|
||||
return fmt.Sprintf("&sort=%s", p.Sort)
|
||||
}
|
||||
|
||||
func (p SearchParameters) ToQueryParamsBeitraege() string {
|
||||
q := "?"
|
||||
|
||||
|
||||
@@ -212,29 +212,36 @@
|
||||
{{- if $model.parameters.IsBaendeSearch -}}
|
||||
<div class="container-normal" id="searchresults">
|
||||
<div class="border-b border-zinc-300 flex flex-row justify-between">
|
||||
<div>
|
||||
<div class="flex flex-row">
|
||||
<div class="inline-block">
|
||||
<i class="ri-hourglass-2-fill request-indicator spinning"></i>
|
||||
</div>
|
||||
<div class="request-indicator">·</div>
|
||||
{{ if $model.parameters.Query -}}
|
||||
Suche nach <b>»{{ $model.parameters.Query }}«</b> ·
|
||||
{{- end -}}
|
||||
{{- if $isAlm -}}
|
||||
Almanach-Nummer <b>»{{ $model.parameters.AlmString }}«</b> ·
|
||||
{{- end -}}
|
||||
<i class="ri-book-line"></i>
|
||||
{{ if eq $model.result.Count 1 -}}
|
||||
Ein Band
|
||||
{{ else -}}
|
||||
{{ $model.result.Count }} Bände
|
||||
{{- end }}
|
||||
in
|
||||
{{ if eq ($model.result.Series | len) 1 -}}
|
||||
einer Reihe
|
||||
{{ else -}}
|
||||
{{ $model.result.Series | len }} Reihen
|
||||
<div>
|
||||
{{ if $model.parameters.Query -}}
|
||||
Suche nach <b>»{{ $model.parameters.Query }}«</b> ·
|
||||
{{- end -}}
|
||||
{{- if $isAlm -}}
|
||||
Almanach-Nummer <b>»{{ $model.parameters.AlmString }}«</b> ·
|
||||
{{- end -}}
|
||||
<i class="ri-book-line"></i>
|
||||
{{ if eq $model.result.Count 1 -}}
|
||||
Ein Band
|
||||
{{ else -}}
|
||||
{{ $model.result.Count }} Bände
|
||||
{{- end }}
|
||||
in
|
||||
{{ if eq ($model.result.Series | len) 1 -}}
|
||||
einer Reihe
|
||||
{{ else -}}
|
||||
{{ $model.result.Series | len }} Reihen
|
||||
{{- end -}}
|
||||
</div>
|
||||
|
||||
{{- if gt (len $model.result.Pages) 1 }}
|
||||
<div> · </div>
|
||||
{{- end -}}
|
||||
{{ template "_pagination" Arr $model "baende" }}
|
||||
</div>
|
||||
|
||||
{{- if not $isAlm -}}
|
||||
@@ -295,6 +302,13 @@
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if $model.result.Hits -}}
|
||||
<div class="flex flex-row justify-end items-start border-t border-zinc-300 pt-4">
|
||||
{{- template "_pagination" Arr $model "baende" -}}
|
||||
</div>
|
||||
{{- else -}}
|
||||
<div class="container-normal">Keine Bände gefunden.</div>
|
||||
{{- end -}}
|
||||
|
||||
<script type="module">
|
||||
let elements = document.querySelectorAll('.search-text');
|
||||
@@ -307,8 +321,4 @@
|
||||
}, 200);
|
||||
</script>
|
||||
</div>
|
||||
|
||||
{{- if not $model.result.Hits -}}
|
||||
<div class="container-normal">Keine Bände gefunden.</div>
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
@@ -229,7 +229,7 @@
|
||||
|
||||
{{- if $model.result.Hits -}}
|
||||
<div class="flex flex-row justify-end items-start border-t border-zinc-300 pt-4">
|
||||
{{- template "pagination" $model -}}
|
||||
{{- template "_pagination" Arr $model "beitraege" -}}
|
||||
</div>
|
||||
{{- else -}}
|
||||
<div class="mt-4">Kein Beitrag gefunden.</div>
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
{{ $model := . }}
|
||||
{{- if gt (len $model.result.Pages) 1 }}
|
||||
<div class="[&>_a]:no-underline">
|
||||
{{ if gt $model.parameters.Page 1 -}}
|
||||
<a
|
||||
href="{{- $model.parameters.ToQueryParamsBeitraege -}}&page={{ $model.parameters.Prev }}"
|
||||
hx-indicator="body"
|
||||
class="mr-1.5 text-stone-500 hover:text-slate-900">
|
||||
<i class="ri-arrow-left-long-line"></i>
|
||||
</a>
|
||||
{{- end -}}
|
||||
Seite
|
||||
<b>{{ $model.parameters.Page }} / {{ $model.result.PagesCount }}</b>
|
||||
{{ if lt $model.parameters.Page ($model.result.PagesCount) -}}
|
||||
<a
|
||||
href="{{- $model.parameters.ToQueryParamsBeitraege -}}&page={{ $model.parameters.Next }}"
|
||||
hx-indicator="body"
|
||||
class="ml-1.5 text-stone-500 hover:text-slate-900">
|
||||
<i class="ri-arrow-right-long-line"></i>
|
||||
</a>
|
||||
{{- end -}}
|
||||
</div>
|
||||
{{- end -}}
|
||||
@@ -49,7 +49,7 @@
|
||||
{{- if gt (len $model.result.Pages) 1 }}
|
||||
<div>·</div>
|
||||
{{- end -}}
|
||||
{{ template "pagination" $model }}
|
||||
{{ template "_pagination" Arr $model "beitraege" }}
|
||||
</div>
|
||||
|
||||
{{- if not $isAlm -}}
|
||||
|
||||
37
views/routes/suche/components/_pagination.gohtml
Normal file
37
views/routes/suche/components/_pagination.gohtml
Normal file
@@ -0,0 +1,37 @@
|
||||
{{ $model := index . 0 }}
|
||||
{{ $type := index . 1 }}
|
||||
|
||||
{{ $queryParams := "./" }}
|
||||
{{ if eq $type "baende" }}
|
||||
{{ $queryParams = $model.parameters.ToQueryParamsBaende }}
|
||||
{{ else }}
|
||||
{{ $queryParams = $model.parameters.ToQueryParamsBeitraege }}
|
||||
{{ end }}
|
||||
|
||||
{{ $filterParams := "" }}
|
||||
{{ if $model.filters }}
|
||||
{{ $filterParams = $model.filters.ToQueryParams }}
|
||||
{{ end }}
|
||||
|
||||
{{- if gt (len $model.result.Pages) 1 }}
|
||||
<div class="[&>_a]:no-underline">
|
||||
{{ if gt $model.parameters.Page 1 -}}
|
||||
<a
|
||||
href="{{- $queryParams -}}{{- $filterParams -}}{{- $model.parameters.SortToQueryParams -}}&page={{ $model.parameters.Prev }}"
|
||||
hx-indicator="body"
|
||||
class="mr-1.5 text-stone-500 hover:text-slate-900">
|
||||
<i class="ri-arrow-left-long-line"></i>
|
||||
</a>
|
||||
{{- end -}}
|
||||
Seite
|
||||
<b>{{ $model.parameters.Page }} / {{ $model.result.PagesCount }}</b>
|
||||
{{ if lt $model.parameters.Page ($model.result.PagesCount) -}}
|
||||
<a
|
||||
href="{{- $queryParams -}}{{- $filterParams -}}{{- $model.parameters.SortToQueryParams -}}&page={{ $model.parameters.Next }}"
|
||||
hx-indicator="body"
|
||||
class="ml-1.5 text-stone-500 hover:text-slate-900">
|
||||
<i class="ri-arrow-right-long-line"></i>
|
||||
</a>
|
||||
{{- end -}}
|
||||
</div>
|
||||
{{- end -}}
|
||||
Reference in New Issue
Block a user