code.gitea.io/gitea@v1.22.3/services/context/pagination.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package context 5 6 import ( 7 "fmt" 8 "html/template" 9 "net/url" 10 "strings" 11 12 "code.gitea.io/gitea/modules/paginator" 13 ) 14 15 // Pagination provides a pagination via paginator.Paginator and additional configurations for the link params used in rendering 16 type Pagination struct { 17 Paginater *paginator.Paginator 18 urlParams []string 19 } 20 21 // NewPagination creates a new instance of the Pagination struct. 22 // "pagingNum" is "page size" or "limit", "current" is "page" 23 func NewPagination(total, pagingNum, current, numPages int) *Pagination { 24 p := &Pagination{} 25 p.Paginater = paginator.New(total, pagingNum, current, numPages) 26 return p 27 } 28 29 // AddParamString adds a string parameter directly 30 func (p *Pagination) AddParamString(key, value string) { 31 urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(value)) 32 p.urlParams = append(p.urlParams, urlParam) 33 } 34 35 // GetParams returns the configured URL params 36 func (p *Pagination) GetParams() template.URL { 37 return template.URL(strings.Join(p.urlParams, "&")) 38 } 39 40 // SetDefaultParams sets common pagination params that are often used 41 func (p *Pagination) SetDefaultParams(ctx *Context) { 42 if v, ok := ctx.Data["SortType"].(string); ok { 43 p.AddParamString("sort", v) 44 } 45 if v, ok := ctx.Data["Keyword"].(string); ok { 46 p.AddParamString("q", v) 47 } 48 if v, ok := ctx.Data["IsFuzzy"].(bool); ok { 49 p.AddParamString("fuzzy", fmt.Sprint(v)) 50 } 51 // do not add any more uncommon params here! 52 }