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,22 @@ 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
36
- pagingNum int // how many rows in one page
37
- current int // current page number
38
- numPages int // how many pages to show on the UI
37
+ total int // total rows count, -1 means unknown
38
+ totalPages int // total pages count, -1 means unknown
39
+ pagingNum int // how many rows in one page
40
+ current int // current page number
41
+ numPages int // how many pages to show on the UI
39
42
}
40
43
41
44
// New initialize a new pagination calculation and returns a Paginator as result.
42
45
func New (total , pagingNum , current , numPages int ) * Paginator {
43
- if pagingNum <= 0 {
44
- pagingNum = 1
45
- }
46
- if current <= 0 {
47
- current = 1
48
- }
49
- p := & Paginator {total , pagingNum , current , numPages }
50
- if p .current > p .TotalPages () {
51
- p .current = p .TotalPages ()
46
+ pagingNum = max (pagingNum , 1 )
47
+ totalPages := util .Iif (total == - 1 , - 1 , (total + pagingNum - 1 )/ pagingNum )
48
+ if total >= 0 {
49
+ current = min (current , totalPages )
52
50
}
53
- return p
51
+ current = max (current , 1 )
52
+ return & Paginator {total , totalPages , pagingNum , current , numPages }
54
53
}
55
54
56
55
// IsFirst returns true if current page is the first page.
@@ -72,7 +71,7 @@ func (p *Paginator) Previous() int {
72
71
73
72
// HasNext returns true if there is a next page relative to current page.
74
73
func (p * Paginator ) HasNext () bool {
75
- return p .total > p .current * p .pagingNum
74
+ return p .total == - 1 || p .current * p .pagingNum < p . total
76
75
}
77
76
78
77
func (p * Paginator ) Next () int {
@@ -84,10 +83,7 @@ func (p *Paginator) Next() int {
84
83
85
84
// IsLast returns true if current page is the last page.
86
85
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 ()
86
+ return ! p .HasNext ()
91
87
}
92
88
93
89
// Total returns number of total rows.
@@ -97,10 +93,7 @@ func (p *Paginator) Total() int {
97
93
98
94
// TotalPages returns number of total pages.
99
95
func (p * Paginator ) TotalPages () int {
100
- if p .total == 0 {
101
- return 1
102
- }
103
- return (p .total + p .pagingNum - 1 ) / p .pagingNum
96
+ return p .totalPages
104
97
}
105
98
106
99
// Current returns current page number.
@@ -135,10 +128,10 @@ func getMiddleIdx(numPages int) int {
135
128
// If value is -1 means "..." that more pages are not showing.
136
129
func (p * Paginator ) Pages () []* Page {
137
130
if p .numPages == 0 {
138
- return [] * Page {}
139
- } else if p .numPages == 1 && p .TotalPages () == 1 {
131
+ return nil
132
+ } else if p .total == - 1 || ( p . numPages == 1 && p .TotalPages () == 1 ) {
140
133
// Only show current page.
141
- return []* Page {{1 , true }}
134
+ return []* Page {{p . current , true }}
142
135
}
143
136
144
137
// Total page number is less or equal.
0 commit comments