2024-05-12 18:13:00 +07:00
|
|
|
package components
|
|
|
|
|
|
|
|
import "github.com/tigorlazuardi/redmage/views"
|
|
|
|
import "strconv"
|
|
|
|
import "github.com/tigorlazuardi/redmage/views/utils"
|
|
|
|
import "strings"
|
2024-05-12 22:45:41 +07:00
|
|
|
import "fmt"
|
2024-05-12 18:13:00 +07:00
|
|
|
|
|
|
|
type PaginationData struct {
|
2024-05-13 10:37:55 +07:00
|
|
|
Offset int64
|
|
|
|
Limit int64
|
|
|
|
BaseURL string
|
|
|
|
Total int64
|
|
|
|
Classes []string
|
|
|
|
UnhideRange int
|
|
|
|
MobileUnhideRange int
|
2024-05-12 22:45:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
type pageStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
pageStatusShow = iota
|
|
|
|
pageStatusHidden
|
|
|
|
pageStatusDot
|
|
|
|
)
|
|
|
|
|
|
|
|
func (pgdata PaginationData) GetUnhideRange() int {
|
|
|
|
if pgdata.UnhideRange < 1 {
|
2024-05-13 10:37:55 +07:00
|
|
|
return 2
|
2024-05-12 22:45:41 +07:00
|
|
|
}
|
|
|
|
return pgdata.UnhideRange
|
|
|
|
}
|
|
|
|
|
2024-05-13 10:37:55 +07:00
|
|
|
func (pgdata PaginationData) GetMobileUnhideRange() int {
|
|
|
|
if pgdata.MobileUnhideRange < 1 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return pgdata.MobileUnhideRange
|
|
|
|
}
|
|
|
|
|
2024-05-12 22:45:41 +07:00
|
|
|
func (pgdata PaginationData) getPageStatus(page int) pageStatus {
|
|
|
|
if page < 2 {
|
|
|
|
return pageStatusShow
|
|
|
|
}
|
|
|
|
lastPage := (pgdata.Total / max(pgdata.Limit, 1)) + 1
|
|
|
|
if page == int(lastPage) {
|
|
|
|
return pageStatusShow
|
|
|
|
}
|
|
|
|
current := pgdata.GetCurrentPage()
|
|
|
|
unhideRange := pgdata.GetUnhideRange()
|
|
|
|
if page == current-unhideRange-1 {
|
|
|
|
return pageStatusDot
|
|
|
|
}
|
|
|
|
if page == current+unhideRange+1 {
|
|
|
|
return pageStatusDot
|
|
|
|
}
|
|
|
|
if page >= current-unhideRange && page <= current+unhideRange {
|
|
|
|
return pageStatusShow
|
|
|
|
}
|
|
|
|
|
|
|
|
return pageStatusHidden
|
|
|
|
}
|
|
|
|
|
2024-05-13 10:37:55 +07:00
|
|
|
func (pgdata PaginationData) getMobilePageStatus(page int) pageStatus {
|
|
|
|
if page < 2 {
|
|
|
|
return pageStatusShow
|
|
|
|
}
|
|
|
|
lastPage := (pgdata.Total / max(pgdata.Limit, 1)) + 1
|
|
|
|
if page == int(lastPage) {
|
|
|
|
return pageStatusShow
|
|
|
|
}
|
|
|
|
current := pgdata.GetCurrentPage()
|
|
|
|
unhideRange := pgdata.GetMobileUnhideRange()
|
|
|
|
if page == current-unhideRange-1 {
|
|
|
|
return pageStatusDot
|
|
|
|
}
|
|
|
|
if page == current+unhideRange+1 {
|
|
|
|
return pageStatusDot
|
|
|
|
}
|
|
|
|
if page >= current-unhideRange && page <= current+unhideRange {
|
|
|
|
return pageStatusShow
|
|
|
|
}
|
|
|
|
|
|
|
|
return pageStatusHidden
|
|
|
|
}
|
|
|
|
|
2024-05-12 22:45:41 +07:00
|
|
|
func (pgdata PaginationData) GetCurrentPage() int {
|
|
|
|
return int(pgdata.Offset/pgdata.Limit) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pgdata PaginationData) GetTotalPage() int {
|
|
|
|
return int(pgdata.Total/max(pgdata.Limit, 1)) + 1
|
2024-05-12 18:13:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
templ Pagination(c *views.Context, data PaginationData) {
|
|
|
|
if data.Total > data.Limit {
|
2024-05-12 22:45:41 +07:00
|
|
|
<div
|
|
|
|
x-data={ fmt.Sprintf(`{
|
|
|
|
page: %d,
|
|
|
|
qs: %s,
|
|
|
|
max: %d,
|
|
|
|
get vals() { return JSON.stringify({...this.qs, offset: (Math.max(1, Math.min(this.page, this.max))-1) * (this.qs.limit || 25)}) }
|
|
|
|
}`, data.GetCurrentPage(), c.JSONQuery(), data.GetTotalPage()) }
|
|
|
|
>
|
2024-05-13 10:37:55 +07:00
|
|
|
<div class="hidden sm:join">
|
2024-05-12 22:45:41 +07:00
|
|
|
for i, count := 1, int64(0); count < data.Total; i, count = i+1, count+data.Limit {
|
2024-05-12 22:51:48 +07:00
|
|
|
if data.GetTotalPage() <= 5 || data.getPageStatus(i) == pageStatusShow {
|
2024-05-12 22:45:41 +07:00
|
|
|
<a
|
|
|
|
href={ c.URLWithExtraQuery(data.BaseURL, "offset", strconv.FormatInt(count, 10)) }
|
|
|
|
class={ utils.CXX(
|
2024-05-12 18:13:00 +07:00
|
|
|
"join-item btn no-underline", true,
|
|
|
|
"btn-active", data.Offset <= count && data.Offset > count - data.Limit ,
|
|
|
|
strings.Join(data.Classes, " "), len(data.Classes) > 0,
|
|
|
|
) }
|
2024-05-12 22:45:41 +07:00
|
|
|
>{ strconv.Itoa(i) }</a>
|
|
|
|
} else if data.getPageStatus(i) == pageStatusDot {
|
|
|
|
<span @click="$refs.dialog.showModal()" class="join-item btn">...</span>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</div>
|
2024-05-13 10:37:55 +07:00
|
|
|
<div class="join sm:hidden">
|
|
|
|
for i, count := 1, int64(0); count < data.Total; i, count = i+1, count+data.Limit {
|
|
|
|
if data.GetTotalPage() <= 5 || data.getMobilePageStatus(i) == pageStatusShow {
|
|
|
|
<a
|
|
|
|
href={ c.URLWithExtraQuery(data.BaseURL, "offset", strconv.FormatInt(count, 10)) }
|
|
|
|
class={ utils.CXX(
|
|
|
|
"join-item btn no-underline btn-sm", true,
|
|
|
|
"btn-active", data.Offset <= count && data.Offset > count - data.Limit ,
|
|
|
|
strings.Join(data.Classes, " "), len(data.Classes) > 0,
|
|
|
|
) }
|
|
|
|
>{ strconv.Itoa(i) }</a>
|
|
|
|
} else if data.getMobilePageStatus(i) == pageStatusDot {
|
|
|
|
<span @click="$refs.dialog.showModal()" class="join-item btn btn-sm">...</span>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</div>
|
2024-05-12 22:45:41 +07:00
|
|
|
<dialog
|
2024-05-13 15:52:41 +07:00
|
|
|
onclick="if (event.target === this && window.matchMedia('(max-width: 600px)').matches) {
|
|
|
|
this.close()
|
|
|
|
}"
|
2024-05-12 22:45:41 +07:00
|
|
|
x-ref="dialog"
|
|
|
|
class="modal"
|
|
|
|
>
|
|
|
|
<div class="modal-box">
|
|
|
|
<h2 class="my-2">Seek Page</h2>
|
|
|
|
<div
|
|
|
|
id="page-seeker"
|
|
|
|
class="join flex"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="number"
|
|
|
|
value={ strconv.Itoa(data.GetCurrentPage()) }
|
|
|
|
min="1"
|
|
|
|
max={ strconv.Itoa(data.GetTotalPage()) }
|
|
|
|
placeholder="Seek Page"
|
|
|
|
class="input input-bordered join-item w-full"
|
|
|
|
x-model="page"
|
|
|
|
hx-get={ c.Request.URL.Path }
|
|
|
|
hx-target="main"
|
|
|
|
hx-select="main"
|
|
|
|
hx-swap="outerHTML"
|
|
|
|
hx-push-url="true"
|
|
|
|
hx-trigger="custom"
|
|
|
|
@keyup.enter="htmx.trigger($el, 'custom')"
|
|
|
|
:hx-vals="vals"
|
2024-05-13 15:52:41 +07:00
|
|
|
onfocus="this.select()"
|
2024-05-12 22:45:41 +07:00
|
|
|
/>
|
|
|
|
<button
|
|
|
|
hx-get={ c.Request.URL.Path }
|
|
|
|
hx-target="main"
|
|
|
|
hx-select="main"
|
|
|
|
hx-swap="outerHTML"
|
|
|
|
hx-push-url="true"
|
|
|
|
:hx-vals="vals"
|
|
|
|
hx-trigger="click"
|
|
|
|
type="button"
|
|
|
|
class="btn btn-primary join-item"
|
|
|
|
>Go</button>
|
|
|
|
</div>
|
2024-05-13 15:52:41 +07:00
|
|
|
<div class="mt-4 grid justify-center">
|
|
|
|
<button type="button" @click="$refs.dialog.close()" class="btn btn-secondary mx-auto">Cancel</button>
|
|
|
|
</div>
|
2024-05-12 22:45:41 +07:00
|
|
|
</div>
|
|
|
|
</dialog>
|
2024-05-12 18:13:00 +07:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|