Skip to content

Commit 69aa478

Browse files
committed
fix
1 parent ea0223e commit 69aa478

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

modules/paginator/paginator.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package paginator
66

7+
import "code.gitea.io/gitea/modules/util"
8+
79
/*
810
In template:
911
@@ -32,25 +34,22 @@ Output:
3234

3335
// Paginator represents a set of results of pagination calculations.
3436
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
3942
}
4043

4144
// New initialize a new pagination calculation and returns a Paginator as result.
4245
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)
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.
7473
func (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

7877
func (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.
8685
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()
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.
9995
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
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.
136129
func (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.

modules/paginator/paginator_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ func TestPaginator(t *testing.T) {
7676
t.Run("Only current page", func(t *testing.T) {
7777
p := New(0, 10, 1, 1)
7878
pages := p.Pages()
79-
assert.Len(t, pages, 1)
80-
assert.Equal(t, 1, pages[0].Num())
81-
assert.True(t, pages[0].IsCurrent())
79+
assert.Empty(t, pages) // no "total", so no pages
8280

8381
p = New(1, 10, 1, 1)
8482
pages = p.Pages()

templates/base/paginate.tmpl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
{{$paginationLink := $.Link}}
33
{{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}}
44
{{with .Page.Paginater}}
5-
{{if gt .TotalPages 1}}
5+
{{if .TotalPages}}
6+
{{$showFirstLast := gt .TotalPages 0}}
67
<div class="center page buttons">
78
<div class="ui borderless pagination menu">
9+
{{if $showFirstLast}}
810
<a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$paginationLink}}{{if $paginationParams}}?{{$paginationParams}}{{end}}"{{end}}>
911
{{svg "gitea-double-chevron-left" 16 "tw-mr-1"}}
1012
<span class="navigation_label">{{ctx.Locale.Tr "admin.first_page"}}</span>
1113
</a>
14+
{{end}}
15+
1216
<a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$paginationLink}}?page={{.Previous}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
1317
{{svg "octicon-chevron-left" 16 "tw-mr-1"}}
1418
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.previous"}}</span>
@@ -24,10 +28,13 @@
2428
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.next"}}</span>
2529
{{svg "octicon-chevron-right" 16 "tw-ml-1"}}
2630
</a>
31+
32+
{{if $showFirstLast}}
2733
<a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$paginationLink}}?page={{.TotalPages}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
2834
<span class="navigation_label">{{ctx.Locale.Tr "admin.last_page"}}</span>
2935
{{svg "gitea-double-chevron-right" 16 "tw-ml-1"}}
3036
</a>
37+
{{end}}
3138
</div>
3239
</div>
3340
{{end}}

0 commit comments

Comments
 (0)