mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.8 KiB
58 lines
1.8 KiB
6 years ago
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
6 years ago
|
|
||
|
package context
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"html/template"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/modules/paginator"
|
||
6 years ago
|
)
|
||
|
|
||
3 years ago
|
// Pagination provides a pagination via paginator.Paginator and additional configurations for the link params used in rendering
|
||
6 years ago
|
type Pagination struct {
|
||
3 years ago
|
Paginater *paginator.Paginator
|
||
6 years ago
|
urlParams []string
|
||
|
}
|
||
|
|
||
2 years ago
|
// NewPagination creates a new instance of the Pagination struct.
|
||
|
// "pagingNum" is "page size" or "limit", "current" is "page"
|
||
|
func NewPagination(total, pagingNum, current, numPages int) *Pagination {
|
||
6 years ago
|
p := &Pagination{}
|
||
2 years ago
|
p.Paginater = paginator.New(total, pagingNum, current, numPages)
|
||
6 years ago
|
return p
|
||
|
}
|
||
|
|
||
|
// AddParam adds a value from context identified by ctxKey as link param under a given paramKey
|
||
3 years ago
|
func (p *Pagination) AddParam(ctx *Context, paramKey, ctxKey string) {
|
||
6 years ago
|
_, exists := ctx.Data[ctxKey]
|
||
|
if !exists {
|
||
|
return
|
||
|
}
|
||
1 year ago
|
paramData := fmt.Sprintf("%v", ctx.Data[ctxKey]) // cast any to string
|
||
6 years ago
|
urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(paramKey), url.QueryEscape(paramData))
|
||
|
p.urlParams = append(p.urlParams, urlParam)
|
||
|
}
|
||
|
|
||
4 years ago
|
// AddParamString adds a string parameter directly
|
||
3 years ago
|
func (p *Pagination) AddParamString(key, value string) {
|
||
4 years ago
|
urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(value))
|
||
|
p.urlParams = append(p.urlParams, urlParam)
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetParams returns the configured URL params
|
||
|
func (p *Pagination) GetParams() template.URL {
|
||
6 years ago
|
return template.URL(strings.Join(p.urlParams, "&"))
|
||
6 years ago
|
}
|
||
|
|
||
|
// 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")
|
||
3 years ago
|
// do not add any more uncommon params here!
|
||
4 years ago
|
p.AddParam(ctx, "t", "queryType")
|
||
6 years ago
|
}
|