package components import "github.com/tigorlazuardi/redmage/views" import "strconv" import "github.com/tigorlazuardi/redmage/views/utils" import "strings" import "fmt" type PaginationData struct { Offset int64 Limit int64 BaseURL string Total int64 Classes []string UnhideRange int MobileUnhideRange int } type pageStatus int const ( pageStatusShow = iota pageStatusHidden pageStatusDot ) func (pgdata PaginationData) GetUnhideRange() int { if pgdata.UnhideRange < 1 { return 2 } return pgdata.UnhideRange } func (pgdata PaginationData) GetMobileUnhideRange() int { if pgdata.MobileUnhideRange < 1 { return 1 } return pgdata.MobileUnhideRange } 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 } 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 } func (pgdata PaginationData) GetCurrentPage() int { return int(pgdata.Offset/max(pgdata.Limit, 1)) + 1 } func (pgdata PaginationData) GetTotalPage() int { return int(pgdata.Total/max(pgdata.Limit, 1)) + 1 } templ Pagination(c *views.Context, data PaginationData) { if data.Total > data.Limit {