openidec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 31c6ffb260f01e4f1b092ec359f50bf621542012
parent bdfa1a6f83e2f29e419f9cacc5f5d48b4883fe01
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Thu,  3 Sep 2020 10:39:54 +0300

pager up

Diffstat:
Mii-node/tpl/topics.tpl | 8+++++++-
Mii-node/web.go | 33+++++++++++++++++++++++----------
2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/ii-node/tpl/topics.tpl b/ii-node/tpl/topics.tpl @@ -1,5 +1,11 @@ {{template "header.tpl"}} -<a href="/{{.Echo}}/1">1</a><a href="/{{.Echo}}/{{.Pages}}">{{.Pages}}</a> +{{ range .Pager }} +{{ if eq . $.Page }} +<a href="/{{$.Echo}}/{{.}}">[{{.}}]</a> +{{ else }} +<a href="/{{$.Echo}}/{{.}}">{{.}}</a> +{{ end }} +{{ end }} <table class="topiclist"> <tr> <th>Topics</th> diff --git a/ii-node/web.go b/ii-node/web.go @@ -19,6 +19,7 @@ type WebContext struct { Echo string Page int Pages int + Pager []int } func www_index(www WWW, w http.ResponseWriter, r *http.Request) error { @@ -75,7 +76,24 @@ type Topic struct { } const PAGE_SIZE = 100 - +func makePager(ctx *WebContext, count int, page int) int { + ctx.Pages = count / PAGE_SIZE + if count % PAGE_SIZE != 0 { + ctx.Pages ++ + } + if page == 0 { + page ++ + } else if page < 0 { + page = ctx.Pages + page + 1 + } + start := (page - 1)* PAGE_SIZE + if start < 0 { + start = 0 + page = 1 + } + ctx.Page = page + return start +} func www_topics(www WWW, w http.ResponseWriter, r *http.Request, echo string, page int) error { db := www.db var ctx WebContext @@ -103,8 +121,9 @@ func www_topics(www WWW, w http.ResponseWriter, r *http.Request, echo string, pa sort.SliceStable(topics, func(i, j int) bool { return topics[i].Last.Off > topics[j].Last.Off }) + ctx.Echo = echo tcount := len(topics) - start := (page - 1)* PAGE_SIZE + start := makePager(&ctx, tcount, page) nr := PAGE_SIZE for i := start; i < tcount && nr > 0; i ++ { t := topics[i] @@ -119,11 +138,8 @@ func www_topics(www WWW, w http.ResponseWriter, r *http.Request, echo string, pa nr -- } ii.Trace.Printf("Stop to generate topics") - ctx.Page = page - ctx.Pages = tcount / PAGE_SIZE - ctx.Echo = echo - if tcount % PAGE_SIZE != 0 { - ctx.Pages ++ + for i := 1; i <= ctx.Pages; i++ { + ctx.Pager = append(ctx.Pager, i) } err := www.tpl.ExecuteTemplate(w, "topics.tpl", ctx) return err @@ -171,9 +187,6 @@ func Web(www WWW, w http.ResponseWriter, r *http.Request) error { if len(args) > 1 { fmt.Sscanf(args[1], "%d", &page) } - if page <= 0 { - page = 1 - } return www_topics(www, w, r, args[0], page) } return nil