4
4
5
5
package paginator
6
6
7
+ import "code.gitea.io/gitea/modules/util"
8
+
7
9
/*
8
10
In template:
9
11
@@ -32,25 +34,43 @@ Output:
32
34
33
35
// Paginator represents a set of results of pagination calculations.
34
36
type Paginator struct {
35
- total int // total rows count
37
+ total int // total rows count, -1 means unknown
38
+ totalPages int // total pages count, -1 means unknown
39
+ current int // current page number
40
+ curRows int // current page rows count
41
+
36
42
pagingNum int // how many rows in one page
37
- current int // current page number
38
43
numPages int // how many pages to show on the UI
39
44
}
40
45
41
46
// New initialize a new pagination calculation and returns a Paginator as result.
42
47
func New (total , pagingNum , current , numPages int ) * Paginator {
43
- if pagingNum <= 0 {
44
- pagingNum = 1
48
+ pagingNum = max (pagingNum , 1 )
49
+ totalPages := util .Iif (total == - 1 , - 1 , (total + pagingNum - 1 )/ pagingNum )
50
+ if total >= 0 {
51
+ current = min (current , totalPages )
45
52
}
46
- if current <= 0 {
47
- current = 1
53
+ current = max (current , 1 )
54
+ return & Paginator {
55
+ total : total ,
56
+ totalPages : totalPages ,
57
+ current : current ,
58
+ pagingNum : pagingNum ,
59
+ numPages : numPages ,
48
60
}
49
- p := & Paginator {total , pagingNum , current , numPages }
50
- if p .current > p .TotalPages () {
51
- p .current = p .TotalPages ()
61
+ }
62
+
63
+ func (p * Paginator ) SetCurRows (rows int ) {
64
+ // For "unlimited paging", we need to know the rows of current page to determine if there is a next page.
65
+ // There is still an edge case: when curRows==pagingNum, then the "next page" will be an empty page.
66
+ // Ideally we should query one more row to determine if there is really a next page, but it's impossible in current framework.
67
+ p .curRows = rows
68
+ if p .total == - 1 && p .current == 1 && ! p .HasNext () {
69
+ // if there is only one page for the "unlimited paging", set total rows/pages count
70
+ // then the tmpl could decide to hide the nav bar.
71
+ p .total = rows
72
+ p .totalPages = util .Iif (p .total == 0 , 0 , 1 )
52
73
}
53
- return p
54
74
}
55
75
56
76
// IsFirst returns true if current page is the first page.
@@ -72,7 +92,10 @@ func (p *Paginator) Previous() int {
72
92
73
93
// HasNext returns true if there is a next page relative to current page.
74
94
func (p * Paginator ) HasNext () bool {
75
- return p .total > p .current * p .pagingNum
95
+ if p .total == - 1 {
96
+ return p .curRows >= p .pagingNum
97
+ }
98
+ return p .current * p .pagingNum < p .total
76
99
}
77
100
78
101
func (p * Paginator ) Next () int {
@@ -84,10 +107,7 @@ func (p *Paginator) Next() int {
84
107
85
108
// IsLast returns true if current page is the last page.
86
109
func (p * Paginator ) IsLast () bool {
87
- if p .total == 0 {
88
- return true
89
- }
90
- return p .total > (p .current - 1 )* p .pagingNum && ! p .HasNext ()
110
+ return ! p .HasNext ()
91
111
}
92
112
93
113
// Total returns number of total rows.
@@ -97,10 +117,7 @@ func (p *Paginator) Total() int {
97
117
98
118
// TotalPages returns number of total pages.
99
119
func (p * Paginator ) TotalPages () int {
100
- if p .total == 0 {
101
- return 1
102
- }
103
- return (p .total + p .pagingNum - 1 ) / p .pagingNum
120
+ return p .totalPages
104
121
}
105
122
106
123
// Current returns current page number.
@@ -135,10 +152,10 @@ func getMiddleIdx(numPages int) int {
135
152
// If value is -1 means "..." that more pages are not showing.
136
153
func (p * Paginator ) Pages () []* Page {
137
154
if p .numPages == 0 {
138
- return [] * Page {}
139
- } else if p .numPages == 1 && p .TotalPages () == 1 {
155
+ return nil
156
+ } else if p .total == - 1 || ( p . numPages == 1 && p .TotalPages () == 1 ) {
140
157
// Only show current page.
141
- return []* Page {{1 , true }}
158
+ return []* Page {{p . current , true }}
142
159
}
143
160
144
161
// Total page number is less or equal.
0 commit comments