aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2015-12-30 08:27:55 (UTC)
committerjan <jan@ruken.pw>2015-12-30 08:27:55 (UTC)
commit9ab495abeec5e316adc2da4ac1afbd16d35826df (patch)
tree96f190a903b2365321db560821165fa1f78b47eb
parent10f014d5da7f1a35d2fc6cf0aa617a5ec4bb074f (diff)
krasse paginationberechnung (patent pending)
-rw-r--r--frontend/utility.go49
-rw-r--r--main.go4
-rw-r--r--modules/tags/tags.go3
-rw-r--r--views/tag.html13
4 files changed, 64 insertions, 5 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 @@
1package frontend
2
3type 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.
11func 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}
diff --git a/main.go b/main.go
index 04db8d6..850f0f9 100644
--- a/main.go
+++ b/main.go
@@ -44,6 +44,10 @@ func main() {
44 44
45 log.Println("database connection established") 45 log.Println("database connection established")
46 46
47 for i := 0; i < 10; i++ {
48 log.Println(frontend.PaginationFromPage(10, i, 3))
49 }
50
47 renderer := frontend.New("views") 51 renderer := frontend.New("views")
48 login := charakterin.New(db) 52 login := charakterin.New(db)
49 login.UseRenderer(renderer) 53 login.UseRenderer(renderer)
diff --git a/modules/tags/tags.go b/modules/tags/tags.go
index 527d1d9..650fa4e 100644
--- a/modules/tags/tags.go
+++ b/modules/tags/tags.go
@@ -122,7 +122,6 @@ func (m *Module) viewTagPage(w http.ResponseWriter, r *http.Request, p httproute
122 data["user"] = user 122 data["user"] = user
123 data["tag"] = tag 123 data["tag"] = tag
124 data["cards"] = cards 124 data["cards"] = cards
125 data["currentPage"] = pageNumber 125 data["pagination"] = frontend.PaginationFromPage(3, pageNumber, pageCount)
126 data["pageCount"] = pageCount
127 m.g.Renderer.RenderPage("tag", w, data) 126 m.g.Renderer.RenderPage("tag", w, data)
128} 127}
diff --git a/views/tag.html b/views/tag.html
index 3e89a7b..bc21fc3 100644
--- a/views/tag.html
+++ b/views/tag.html
@@ -1,4 +1,5 @@
1{{ define "tag" }} 1{{ define "tag" }}
2{{ $tagBaseURI := printf "/tag/%s/" .tag }}
2<html> 3<html>
3 <head> 4 <head>
4 {{ template "materialize" }} 5 {{ template "materialize" }}
@@ -17,9 +18,15 @@
17 {{ end }} 18 {{ end }}
18 </div> 19 </div>
19 <ul class="pagination center-align"> 20 <ul class="pagination center-align">
20 <li class="{{ if eq .currentPage 1 }}disabled{{ else }}waves-effect{{ end }}"><a href="/tag/{{ .tag }}/{{ sub .currentPage 1 }}"><i class="material-icons">chevron_left</i></a></li> 21 <li class="{{ if eq .pagination.Current 1 }}disabled{{ else }}waves-effect{{ end }}"><a href="{{ $tagBaseURI }}{{ sub .pagination.Current 1 }}"><i class="material-icons">chevron_left</i></a></li>
21 <li class="active"><a href="#!">{{ .currentPage }}</a></li> 22 {{ range .pagination.Before }}
22 <li class="{{ if eq .currentPage .pageCount }}disabled{{ else }}waves-effect{{ end }}"><a href="/tag/{{ .tag }}/{{ add .currentPage 1 }}"><i class="material-icons">chevron_right</i></a></li> 23 <li class="wave-effect"><a href="{{ $tagBaseURI }}{{ . }}">{{ . }}</a></li>
24 {{ end }}
25 <li class="active"><a href="#!">{{ .pagination.Current }}</a></li>
26 {{ range .pagination.After }}
27 <li class="wave-effect"><a href="{{ $tagBaseURI }}{{ . }}">{{ . }}</a></li>
28 {{ end }}
29 <li class="{{ if eq .pagination.Current .pagination.MaxPage }}disabled{{ else }}waves-effect{{ end }}"><a href="{{ $tagBaseURI }}{{ add .pagination.Current 1 }}"><i class="material-icons">chevron_right</i></a></li>
23 </ul> 30 </ul>
24 </div> 31 </div>
25 </div> 32 </div>