diff --git a/modules/context/api.go b/modules/context/api.go index cbabfe40e18..61f65147593 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -19,8 +19,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - "github.com/Unknwon/paginater" - macaron "gopkg.in/macaron.v1" + "gopkg.in/macaron.v1" ) // APIContext is a specific macaron context for API service @@ -83,19 +82,20 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) { // SetLinkHeader sets pagination link header by given total number and page size. func (ctx *APIContext) SetLinkHeader(total, pageSize int) { - page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0) + page := NewPagination(total, pageSize, ctx.QueryInt("page"), 0) + paginater := page.Paginater links := make([]string, 0, 4) - if page.HasNext() { - links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Next())) + if paginater.HasNext() { + links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], paginater.Next())) } - if !page.IsLast() { - links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], page.TotalPages())) + if !paginater.IsLast() { + links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], paginater.TotalPages())) } - if !page.IsFirst() { + if !paginater.IsFirst() { links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, ctx.Req.URL.Path[1:])) } - if page.HasPrevious() { - links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Previous())) + if paginater.HasPrevious() { + links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], paginater.Previous())) } if len(links) > 0 { diff --git a/modules/context/pagination.go b/modules/context/pagination.go new file mode 100644 index 00000000000..4795f650fb8 --- /dev/null +++ b/modules/context/pagination.go @@ -0,0 +1,50 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package context + +import ( + "fmt" + "html/template" + "net/url" + "strings" + + "github.com/Unknwon/paginater" +) + +// Pagination provides a pagination via Paginater and additional configurations for the link params used in rendering +type Pagination struct { + Paginater *paginater.Paginater + urlParams []string +} + +// NewPagination creates a new instance of the Pagination struct +func NewPagination(total int, page int, issueNum int, numPages int) *Pagination { + p := &Pagination{} + p.Paginater = paginater.New(total, page, issueNum, numPages) + return p +} + +// AddParam adds a value from context identified by ctxKey as link param under a given paramKey +func (p *Pagination) AddParam(ctx *Context, paramKey string, ctxKey string) { + _, exists := ctx.Data[ctxKey] + if !exists { + return + } + paramData := fmt.Sprintf("%v", ctx.Data[ctxKey]) // cast interface{} to string + urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(paramKey), url.QueryEscape(paramData)) + p.urlParams = append(p.urlParams, urlParam) +} + +// GetParams returns the configured URL params +func (p *Pagination) GetParams() template.URL { + return template.URL(strings.Join(p.urlParams[:], "&")) +} + +// SetDefaultParams sets common pagination params that are often used +func (p *Pagination) SetDefaultParams(ctx *Context) { + p.AddParam(ctx, "sort", "SortType") + p.AddParam(ctx, "q", "Keyword") + p.AddParam(ctx, "tab", "TabName") +} diff --git a/routers/admin/notice.go b/routers/admin/notice.go index 5892d88e582..d2c067c143b 100644 --- a/routers/admin/notice.go +++ b/routers/admin/notice.go @@ -1,18 +1,18 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package admin import ( - "github.com/Unknwon/com" - "github.com/Unknwon/paginater" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + + "github.com/Unknwon/com" ) const ( @@ -30,7 +30,6 @@ func Notices(ctx *context.Context) { if page <= 1 { page = 1 } - ctx.Data["Page"] = paginater.New(int(total), setting.UI.Admin.NoticePagingNum, page, 5) notices, err := models.Notices(page, setting.UI.Admin.NoticePagingNum) if err != nil { @@ -40,6 +39,9 @@ func Notices(ctx *context.Context) { ctx.Data["Notices"] = notices ctx.Data["Total"] = total + + ctx.Data["Page"] = context.NewPagination(int(total), setting.UI.Admin.NoticePagingNum, page, 5) + ctx.HTML(200, tplNotices) } diff --git a/routers/home.go b/routers/home.go index a9c4774867f..dbe27bd425c 100644 --- a/routers/home.go +++ b/routers/home.go @@ -17,8 +17,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/user" - - "github.com/Unknwon/paginater" ) const ( @@ -151,10 +149,13 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { } ctx.Data["Keyword"] = keyword ctx.Data["Total"] = count - ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5) ctx.Data["Repos"] = repos ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled + pager := context.NewPagination(int(count), opts.PageSize, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, opts.TplName) } @@ -222,11 +223,14 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN } ctx.Data["Keyword"] = opts.Keyword ctx.Data["Total"] = count - ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, opts.Page, 5) ctx.Data["Users"] = users ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled + pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, tplName) } @@ -364,11 +368,14 @@ func ExploreCode(ctx *context.Context) { } ctx.Data["Keyword"] = keyword - pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5) - ctx.Data["Page"] = pager ctx.Data["SearchResults"] = searchResults ctx.Data["RequireHighlightJS"] = true ctx.Data["PageIsViewCode"] = true + + pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, tplExploreCode) } diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 9263bcac7fb..2978eda6c01 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -14,8 +15,6 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - - "github.com/Unknwon/paginater" ) const ( @@ -55,7 +54,6 @@ func Commits(ctx *context.Context) { if page <= 1 { page = 1 } - ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5) // Both `git log branchName` and `git log commitId` work. commits, err := ctx.Repo.Commit.CommitsByRange(page) @@ -72,6 +70,11 @@ func Commits(ctx *context.Context) { ctx.Data["Reponame"] = ctx.Repo.Repository.Name ctx.Data["CommitCount"] = commitsCount ctx.Data["Branch"] = ctx.Repo.BranchName + + pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, tplCommits) } @@ -160,7 +163,6 @@ func FileHistory(ctx *context.Context) { if page <= 1 { page = 1 } - ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5) commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page) if err != nil { @@ -177,6 +179,11 @@ func FileHistory(ctx *context.Context) { ctx.Data["FileName"] = fileName ctx.Data["CommitCount"] = commitsCount ctx.Data["Branch"] = branchName + + pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, tplCommits) } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 7d235d84ef9..c818ac4840b 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -27,7 +27,6 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/Unknwon/com" - "github.com/Unknwon/paginater" ) const ( @@ -186,8 +185,7 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB } else { total = int(issueStats.ClosedCount) } - pager := paginater.New(total, setting.UI.IssuePagingNum, page, 5) - ctx.Data["Page"] = pager + pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5) var issues []*models.Issue if forceEmpty { @@ -199,7 +197,7 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB PosterID: posterID, MentionedID: mentionedID, MilestoneID: milestoneID, - Page: pager.Current(), + Page: pager.Paginater.Current(), PageSize: setting.UI.IssuePagingNum, IsClosed: util.OptionalBoolOf(isShowClosed), IsPull: isPullOption, @@ -268,6 +266,15 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB } else { ctx.Data["State"] = "open" } + + pager.AddParam(ctx, "q", "Keyword") + pager.AddParam(ctx, "type", "ViewType") + pager.AddParam(ctx, "sort", "SortType") + pager.AddParam(ctx, "state", "State") + pager.AddParam(ctx, "labels", "SelectLabels") + pager.AddParam(ctx, "milestone", "MilestoneID") + pager.AddParam(ctx, "assignee", "AssigneeID") + ctx.Data["Page"] = pager } // Issues render issues page diff --git a/routers/repo/milestone.go b/routers/repo/milestone.go index 632c792c434..644f7e043ba 100644 --- a/routers/repo/milestone.go +++ b/routers/repo/milestone.go @@ -14,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" - "github.com/Unknwon/paginater" ) const ( @@ -51,7 +50,6 @@ func Milestones(ctx *context.Context) { } else { total = int(closedCount) } - ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5) miles, err := models.GetMilestones(ctx.Repo.Repository.ID, page, isShowClosed, sortType) if err != nil { @@ -77,6 +75,11 @@ func Milestones(ctx *context.Context) { ctx.Data["SortType"] = sortType ctx.Data["IsShowClosed"] = isShowClosed + + pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5) + pager.AddParam(ctx, "state", "State") + ctx.Data["Page"] = pager + ctx.HTML(200, tplMilestone) } diff --git a/routers/repo/release.go b/routers/repo/release.go index 38e8da14018..fdef533f105 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -15,8 +15,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" - - "github.com/Unknwon/paginater" ) const ( @@ -120,9 +118,12 @@ func Releases(ctx *context.Context) { r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) } - pager := paginater.New(int(count), limit, page, 5) - ctx.Data["Page"] = pager ctx.Data["Releases"] = releases + + pager := context.NewPagination(int(count), limit, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, tplReleases) } diff --git a/routers/repo/search.go b/routers/repo/search.go index 95715c30c63..de16eda83d1 100644 --- a/routers/repo/search.go +++ b/routers/repo/search.go @@ -12,8 +12,6 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/search" "code.gitea.io/gitea/modules/setting" - - "github.com/Unknwon/paginater" ) const tplSearch base.TplName = "repo/search" @@ -36,12 +34,15 @@ func Search(ctx *context.Context) { return } ctx.Data["Keyword"] = keyword - pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5) - ctx.Data["Page"] = pager ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name, "src", "branch", ctx.Repo.Repository.DefaultBranch) ctx.Data["SearchResults"] = searchResults ctx.Data["RequireHighlightJS"] = true ctx.Data["PageIsViewCode"] = true + + pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, tplSearch) } diff --git a/routers/repo/view.go b/routers/repo/view.go index d20f94dfba6..fa1e2d07471 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -24,8 +24,6 @@ import ( "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/templates" - - "github.com/Unknwon/paginater" ) const ( @@ -462,10 +460,10 @@ func RenderUserCards(ctx *context.Context, total int, getter func(page int) ([]* if page <= 0 { page = 1 } - pager := paginater.New(total, models.ItemsPerPage, page, 5) + pager := context.NewPagination(total, models.ItemsPerPage, page, 5) ctx.Data["Page"] = pager - items, err := getter(pager.Current()) + items, err := getter(pager.Paginater.Current()) if err != nil { ctx.ServerError("getter", err) return @@ -480,6 +478,7 @@ func Watchers(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.watchers") ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers") ctx.Data["PageIsWatchers"] = true + RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, tplWatchers) } diff --git a/routers/user/home.go b/routers/user/home.go index 8eedeb70bd1..79377ac500f 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -18,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/Unknwon/com" - "github.com/Unknwon/paginater" "github.com/keybase/go-crypto/openpgp" "github.com/keybase/go-crypto/openpgp/armor" ) @@ -354,7 +353,6 @@ func Issues(ctx *context.Context) { ctx.Data["CommitStatus"] = commitStatus ctx.Data["Repos"] = showRepos ctx.Data["Counts"] = counts - ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5) ctx.Data["IssueStats"] = issueStats ctx.Data["ViewType"] = viewType ctx.Data["SortType"] = sortType @@ -367,6 +365,16 @@ func Issues(ctx *context.Context) { ctx.Data["State"] = "open" } + pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5) + pager.AddParam(ctx, "type", "ViewType") + pager.AddParam(ctx, "repo", "RepoID") + pager.AddParam(ctx, "sort", "SortType") + pager.AddParam(ctx, "state", "State") + pager.AddParam(ctx, "labels", "SelectLabels") + pager.AddParam(ctx, "milestone", "MilestoneID") + pager.AddParam(ctx, "assignee", "AssigneeID") + ctx.Data["Page"] = pager + ctx.HTML(200, tplIssues) } @@ -534,10 +542,13 @@ func showOrgProfile(ctx *context.Context) { ctx.Data["Repos"] = repos ctx.Data["Total"] = count - ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5) ctx.Data["Members"] = org.Members ctx.Data["Teams"] = org.Teams + pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + ctx.HTML(200, tplOrgHome) } diff --git a/routers/user/notification.go b/routers/user/notification.go index 32cacdd2c4e..8c23d76fa9e 100644 --- a/routers/user/notification.go +++ b/routers/user/notification.go @@ -1,3 +1,7 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package user import ( @@ -6,8 +10,6 @@ import ( "strconv" "strings" - "github.com/Unknwon/paginater" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -80,7 +82,11 @@ func Notifications(c *context.Context) { c.Data["Keyword"] = keyword c.Data["Status"] = status c.Data["Notifications"] = notifications - c.Data["Page"] = paginater.New(int(total), perPage, page, 5) + + pager := context.NewPagination(int(total), perPage, page, 5) + pager.SetDefaultParams(c) + c.Data["Page"] = pager + c.HTML(200, tplNotification) } diff --git a/routers/user/profile.go b/routers/user/profile.go index 675c1dc3f43..a7dab18c7a1 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -10,8 +10,6 @@ import ( "path" "strings" - "github.com/Unknwon/paginater" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -127,6 +125,7 @@ func Profile(ctx *context.Context) { var ( repos []*models.Repository count int64 + total int orderBy models.SearchOrderBy ) @@ -201,18 +200,14 @@ func Profile(ctx *context.Context) { } } - ctx.Data["Repos"] = repos - ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5) - ctx.Data["Total"] = count + total = int(count) default: if len(keyword) == 0 { - var total int repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String()) if err != nil { ctx.ServerError("GetRepositories", err) return } - ctx.Data["Repos"] = repos if showPrivate { total = ctxUser.NumRepos @@ -224,9 +219,6 @@ func Profile(ctx *context.Context) { } total = int(count) } - - ctx.Data["Page"] = paginater.New(total, setting.UI.User.RepoPagingNum, page, 5) - ctx.Data["Total"] = total } else { repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ Keyword: keyword, @@ -244,11 +236,15 @@ func Profile(ctx *context.Context) { return } - ctx.Data["Repos"] = repos - ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5) - ctx.Data["Total"] = count + total = int(count) } } + ctx.Data["Repos"] = repos + ctx.Data["Total"] = total + + pager := context.NewPagination(total, setting.UI.User.RepoPagingNum, page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager ctx.Data["ShowUserEmail"] = len(ctxUser.Email) > 0 && ctx.IsSigned && (!ctxUser.KeepEmailPrivate || ctxUser.ID == ctx.User.ID) diff --git a/templates/admin/notice.tmpl b/templates/admin/notice.tmpl index b1d886be42c..972771b4319 100644 --- a/templates/admin/notice.tmpl +++ b/templates/admin/notice.tmpl @@ -66,29 +66,7 @@ - {{with .Page}} - {{if gt .TotalPages 1}} -
- {{end}} - {{end}} + {{ template "base/paginate" . }} diff --git a/templates/admin/repo/list.tmpl b/templates/admin/repo/list.tmpl index 423de6a3d8c..793d9c620eb 100644 --- a/templates/admin/repo/list.tmpl +++ b/templates/admin/repo/list.tmpl @@ -44,7 +44,7 @@