2022-08-13 06:11:36 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-08-13 22:39:31 +00:00
|
|
|
"math"
|
2022-08-13 06:11:36 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2022-08-31 05:13:57 +00:00
|
|
|
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2022-08-13 06:11:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pagination result object.
|
|
|
|
type Pagination struct {
|
2022-08-31 05:13:57 +00:00
|
|
|
Page int // provide <0 to mean "last page"
|
2022-08-13 06:11:36 +00:00
|
|
|
PerPage int
|
|
|
|
Total int64
|
|
|
|
Sort string
|
2022-08-31 05:13:57 +00:00
|
|
|
|
|
|
|
// privates
|
|
|
|
lastPage bool
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Page for Iter.
|
|
|
|
type Page struct {
|
|
|
|
Page int
|
|
|
|
IsCurrent bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the page from form or query parameters.
|
|
|
|
func (p *Pagination) ParsePage(r *http.Request) {
|
|
|
|
raw := r.FormValue("page")
|
|
|
|
a, err := strconv.Atoi(raw)
|
|
|
|
if err == nil {
|
2022-08-14 00:42:42 +00:00
|
|
|
if a <= 0 {
|
2022-08-31 05:13:57 +00:00
|
|
|
p.lastPage = true
|
2022-08-13 06:11:36 +00:00
|
|
|
a = 1
|
|
|
|
}
|
|
|
|
p.Page = a
|
2022-08-14 00:42:42 +00:00
|
|
|
} else {
|
|
|
|
p.Page = 1
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iter the pages, for templates.
|
|
|
|
func (p *Pagination) Iter() []Page {
|
2022-08-13 22:39:31 +00:00
|
|
|
var (
|
2022-08-31 05:13:57 +00:00
|
|
|
pages = []Page{}
|
|
|
|
pageIdx int
|
|
|
|
total = p.Pages()
|
2022-08-13 22:39:31 +00:00
|
|
|
)
|
|
|
|
for i := 1; i <= total; i++ {
|
2022-08-13 06:11:36 +00:00
|
|
|
pages = append(pages, Page{
|
|
|
|
Page: i,
|
|
|
|
IsCurrent: i == p.Page,
|
|
|
|
})
|
2022-08-31 05:13:57 +00:00
|
|
|
|
|
|
|
if i == p.Page {
|
|
|
|
pageIdx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we have A LOT of pages?
|
|
|
|
if len(pages) > config.PagerButtonLimit {
|
|
|
|
// We return a slide only N pages long. Where is our current page in the offset?
|
|
|
|
if pageIdx <= config.PagerButtonLimit/2 {
|
|
|
|
// We are near the front, return the first N pages.
|
|
|
|
return pages[:config.PagerButtonLimit+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we near the end?
|
|
|
|
if pageIdx > len(pages)-(config.PagerButtonLimit/2) {
|
|
|
|
// We are near the end, return the last N pages.
|
|
|
|
return pages[len(pages)-config.PagerButtonLimit-1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are somewhere in the middle.
|
|
|
|
var result = []Page{}
|
|
|
|
for i := pageIdx - (config.PagerButtonLimit / 2) - 1; i < pageIdx+(config.PagerButtonLimit/2); i++ {
|
|
|
|
if i >= 0 && i < len(pages) {
|
|
|
|
result = append(result, pages[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
2022-08-13 22:39:31 +00:00
|
|
|
func (p *Pagination) Pages() int {
|
2023-02-14 06:19:18 +00:00
|
|
|
if p.PerPage == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2022-08-13 22:39:31 +00:00
|
|
|
return int(math.Ceil(float64(p.Total) / float64(p.PerPage)))
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
func (p *Pagination) GetOffset() int {
|
2022-08-31 05:13:57 +00:00
|
|
|
// Are we looking for the FINAL page?
|
|
|
|
if p.lastPage && p.Pages() >= 1 {
|
|
|
|
p.Page = p.Pages()
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
return (p.Page - 1) * p.PerPage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pagination) HasNext() bool {
|
2022-08-13 22:39:31 +00:00
|
|
|
return p.Page < p.Pages()
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pagination) HasPrevious() bool {
|
|
|
|
return p.Page > 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pagination) Next() int {
|
2022-08-13 22:39:31 +00:00
|
|
|
if p.Page >= p.Pages() {
|
|
|
|
return p.Pages()
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
return p.Page + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pagination) Previous() int {
|
|
|
|
if p.Page > 1 {
|
|
|
|
return p.Page - 1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|