From fef3f2e7419b46ef784a0f3136a41cbf83a37641 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 22 Mar 2025 21:15:11 -0700 Subject: [PATCH 1/6] Don't display pagination on user dashboard by default but with prev and next buttons --- custom/conf/app.example.ini | 3 +++ modules/setting/ui.go | 15 +++++++++------ routers/web/user/home.go | 1 + services/feed/feed.go | 5 +++++ templates/base/pre_next.tmpl | 17 +++++++++++++++++ templates/user/dashboard/feeds.tmpl | 9 ++++++++- 6 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 templates/base/pre_next.tmpl diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 0fc49accef84e..c38f95b96cc46 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -1357,6 +1357,9 @@ LEVEL = Info ;; Number of orgs that are displayed on profile page ;ORG_PAGING_NUM = 15 +;; Whether to show the pagination on the user dashbaord page +;DASHBOARD_ACTIVITIES_PAGINATION = false + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;[ui.meta] diff --git a/modules/setting/ui.go b/modules/setting/ui.go index 3d9c916bf7f72..60d80005b0297 100644 --- a/modules/setting/ui.go +++ b/modules/setting/ui.go @@ -63,8 +63,9 @@ var UI = struct { OrgPagingNum int } `ini:"ui.admin"` User struct { - RepoPagingNum int - OrgPagingNum int + RepoPagingNum int + OrgPagingNum int + DashboardActivitiesPagination bool // display pagination for dashboard activities } `ini:"ui.user"` Meta struct { Author string @@ -129,11 +130,13 @@ var UI = struct { OrgPagingNum: 50, }, User: struct { - RepoPagingNum int - OrgPagingNum int + RepoPagingNum int + OrgPagingNum int + DashboardActivitiesPagination bool }{ - RepoPagingNum: 15, - OrgPagingNum: 15, + RepoPagingNum: 15, + OrgPagingNum: 15, + DashboardActivitiesPagination: false, }, Meta: struct { Author string diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 864a2831d1fbb..16ba5fed08565 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -142,6 +142,7 @@ func Dashboard(ctx *context.Context) { pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5) pager.AddParamFromRequest(ctx.Req) ctx.Data["Page"] = pager + ctx.Data["ShowPagination"] = setting.UI.User.DashboardActivitiesPagination ctx.HTML(http.StatusOK, tplDashboard) } diff --git a/services/feed/feed.go b/services/feed/feed.go index 41a918f00e3fd..ea582e7e5a539 100644 --- a/services/feed/feed.go +++ b/services/feed/feed.go @@ -6,6 +6,7 @@ package feed import ( "context" "fmt" + "math" activities_model "code.gitea.io/gitea/models/activities" "code.gitea.io/gitea/models/db" @@ -27,6 +28,10 @@ func GetFeedsForDashboard(ctx context.Context, opts activities_model.GetFeedsOpt if err != nil { return nil, 0, err } + if !setting.UI.User.DashboardActivitiesPagination { + return results, math.MaxInt32, nil + } + if opts.DontCount { cnt, err = cache.GetInt64(userFeedCacheKey(opts.Actor.ID), func() (int64, error) { return activities_model.CountUserFeeds(ctx, opts.Actor.ID) diff --git a/templates/base/pre_next.tmpl b/templates/base/pre_next.tmpl new file mode 100644 index 0000000000000..75f07f75b4fea --- /dev/null +++ b/templates/base/pre_next.tmpl @@ -0,0 +1,17 @@ +{{$paginationParams := .Page.GetParams}} +{{$paginationLink := $.Link}} +{{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}} +{{with .Page.Paginater}} +
+ +
+{{end}} diff --git a/templates/user/dashboard/feeds.tmpl b/templates/user/dashboard/feeds.tmpl index 47686dd442931..8f316b5af9d53 100644 --- a/templates/user/dashboard/feeds.tmpl +++ b/templates/user/dashboard/feeds.tmpl @@ -126,5 +126,12 @@ {{end}} - {{template "base/paginate" .}} + {{$paginater := .Page.Paginater}} + {{if $paginater}} + {{if .ShowPagination}} + {{template "base/paginate" .}} + {{else}} + {{template "base/pre_next" .}} + {{end}} + {{end}} From cd015d90c9376d073ae9d89cdba1707e2b551441 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 23 Mar 2025 15:33:52 +0800 Subject: [PATCH 2/6] revert --- custom/conf/app.example.ini | 3 --- modules/setting/ui.go | 15 ++++++--------- routers/web/user/home.go | 1 - templates/base/pre_next.tmpl | 17 ----------------- templates/user/dashboard/feeds.tmpl | 9 +-------- 5 files changed, 7 insertions(+), 38 deletions(-) delete mode 100644 templates/base/pre_next.tmpl diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index c38f95b96cc46..0fc49accef84e 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -1357,9 +1357,6 @@ LEVEL = Info ;; Number of orgs that are displayed on profile page ;ORG_PAGING_NUM = 15 -;; Whether to show the pagination on the user dashbaord page -;DASHBOARD_ACTIVITIES_PAGINATION = false - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;[ui.meta] diff --git a/modules/setting/ui.go b/modules/setting/ui.go index 60d80005b0297..3d9c916bf7f72 100644 --- a/modules/setting/ui.go +++ b/modules/setting/ui.go @@ -63,9 +63,8 @@ var UI = struct { OrgPagingNum int } `ini:"ui.admin"` User struct { - RepoPagingNum int - OrgPagingNum int - DashboardActivitiesPagination bool // display pagination for dashboard activities + RepoPagingNum int + OrgPagingNum int } `ini:"ui.user"` Meta struct { Author string @@ -130,13 +129,11 @@ var UI = struct { OrgPagingNum: 50, }, User: struct { - RepoPagingNum int - OrgPagingNum int - DashboardActivitiesPagination bool + RepoPagingNum int + OrgPagingNum int }{ - RepoPagingNum: 15, - OrgPagingNum: 15, - DashboardActivitiesPagination: false, + RepoPagingNum: 15, + OrgPagingNum: 15, }, Meta: struct { Author string diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 16ba5fed08565..864a2831d1fbb 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -142,7 +142,6 @@ func Dashboard(ctx *context.Context) { pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5) pager.AddParamFromRequest(ctx.Req) ctx.Data["Page"] = pager - ctx.Data["ShowPagination"] = setting.UI.User.DashboardActivitiesPagination ctx.HTML(http.StatusOK, tplDashboard) } diff --git a/templates/base/pre_next.tmpl b/templates/base/pre_next.tmpl deleted file mode 100644 index 75f07f75b4fea..0000000000000 --- a/templates/base/pre_next.tmpl +++ /dev/null @@ -1,17 +0,0 @@ -{{$paginationParams := .Page.GetParams}} -{{$paginationLink := $.Link}} -{{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}} -{{with .Page.Paginater}} - -{{end}} diff --git a/templates/user/dashboard/feeds.tmpl b/templates/user/dashboard/feeds.tmpl index 8f316b5af9d53..47686dd442931 100644 --- a/templates/user/dashboard/feeds.tmpl +++ b/templates/user/dashboard/feeds.tmpl @@ -126,12 +126,5 @@ {{end}} - {{$paginater := .Page.Paginater}} - {{if $paginater}} - {{if .ShowPagination}} - {{template "base/paginate" .}} - {{else}} - {{template "base/pre_next" .}} - {{end}} - {{end}} + {{template "base/paginate" .}} From ea0223eaa11c8911757ef96e4c219c9f15df8fea Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 23 Mar 2025 15:40:06 +0800 Subject: [PATCH 3/6] fix --- routers/web/user/home.go | 2 +- services/context/pagination.go | 1 + services/feed/feed.go | 18 +++--------------- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 864a2831d1fbb..e4f9937b4bcad 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -139,7 +139,7 @@ func Dashboard(ctx *context.Context) { ctx.Data["Feeds"] = feeds - pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5) + pager := context.NewPagination(count, setting.UI.FeedPagingNum, page, 5) pager.AddParamFromRequest(ctx.Req) ctx.Data["Page"] = pager diff --git a/services/context/pagination.go b/services/context/pagination.go index d33dd217d0eaa..49cd37e36b4d4 100644 --- a/services/context/pagination.go +++ b/services/context/pagination.go @@ -21,6 +21,7 @@ type Pagination struct { // NewPagination creates a new instance of the Pagination struct. // "pagingNum" is "page size" or "limit", "current" is "page" +// total=-1 means only showing prev/next func NewPagination(total, pagingNum, current, numPages int) *Pagination { p := &Pagination{} p.Paginater = paginator.New(total, pagingNum, current, numPages) diff --git a/services/feed/feed.go b/services/feed/feed.go index ea582e7e5a539..214e9b5765302 100644 --- a/services/feed/feed.go +++ b/services/feed/feed.go @@ -6,7 +6,6 @@ package feed import ( "context" "fmt" - "math" activities_model "code.gitea.io/gitea/models/activities" "code.gitea.io/gitea/models/db" @@ -16,28 +15,17 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" ) func userFeedCacheKey(userID int64) string { return fmt.Sprintf("user_feed_%d", userID) } -func GetFeedsForDashboard(ctx context.Context, opts activities_model.GetFeedsOptions) (activities_model.ActionList, int64, error) { +func GetFeedsForDashboard(ctx context.Context, opts activities_model.GetFeedsOptions) (activities_model.ActionList, int, error) { opts.DontCount = opts.RequestedTeam == nil && opts.Date == "" results, cnt, err := activities_model.GetFeeds(ctx, opts) - if err != nil { - return nil, 0, err - } - if !setting.UI.User.DashboardActivitiesPagination { - return results, math.MaxInt32, nil - } - - if opts.DontCount { - cnt, err = cache.GetInt64(userFeedCacheKey(opts.Actor.ID), func() (int64, error) { - return activities_model.CountUserFeeds(ctx, opts.Actor.ID) - }) - } - return results, cnt, err + return results, util.Iif(opts.DontCount, -1, int(cnt)), err } // GetFeeds returns actions according to the provided options From 69aa4787b756695ba7ff45d1199430bfebc8738a Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 23 Mar 2025 16:01:43 +0800 Subject: [PATCH 4/6] fix --- modules/paginator/paginator.go | 45 ++++++++++++----------------- modules/paginator/paginator_test.go | 4 +-- templates/base/paginate.tmpl | 9 +++++- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/modules/paginator/paginator.go b/modules/paginator/paginator.go index 8258d194c263f..0e7040a029b31 100644 --- a/modules/paginator/paginator.go +++ b/modules/paginator/paginator.go @@ -4,6 +4,8 @@ package paginator +import "code.gitea.io/gitea/modules/util" + /* In template: @@ -32,25 +34,22 @@ Output: // Paginator represents a set of results of pagination calculations. type Paginator struct { - total int // total rows count - pagingNum int // how many rows in one page - current int // current page number - numPages int // how many pages to show on the UI + total int // total rows count, -1 means unknown + totalPages int // total pages count, -1 means unknown + pagingNum int // how many rows in one page + current int // current page number + numPages int // how many pages to show on the UI } // New initialize a new pagination calculation and returns a Paginator as result. func New(total, pagingNum, current, numPages int) *Paginator { - if pagingNum <= 0 { - pagingNum = 1 - } - if current <= 0 { - current = 1 - } - p := &Paginator{total, pagingNum, current, numPages} - if p.current > p.TotalPages() { - p.current = p.TotalPages() + pagingNum = max(pagingNum, 1) + totalPages := util.Iif(total == -1, -1, (total+pagingNum-1)/pagingNum) + if total >= 0 { + current = min(current, totalPages) } - return p + current = max(current, 1) + return &Paginator{total, totalPages, pagingNum, current, numPages} } // IsFirst returns true if current page is the first page. @@ -72,7 +71,7 @@ func (p *Paginator) Previous() int { // HasNext returns true if there is a next page relative to current page. func (p *Paginator) HasNext() bool { - return p.total > p.current*p.pagingNum + return p.total == -1 || p.current*p.pagingNum < p.total } func (p *Paginator) Next() int { @@ -84,10 +83,7 @@ func (p *Paginator) Next() int { // IsLast returns true if current page is the last page. func (p *Paginator) IsLast() bool { - if p.total == 0 { - return true - } - return p.total > (p.current-1)*p.pagingNum && !p.HasNext() + return !p.HasNext() } // Total returns number of total rows. @@ -97,10 +93,7 @@ func (p *Paginator) Total() int { // TotalPages returns number of total pages. func (p *Paginator) TotalPages() int { - if p.total == 0 { - return 1 - } - return (p.total + p.pagingNum - 1) / p.pagingNum + return p.totalPages } // Current returns current page number. @@ -135,10 +128,10 @@ func getMiddleIdx(numPages int) int { // If value is -1 means "..." that more pages are not showing. func (p *Paginator) Pages() []*Page { if p.numPages == 0 { - return []*Page{} - } else if p.numPages == 1 && p.TotalPages() == 1 { + return nil + } else if p.total == -1 || (p.numPages == 1 && p.TotalPages() == 1) { // Only show current page. - return []*Page{{1, true}} + return []*Page{{p.current, true}} } // Total page number is less or equal. diff --git a/modules/paginator/paginator_test.go b/modules/paginator/paginator_test.go index 8a56ee5121e83..ed46ecea94ce0 100644 --- a/modules/paginator/paginator_test.go +++ b/modules/paginator/paginator_test.go @@ -76,9 +76,7 @@ func TestPaginator(t *testing.T) { t.Run("Only current page", func(t *testing.T) { p := New(0, 10, 1, 1) pages := p.Pages() - assert.Len(t, pages, 1) - assert.Equal(t, 1, pages[0].Num()) - assert.True(t, pages[0].IsCurrent()) + assert.Empty(t, pages) // no "total", so no pages p = New(1, 10, 1, 1) pages = p.Pages() diff --git a/templates/base/paginate.tmpl b/templates/base/paginate.tmpl index 253892c009231..8e7bb9497a98b 100644 --- a/templates/base/paginate.tmpl +++ b/templates/base/paginate.tmpl @@ -2,13 +2,17 @@ {{$paginationLink := $.Link}} {{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}} {{with .Page.Paginater}} - {{if gt .TotalPages 1}} + {{if .TotalPages}} + {{$showFirstLast := gt .TotalPages 0}} {{end}} From 96cbabbef893397764c2deb22813dd93f267e012 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 24 Mar 2025 02:55:16 +0800 Subject: [PATCH 5/6] do not highlight the current page if there is only one page --- templates/base/paginate.tmpl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/templates/base/paginate.tmpl b/templates/base/paginate.tmpl index 8e7bb9497a98b..f6c1785ccf0b1 100644 --- a/templates/base/paginate.tmpl +++ b/templates/base/paginate.tmpl @@ -2,8 +2,8 @@ {{$paginationLink := $.Link}} {{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}} {{with .Page.Paginater}} - {{if .TotalPages}} - {{$showFirstLast := gt .TotalPages 0}} + {{if or (eq .TotalPages -1) (gt .TotalPages 1)}} + {{$showFirstLast := gt .TotalPages 1}}