diff options
Diffstat (limited to 'frontend')
-rw-r--r-- | frontend/utility.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/frontend/utility.go b/frontend/utility.go new file mode 100644 index 0000000..28e7db1 --- /dev/null +++ b/frontend/utility.go | |||
@@ -0,0 +1,49 @@ | |||
1 | package frontend | ||
2 | |||
3 | type PaginationRange struct { | ||
4 | Before []int | ||
5 | Current int | ||
6 | MaxPage int | ||
7 | After []int | ||
8 | } | ||
9 | |||
10 | // PaginationFromPage gibt die Seiten vor und nach der gegebenen Seite zurück. pageRange gibt an, wie viele Seiten vor und nach der aktuellen Page angezeigt werden soll. | ||
11 | func PaginationFromPage(pageRange, currentPage, maxPage int) PaginationRange { | ||
12 | var before []int | ||
13 | var after []int | ||
14 | |||
15 | startPage := currentPage - pageRange | ||
16 | endPage := currentPage + pageRange + 1 | ||
17 | |||
18 | if startPage < 1 { | ||
19 | endPage += -startPage + 1 | ||
20 | startPage = 1 | ||
21 | } | ||
22 | |||
23 | if endPage > maxPage { | ||
24 | startPage -= (endPage - maxPage) | ||
25 | endPage = maxPage | ||
26 | |||
27 | if startPage < 1 { | ||
28 | startPage = 1 | ||
29 | } | ||
30 | } | ||
31 | |||
32 | for i := startPage; i < currentPage; i++ { | ||
33 | if i > maxPage { | ||
34 | break | ||
35 | } | ||
36 | before = append(before, i) | ||
37 | } | ||
38 | |||
39 | for i := currentPage + 1; i < endPage; i++ { | ||
40 | after = append(after, i) | ||
41 | } | ||
42 | |||
43 | return PaginationRange{ | ||
44 | before, | ||
45 | currentPage, | ||
46 | maxPage, | ||
47 | after, | ||
48 | } | ||
49 | } | ||