44
55package paginator
66
7+ import "code.gitea.io/gitea/modules/util"
8+
79/*
810In template:
911
@@ -32,25 +34,22 @@ Output:
3234
3335// Paginator represents a set of results of pagination calculations.
3436type 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
3942}
4043
4144// New initialize a new pagination calculation and returns a Paginator as result.
4245func 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 )
5250 }
53- return p
51+ current = max (current , 1 )
52+ return & Paginator {total , totalPages , pagingNum , current , numPages }
5453}
5554
5655// IsFirst returns true if current page is the first page.
@@ -72,7 +71,7 @@ func (p *Paginator) Previous() int {
7271
7372// HasNext returns true if there is a next page relative to current page.
7473func (p * Paginator ) HasNext () bool {
75- return p .total > p .current * p .pagingNum
74+ return p .total == - 1 || p .current * p .pagingNum < p . total
7675}
7776
7877func (p * Paginator ) Next () int {
@@ -84,10 +83,7 @@ func (p *Paginator) Next() int {
8483
8584// IsLast returns true if current page is the last page.
8685func (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 ()
9187}
9288
9389// Total returns number of total rows.
@@ -97,10 +93,7 @@ func (p *Paginator) Total() int {
9793
9894// TotalPages returns number of total pages.
9995func (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
10497}
10598
10699// Current returns current page number.
@@ -135,10 +128,10 @@ func getMiddleIdx(numPages int) int {
135128// If value is -1 means "..." that more pages are not showing.
136129func (p * Paginator ) Pages () []* Page {
137130 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 ) {
140133 // Only show current page.
141- return []* Page {{1 , true }}
134+ return []* Page {{p . current , true }}
142135 }
143136
144137 // Total page number is less or equal.
0 commit comments