Skip to content

Commit fcbac38

Browse files
saithotechknowlogick
authored andcommitted
Unifies pagination template usage (#6531) (#6533)
1 parent 40dc458 commit fcbac38

File tree

21 files changed

+165
-196
lines changed

21 files changed

+165
-196
lines changed

modules/context/api.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import (
1919
"code.gitea.io/gitea/modules/log"
2020
"code.gitea.io/gitea/modules/setting"
2121

22-
"github.com/Unknwon/paginater"
23-
macaron "gopkg.in/macaron.v1"
22+
"gopkg.in/macaron.v1"
2423
)
2524

2625
// APIContext is a specific macaron context for API service
@@ -83,19 +82,20 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
8382

8483
// SetLinkHeader sets pagination link header by given total number and page size.
8584
func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
86-
page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
85+
page := NewPagination(total, pageSize, ctx.QueryInt("page"), 0)
86+
paginater := page.Paginater
8787
links := make([]string, 0, 4)
88-
if page.HasNext() {
89-
links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Next()))
88+
if paginater.HasNext() {
89+
links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], paginater.Next()))
9090
}
91-
if !page.IsLast() {
92-
links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], page.TotalPages()))
91+
if !paginater.IsLast() {
92+
links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], paginater.TotalPages()))
9393
}
94-
if !page.IsFirst() {
94+
if !paginater.IsFirst() {
9595
links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, ctx.Req.URL.Path[1:]))
9696
}
97-
if page.HasPrevious() {
98-
links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Previous()))
97+
if paginater.HasPrevious() {
98+
links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], paginater.Previous()))
9999
}
100100

101101
if len(links) > 0 {

modules/context/pagination.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 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+
package context
6+
7+
import (
8+
"fmt"
9+
"html/template"
10+
"net/url"
11+
"strings"
12+
13+
"github.com/Unknwon/paginater"
14+
)
15+
16+
// Pagination provides a pagination via Paginater and additional configurations for the link params used in rendering
17+
type Pagination struct {
18+
Paginater *paginater.Paginater
19+
urlParams []string
20+
}
21+
22+
// NewPagination creates a new instance of the Pagination struct
23+
func NewPagination(total int, page int, issueNum int, numPages int) *Pagination {
24+
p := &Pagination{}
25+
p.Paginater = paginater.New(total, page, issueNum, numPages)
26+
return p
27+
}
28+
29+
// AddParam adds a value from context identified by ctxKey as link param under a given paramKey
30+
func (p *Pagination) AddParam(ctx *Context, paramKey string, ctxKey string) {
31+
_, exists := ctx.Data[ctxKey]
32+
if !exists {
33+
return
34+
}
35+
paramData := fmt.Sprintf("%v", ctx.Data[ctxKey]) // cast interface{} to string
36+
urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(paramKey), url.QueryEscape(paramData))
37+
p.urlParams = append(p.urlParams, urlParam)
38+
}
39+
40+
// GetParams returns the configured URL params
41+
func (p *Pagination) GetParams() template.URL {
42+
return template.URL(strings.Join(p.urlParams[:], "&"))
43+
}
44+
45+
// SetDefaultParams sets common pagination params that are often used
46+
func (p *Pagination) SetDefaultParams(ctx *Context) {
47+
p.AddParam(ctx, "sort", "SortType")
48+
p.AddParam(ctx, "q", "Keyword")
49+
p.AddParam(ctx, "tab", "TabName")
50+
}

routers/admin/notice.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package admin
67

78
import (
8-
"github.com/Unknwon/com"
9-
"github.com/Unknwon/paginater"
10-
119
"code.gitea.io/gitea/models"
1210
"code.gitea.io/gitea/modules/base"
1311
"code.gitea.io/gitea/modules/context"
1412
"code.gitea.io/gitea/modules/log"
1513
"code.gitea.io/gitea/modules/setting"
14+
15+
"github.com/Unknwon/com"
1616
)
1717

