@ -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") | |||||
} |
@ -1,22 +1,23 @@ | |||||
{{with .Page}} | |||||
{{$paginationLink := .Page.GetParams}} | |||||
{{with .Page.Paginater}} | |||||
{{if gt .TotalPages 1}} | {{if gt .TotalPages 1}} | ||||
<div class="center page buttons"> | <div class="center page buttons"> | ||||
<div class="ui borderless pagination menu"> | <div class="ui borderless pagination menu"> | ||||
<a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$.Link}}?sort={{$.SortType}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}><i class="angle double left icon"></i><span class="navigation_label"> {{$.i18n.Tr "admin.first_page"}}</span></a> | |||||
<a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$.Link}}?sort={{$.SortType}}&page={{.Previous}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}> | |||||
<a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$.Link}}{{if $paginationLink}}?{{$paginationLink}}{{end}}"{{end}}><i class="angle double left icon"></i><span class="navigation_label"> {{$.i18n.Tr "admin.first_page"}}</span></a> | |||||
<a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$.Link}}?page={{.Previous}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}> | |||||
<i class="left arrow icon"></i><span class="navigation_label"> {{$.i18n.Tr "repo.issues.previous"}}</span> | <i class="left arrow icon"></i><span class="navigation_label"> {{$.i18n.Tr "repo.issues.previous"}}</span> | ||||
</a> | </a> | ||||
{{range .Pages}} | {{range .Pages}} | ||||
{{if eq .Num -1}} | {{if eq .Num -1}} | ||||
<a class="disabled item">...</a> | <a class="disabled item">...</a> | ||||
{{else}} | {{else}} | ||||
<a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?sort={{$.SortType}}&page={{.Num}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}>{{.Num}}</a> | |||||
<a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?page={{.Num}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}>{{.Num}}</a> | |||||
{{end}} | {{end}} | ||||
{{end}} | {{end}} | ||||
<a class="{{if not .HasNext}}disabled{{end}} item navigation" {{if .HasNext}}href="{{$.Link}}?sort={{$.SortType}}&page={{.Next}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}> | |||||
<a class="{{if not .HasNext}}disabled{{end}} item navigation" {{if .HasNext}}href="{{$.Link}}?page={{.Next}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}> | |||||
<span class="navigation_label">{{$.i18n.Tr "repo.issues.next"}} </span><i class="icon right arrow"></i> | <span class="navigation_label">{{$.i18n.Tr "repo.issues.next"}} </span><i class="icon right arrow"></i> | ||||
</a> | </a> | ||||
<a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$.Link}}?sort={{$.SortType}}&page={{.TotalPages}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}><span class="navigation_label">{{$.i18n.Tr "admin.last_page"}} </span><i class="angle double right icon"></i></a> | |||||
<a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$.Link}}?page={{.TotalPages}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}><span class="navigation_label">{{$.i18n.Tr "admin.last_page"}} </span><i class="angle double right icon"></i></a> | |||||
</div> | </div> | ||||
</div> | </div> | ||||
{{end}} | {{end}} | ||||