Skip to content

Commit ee09954

Browse files
committed
remove legacy paginater
1 parent 3ea09eb commit ee09954

File tree

5 files changed

+525
-7
lines changed

5 files changed

+525
-7
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ require (
7878
github.com/stretchr/testify v1.7.0
7979
github.com/syndtr/goleveldb v1.0.0
8080
github.com/tstranex/u2f v1.0.0
81-
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae
8281
github.com/unrolled/render v1.4.1
8382
github.com/urfave/cli v1.22.5
8483
github.com/xanzy/go-gitlab v0.58.0

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1503,8 +1503,6 @@ github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o
15031503
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM=
15041504
github.com/unknwon/com v1.0.1 h1:3d1LTxD+Lnf3soQiD4Cp/0BRB+Rsa/+RTvz8GMMzIXs=
15051505
github.com/unknwon/com v1.0.1/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM=
1506-
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae h1:ihaXiJkaca54IaCSnEXtE/uSZOmPxKZhDfVLrzZLFDs=
1507-
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae/go.mod h1:1fdkY6xxl6ExVs2QFv7R0F5IRZHKA8RahhB9fMC9RvM=
15081506
github.com/unrolled/render v1.4.1 h1:VdpMc2YkAOWzbmC/P2yoHhRDXgsaCQHcTJ1KK6SNCA4=
15091507
github.com/unrolled/render v1.4.1/go.mod h1:cK4RSTTVdND5j9EYEc0LAMOvdG11JeiKjyjfyZRvV2w=
15101508
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=

modules/context/pagination.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import (
1010
"net/url"
1111
"strings"
1212

13-
"github.com/unknwon/paginater"
13+
"code.gitea.io/gitea/modules/paginator"
1414
)
1515

16-
// Pagination provides a pagination via Paginater and additional configurations for the link params used in rendering
16+
// Pagination provides a pagination via paginator.Paginator and additional configurations for the link params used in rendering
1717
type Pagination struct {
18-
Paginater *paginater.Paginater
18+
Paginater *paginator.Paginator
1919
urlParams []string
2020
}
2121

2222
// NewPagination creates a new instance of the Pagination struct
2323
func NewPagination(total, page, issueNum, numPages int) *Pagination {
2424
p := &Pagination{}
25-
p.Paginater = paginater.New(total, page, issueNum, numPages)
25+
p.Paginater = paginator.New(total, page, issueNum, numPages)
2626
return p
2727
}
2828

@@ -53,5 +53,6 @@ func (p *Pagination) SetDefaultParams(ctx *Context) {
5353
p.AddParam(ctx, "sort", "SortType")
5454
p.AddParam(ctx, "q", "Keyword")
5555
p.AddParam(ctx, "tab", "TabName")
56+
// do not add any more uncommon params here!
5657
p.AddParam(ctx, "t", "queryType")
5758
}

modules/paginator/paginator.go

+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Copyright 2015 Unknwon. Licensed under the Apache License, Version 2.0
6+
7+
package paginator
8+
9+
/*
10+
In template:
11+
12+
```html
13+
{{if not .Page.IsFirst}}[First](1){{end}}
14+
{{if .Page.HasPrevious}}[Previous]({{.Page.Previous}}){{end}}
15+
16+
{{range .Page.Pages}}
17+
{{if eq .Num -1}}
18+
...
19+
{{else}}
20+
{{.Num}}{{if .IsCurrent}}(current){{end}}
21+
{{end}}
22+
{{end}}
23+
24+
{{if .Page.HasNext}}[Next]({{.Page.Next}}){{end}}
25+
{{if not .Page.IsLast}}[Last]({{.Page.TotalPages}}){{end}}
26+
```
27+
28+
Output:
29+
30+
```
31+
[First](1) [Previous](2) ... 2 3(current) 4 ... [Next](4) [Last](5)
32+
```
33+
*/
34+
35+
// Paginator represents a set of results of pagination calculations.
36+
type Paginator struct {
37+
total int // total rows count
38+
pagingNum int // how many rows in one page
39+
current int // current page number
40+
numPages int // how many pages to show on the UI
41+
}
42+
43+
// New initialize a new pagination calculation and returns a Paginator as result.
44+
func New(total, pagingNum, current, numPages int) *Paginator {
45+
if pagingNum <= 0 {
46+
pagingNum = 1
47+
}
48+
if current <= 0 {
49+
current = 1
50+
}
51+
p := &Paginator{total, pagingNum, current, numPages}
52+
if p.current > p.TotalPages() {
53+
p.current = p.TotalPages()
54+
}
55+
return p
56+
}
57+
58+
// IsFirst returns true if current page is the first page.
59+
func (p *Paginator) IsFirst() bool {
60+
return p.current == 1
61+
}
62+
63+
// HasPrevious returns true if there is a previous page relative to current page.
64+
func (p *Paginator) HasPrevious() bool {
65+
return p.current > 1
66+
}
67+
68+
func (p *Paginator) Previous() int {
69+
if !p.HasPrevious() {
70+
return p.current
71+
}
72+
return p.current - 1
73+
}
74+
75+
// HasNext returns true if there is a next page relative to current page.
76+
func (p *Paginator) HasNext() bool {
77+
return p.total > p.current*p.pagingNum
78+
}
79+
80+
func (p *Paginator) Next() int {
81+
if !p.HasNext() {
82+
return p.current
83+
}
84+
return p.current + 1
85+
}
86+
87+
// IsLast returns true if current page is the last page.
88+
func (p *Paginator) IsLast() bool {
89+
if p.total == 0 {
90+
return true
91+
}
92+
return p.total > (p.current-1)*p.pagingNum && !p.HasNext()
93+
}
94+
95+
// Total returns number of total rows.
96+
func (p *Paginator) Total() int {
97+
return p.total
98+
}
99+
100+
// TotalPages returns number of total pages.
101+
func (p *Paginator) TotalPages() int {
102+
if p.total == 0 {
103+
return 1
104+
}
105+
return (p.total + p.pagingNum - 1) / p.pagingNum
106+
}
107+
108+
// Current returns current page number.
109+
func (p *Paginator) Current() int {
110+
return p.current
111+
}
112+
113+
// PagingNum returns number of page size.
114+
func (p *Paginator) PagingNum() int {
115+
return p.pagingNum
116+
}
117+
118+
// Page presents a page in the paginator.
119+
type Page struct {
120+
num int
121+
isCurrent bool
122+
}
123+
124+
func (p *Page) Num() int {
125+
return p.num
126+
}
127+
128+
func (p *Page) IsCurrent() bool {
129+
return p.isCurrent
130+
}
131+
132+
func getMiddleIdx(numPages int) int {
133+
return (numPages + 1) / 2
134+
}
135+
136+
// Pages returns a list of nearby page numbers relative to current page.
137+
// If value is -1 means "..." that more pages are not showing.
138+
func (p *Paginator) Pages() []*Page {
139+
if p.numPages == 0 {
140+
return []*Page{}
141+
} else if p.numPages == 1 && p.TotalPages() == 1 {
142+
// Only show current page.
143+
return []*Page{{1, true}}
144+
}
145+
146+
// Total page number is less or equal.
147+
if p.TotalPages() <= p.numPages {
148+
pages := make([]*Page, p.TotalPages())
149+
for i := range pages {
150+
pages[i] = &Page{i + 1, i+1 == p.current}
151+
}
152+
return pages
153+
}
154+
155+
numPages := p.numPages
156+
offsetIdx := 0
157+
hasMoreNext := false
158+
159+
// Check more previous and next pages.
160+
previousNum := getMiddleIdx(p.numPages) - 1
161+
if previousNum > p.current-1 {
162+
previousNum -= previousNum - (p.current - 1)
163+
}
164+
nextNum := p.numPages - previousNum - 1
165+
if p.current+nextNum > p.TotalPages() {
166+
delta := nextNum - (p.TotalPages() - p.current)
167+
nextNum -= delta
168+
previousNum += delta
169+
}
170+
171+
offsetVal := p.current - previousNum
172+
if offsetVal > 1 {
173+
numPages++
174+
offsetIdx = 1
175+
}
176+
177+
if p.current+nextNum < p.TotalPages() {
178+
numPages++
179+
hasMoreNext = true
180+
}
181+
182+
pages := make([]*Page, numPages)
183+
184+
// There are more previous pages.
185+
if offsetIdx == 1 {
186+
pages[0] = &Page{-1, false}
187+
}
188+
// There are more next pages.
189+
if hasMoreNext {
190+
pages[len(pages)-1] = &Page{-1, false}
191+
}
192+
193+
// Check previous pages.
194+
for i := 0; i < previousNum; i++ {
195+
pages[offsetIdx+i] = &Page{i + offsetVal, false}
196+
}
197+
198+
pages[offsetIdx+previousNum] = &Page{p.current, true}
199+
200+
// Check next pages.
201+
for i := 1; i <= nextNum; i++ {
202+
pages[offsetIdx+previousNum+i] = &Page{p.current + i, false}
203+
}
204+
205+
return pages
206+
}

0 commit comments

Comments
 (0)