1818
const (
@@ -30,7 +30,6 @@ func Notices(ctx *context.Context) {
3030
if page <= 1 {
3131
page = 1
3232
}
33-
ctx.Data["Page"] = paginater.New(int(total), setting.UI.Admin.NoticePagingNum, page, 5)
3433

3534
notices, err := models.Notices(page, setting.UI.Admin.NoticePagingNum)
3635
if err != nil {
@@ -40,6 +39,9 @@ func Notices(ctx *context.Context) {
4039
ctx.Data["Notices"] = notices
4140

4241
ctx.Data["Total"] = total
42+
43+
ctx.Data["Page"] = context.NewPagination(int(total), setting.UI.Admin.NoticePagingNum, page, 5)
44+
4345
ctx.HTML(200, tplNotices)
4446
}
4547

routers/home.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
"code.gitea.io/gitea/modules/setting"
1818
"code.gitea.io/gitea/modules/util"
1919
"code.gitea.io/gitea/routers/user"
20-
21-
"github.com/Unknwon/paginater"
2220
)
2321

2422
const (
@@ -151,10 +149,13 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
151149
}
152150
ctx.Data["Keyword"] = keyword
153151
ctx.Data["Total"] = count
154-
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
155152
ctx.Data["Repos"] = repos
156153
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
157154

155+
pager := context.NewPagination(int(count), opts.PageSize, page, 5)
156+
pager.SetDefaultParams(ctx)
157+
ctx.Data["Page"] = pager
158+
158159
ctx.HTML(200, opts.TplName)
159160
}
160161

@@ -222,11 +223,14 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
222223
}
223224
ctx.Data["Keyword"] = opts.Keyword
224225
ctx.Data["Total"] = count
225-
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, opts.Page, 5)
226226
ctx.Data["Users"] = users
227227
ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
228228
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
229229

230+
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
231+
pager.SetDefaultParams(ctx)
232+
ctx.Data["Page"] = pager
233+
230234
ctx.HTML(200, tplName)
231235
}
232236

@@ -364,11 +368,14 @@ func ExploreCode(ctx *context.Context) {
364368
}
365369

366370
ctx.Data["Keyword"] = keyword
367-
pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5)
368-
ctx.Data["Page"] = pager
369371
ctx.Data["SearchResults"] = searchResults
370372
ctx.Data["RequireHighlightJS"] = true
371373
ctx.Data["PageIsViewCode"] = true
374+
375+
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
376+
pager.SetDefaultParams(ctx)
377+
ctx.Data["Page"] = pager
378+
372379
ctx.HTML(200, tplExploreCode)
373380
}
374381

routers/repo/commit.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -14,8 +15,6 @@ import (
1415
"code.gitea.io/gitea/modules/git"
1516
"code.gitea.io/gitea/modules/log"
1617
"code.gitea.io/gitea/modules/setting"
17-
18-
"github.com/Unknwon/paginater"
1918
)
2019

2120
const (
@@ -55,7 +54,6 @@ func Commits(ctx *context.Context) {
5554
if page <= 1 {
5655
page = 1
5756
}
58-
ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
5957

6058
// Both `git log branchName` and `git log commitId` work.
6159
commits, err := ctx.Repo.Commit.CommitsByRange(page)
@@ -72,6 +70,11 @@ func Commits(ctx *context.Context) {
7270
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
7371
ctx.Data["CommitCount"] = commitsCount
7472
ctx.Data["Branch"] = ctx.Repo.BranchName
73+
74+
pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
75+
pager.SetDefaultParams(ctx)
76+
ctx.Data["Page"] = pager
77+
7578
ctx.HTML(200, tplCommits)
7679
}
7780

@@ -160,7 +163,6 @@ func FileHistory(ctx *context.Context) {
160163
if page <= 1 {
161164
page = 1
162165
}
163-
ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
164166

165167
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
166168
if err != nil {
@@ -177,6 +179,11 @@ func FileHistory(ctx *context.Context) {
177179
ctx.Data["FileName"] = fileName
178180
ctx.Data["CommitCount"] = commitsCount
179181
ctx.Data["Branch"] = branchName
182+
183+
pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
184+
pager.SetDefaultParams(ctx)
185+
ctx.Data["Page"] = pager
186+
180187
ctx.HTML(200, tplCommits)
181188
}
182189

routers/repo/issue.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"code.gitea.io/gitea/modules/util"
2828

2929
"github.com/Unknwon/com"
30-
"github.com/Unknwon/paginater"
3130
)
3231

3332
const (
@@ -186,8 +185,7 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
186185
} else {
187186
total = int(issueStats.ClosedCount)
188187
}
189-
pager := paginater.New(total, setting.UI.IssuePagingNum, page, 5)
190-
ctx.Data["Page"] = pager
188+
pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5)
191189

192190
var issues []*models.Issue
193191
if forceEmpty {
@@ -199,7 +197,7 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
199197
PosterID: posterID,
200198
MentionedID: mentionedID,
201199
MilestoneID: milestoneID,
202-
Page: pager.Current(),
200+
Page: pager.Paginater.Current(),
203201
PageSize: setting.UI.IssuePagingNum,
204202
IsClosed: util.OptionalBoolOf(isShowClosed),
205203
IsPull: isPullOption,
@@ -268,6 +266,15 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
268266
} else {
269267
ctx.Data["State"] = "open"
270268
}
269+
270+
pager.AddParam(ctx, "q", "Keyword")
271+
pager.AddParam(ctx, "type", "ViewType")
272+
pager.AddParam(ctx, "sort", "SortType")
273+
pager.AddParam(ctx, "state", "State")
274+
pager.AddParam(ctx, "labels", "SelectLabels")
275+
pager.AddParam(ctx, "milestone", "MilestoneID")
276+
pager.AddParam(ctx, "assignee", "AssigneeID")
277+
ctx.Data["Page"] = pager
271278
}
272279

273280
// Issues render issues page

routers/repo/milestone.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"code.gitea.io/gitea/modules/markup/markdown"
1515
"code.gitea.io/gitea/modules/setting"
1616
"code.gitea.io/gitea/modules/util"
17-
"github.com/Unknwon/paginater"
1817
)
1918

