2023-01-18 15:04:12 +00:00
|
|
|
package pagination
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Elements interface {
|
2023-01-28 15:12:46 +00:00
|
|
|
Current(page, totalPage int) string
|
2023-01-18 15:04:12 +00:00
|
|
|
Prev(url string) string
|
|
|
|
Next(url string) string
|
|
|
|
Dots() string
|
|
|
|
Middle(page int, url string) string
|
2023-01-19 14:02:45 +00:00
|
|
|
Url(path, query string, page int) string
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ParsePagination struct {
|
|
|
|
Elements
|
|
|
|
TotalPage int
|
|
|
|
TotalRaw int
|
|
|
|
PageSize int
|
|
|
|
CurrentPage int
|
|
|
|
Query string
|
|
|
|
Path string
|
|
|
|
Step int
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:02:45 +00:00
|
|
|
func NewParsePagination(totalRaw int, pageSize int, currentPage, step int, query string, path string) ParsePagination {
|
2023-01-18 15:04:12 +00:00
|
|
|
allPage := int(math.Ceil(float64(totalRaw) / float64(pageSize)))
|
|
|
|
return ParsePagination{TotalPage: allPage, TotalRaw: totalRaw, PageSize: pageSize, CurrentPage: currentPage, Query: query, Path: path, Step: step}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Paginate(e Elements, p ParsePagination) string {
|
|
|
|
p.Elements = e
|
|
|
|
return p.ToHtml()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p ParsePagination) ToHtml() (html string) {
|
|
|
|
if p.TotalRaw < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s := strings.Builder{}
|
|
|
|
if p.CurrentPage > p.TotalPage {
|
|
|
|
p.CurrentPage = p.TotalPage
|
|
|
|
}
|
|
|
|
start := p.CurrentPage - p.Step
|
|
|
|
end := p.CurrentPage + p.Step
|
|
|
|
if start < 1 {
|
|
|
|
start = 1
|
|
|
|
}
|
|
|
|
if p.CurrentPage > 1 {
|
2023-01-19 14:02:45 +00:00
|
|
|
s.WriteString(p.Prev(p.Url(p.Path, p.Query, p.CurrentPage-1)))
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
if p.CurrentPage >= p.Step+2 {
|
|
|
|
d := false
|
|
|
|
if p.CurrentPage > p.Step+2 {
|
|
|
|
d = true
|
|
|
|
}
|
2023-01-19 14:02:45 +00:00
|
|
|
s.WriteString(p.Middle(1, p.Url(p.Path, p.Query, 1)))
|
2023-01-18 15:04:12 +00:00
|
|
|
if d {
|
|
|
|
s.WriteString(p.Dots())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.TotalPage < end {
|
|
|
|
end = p.TotalPage
|
|
|
|
}
|
|
|
|
|
|
|
|
for page := start; page <= end; page++ {
|
|
|
|
h := ""
|
|
|
|
if p.CurrentPage == page {
|
2023-01-28 15:12:46 +00:00
|
|
|
h = p.Current(page, p.TotalPage)
|
2023-01-18 15:04:12 +00:00
|
|
|
} else {
|
2023-01-19 14:02:45 +00:00
|
|
|
h = p.Middle(page, p.Url(p.Path, p.Query, page))
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
s.WriteString(h)
|
|
|
|
|
|
|
|
}
|
|
|
|
if p.TotalPage >= p.CurrentPage+p.Step+1 {
|
|
|
|
d := false
|
|
|
|
if p.TotalPage > p.CurrentPage+p.Step+1 {
|
|
|
|
d = true
|
|
|
|
}
|
|
|
|
if d {
|
|
|
|
s.WriteString(p.Dots())
|
|
|
|
}
|
2023-01-19 14:02:45 +00:00
|
|
|
s.WriteString(p.Middle(p.TotalPage, p.Url(p.Path, p.Query, p.TotalPage)))
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
if p.CurrentPage < p.TotalPage {
|
2023-01-19 14:02:45 +00:00
|
|
|
s.WriteString(p.Next(p.Url(p.Path, p.Query, p.CurrentPage+1)))
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
html = s.String()
|
|
|
|
return
|
|
|
|
}
|