diff options
author | jan <jan@ruken.pw> | 2015-12-30 08:27:55 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2015-12-30 08:27:55 (UTC) |
commit | 9ab495abeec5e316adc2da4ac1afbd16d35826df (patch) | |
tree | 96f190a903b2365321db560821165fa1f78b47eb /frontend | |
parent | 10f014d5da7f1a35d2fc6cf0aa617a5ec4bb074f (diff) |
krasse paginationberechnung (patent pending)
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 | } | ||