2019
const (
@@ -51,7 +50,6 @@ func Milestones(ctx *context.Context) {
5150
} else {
5251
total = int(closedCount)
5352
}
54-
ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
5553

5654
miles, err := models.GetMilestones(ctx.Repo.Repository.ID, page, isShowClosed, sortType)
5755
if err != nil {
@@ -77,6 +75,11 @@ func Milestones(ctx *context.Context) {
7775

7876
ctx.Data["SortType"] = sortType
7977
ctx.Data["IsShowClosed"] = isShowClosed
78+
79+
pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5)
80+
pager.AddParam(ctx, "state", "State")
81+
ctx.Data["Page"] = pager
82+
8083
ctx.HTML(200, tplMilestone)
8184
}
8285

routers/repo/release.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/markup/markdown"
1717
"code.gitea.io/gitea/modules/setting"
18-
19-
"github.com/Unknwon/paginater"
2018
)
2119

2220
const (
@@ -120,9 +118,12 @@ func Releases(ctx *context.Context) {
120118
r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
121119
}
122120

123-
pager := paginater.New(int(count), limit, page, 5)
124-
ctx.Data["Page"] = pager
125121
ctx.Data["Releases"] = releases
122+
123+
pager := context.NewPagination(int(count), limit, page, 5)
124+
pager.SetDefaultParams(ctx)
125+
ctx.Data["Page"] = pager
126+
126127
ctx.HTML(200, tplReleases)
127128
}
128129

routers/repo/search.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"code.gitea.io/gitea/modules/context"
1313
"code.gitea.io/gitea/modules/search"
1414
"code.gitea.io/gitea/modules/setting"
15-
16-
"github.com/Unknwon/paginater"
1715
)
1816

1917
const tplSearch base.TplName = "repo/search"
@@ -36,12 +34,15 @@ func Search(ctx *context.Context) {
3634
return
3735
}
3836
ctx.Data["Keyword"] = keyword
39-
pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5)
40-
ctx.Data["Page"] = pager
4137
ctx.Data["SourcePath"] = setting.AppSubURL + "/" +
4238
path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name, "src", "branch", ctx.Repo.Repository.DefaultBranch)
4339
ctx.Data["SearchResults"] = searchResults
4440
ctx.Data["RequireHighlightJS"] = true
4541
ctx.Data["PageIsViewCode"] = true
42+
43+
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
44+
pager.SetDefaultParams(ctx)
45+
ctx.Data["Page"] = pager
46+
4647
ctx.HTML(200, tplSearch)
4748
}

routers/repo/view.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"code.gitea.io/gitea/modules/markup"
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/templates"
27-
28-
"github.com/Unknwon/paginater"
2927
)
3028

3129
const (
@@ -462,10 +460,10 @@ func RenderUserCards(ctx *context.Context, total int, getter func(page int) ([]*
462460
if page <= 0 {
463461
page = 1
464462
}
465-
pager := paginater.New(total, models.ItemsPerPage, page, 5)
463+
pager := context.NewPagination(total, models.ItemsPerPage, page, 5)
466464
ctx.Data["Page"] = pager
467465

468-
items, err := getter(pager.Current())
466+
items, err := getter(pager.Paginater.Current())
469467
if err != nil {
470468
ctx.ServerError("getter", err)
471469
return
@@ -480,6 +478,7 @@ func Watchers(ctx *context.Context) {
480478
ctx.Data["Title"] = ctx.Tr("repo.watchers")
481479
ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
482480
ctx.Data["PageIsWatchers"] = true
481+
483482
RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, tplWatchers)
484483
}
485484

0 commit comments

Comments
 (0)