2023-01-18 15:04:12 +00:00
|
|
|
package pagination
|
|
|
|
|
|
|
|
import (
|
2023-02-14 15:28:45 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
2023-12-23 12:17:42 +00:00
|
|
|
"net/url"
|
2023-01-18 15:04:12 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-12-23 12:17:42 +00:00
|
|
|
type Render interface {
|
2023-02-14 15:44:48 +00:00
|
|
|
Current(page, totalPage, totalRows 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-12-23 12:17:42 +00:00
|
|
|
Urls(u url.URL, page int, isTLS bool) string
|
|
|
|
Step() int
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 12:17:42 +00:00
|
|
|
type parser struct {
|
|
|
|
Render
|
2023-01-18 15:04:12 +00:00
|
|
|
TotalPage int
|
|
|
|
TotalRaw int
|
|
|
|
PageSize int
|
|
|
|
CurrentPage int
|
2023-12-23 12:17:42 +00:00
|
|
|
Url url.URL
|
2023-01-18 15:04:12 +00:00
|
|
|
Step int
|
2023-12-23 12:17:42 +00:00
|
|
|
IsTLS bool
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 12:17:42 +00:00
|
|
|
func Paginate(e Render, totalRaw int, pageSize int, currentPage, step int, u url.URL, isTLS bool) string {
|
|
|
|
st := e.Step()
|
|
|
|
if st > 0 {
|
|
|
|
step = st
|
|
|
|
}
|
|
|
|
return parser{
|
|
|
|
Render: e,
|
|
|
|
TotalPage: number.DivideCeil(totalRaw, pageSize),
|
|
|
|
TotalRaw: totalRaw,
|
|
|
|
PageSize: pageSize,
|
|
|
|
CurrentPage: currentPage,
|
|
|
|
Url: u,
|
|
|
|
Step: step,
|
|
|
|
IsTLS: isTLS,
|
|
|
|
}.ToHtml()
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 12:17:42 +00:00
|
|
|
func (p parser) ToHtml() (html string) {
|
2023-02-14 15:28:45 +00:00
|
|
|
if p.TotalPage < 2 {
|
2023-01-18 15:04:12 +00:00
|
|
|
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-12-23 12:17:42 +00:00
|
|
|
s.WriteString(p.Prev(p.Urls(p.Url, p.CurrentPage-1, p.IsTLS)))
|
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-12-23 12:17:42 +00:00
|
|
|
s.WriteString(p.Middle(1, p.Urls(p.Url, 1, p.IsTLS)))
|
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-02-14 15:44:48 +00:00
|
|
|
h = p.Current(page, p.TotalPage, p.TotalRaw)
|
2023-01-18 15:04:12 +00:00
|
|
|
} else {
|
2023-12-23 12:17:42 +00:00
|
|
|
h = p.Middle(page, p.Urls(p.Url, page, p.IsTLS))
|
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-12-23 12:17:42 +00:00
|
|
|
s.WriteString(p.Middle(p.TotalPage, p.Urls(p.Url, p.TotalPage, p.IsTLS)))
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
if p.CurrentPage < p.TotalPage {
|
2023-12-23 12:17:42 +00:00
|
|
|
s.WriteString(p.Next(p.Urls(p.Url, p.CurrentPage+1, p.IsTLS)))
|
2023-01-18 15:04:12 +00:00
|
|
|
}
|
|
|
|
html = s.String()
|
|
|
|
return
|
|
|
|
}
|