D3vugu-components/components/navigation/pagination.go
David Vogel 114a7d893c Add input field and a lot of other fixes
- Add input field and general data binding
- Rename SymbolSlot to IconSlot
- Remove d3c-color-accent
- Update icons page
- Add input example page
- Update button to support highlighting
- Update navigation entry to support highlighting
- Add d3c-button-only-icon to button and let sidebar use that
- Use d3c-button-transparent in sidebar
- Add focus indicator to buttons
- Rename CSS color variables and change definition of some
- Let pagination use input:Button
- Add some padding to the inline code component
2023-05-25 15:59:13 +02:00

94 lines
2.2 KiB
Go

package navigation
import (
"git.d3nexus.de/Dadido3/D3vugu-components/components/input"
"github.com/vugu/vugu"
)
type Pagination struct {
AttrMap vugu.AttrMap
buttons []paginationButtonInfo
Page int `vugu:"data"`
Pages int `vugu:"data"`
Pagination PaginationHandler // External handler that is called upon an event.
}
type paginationButtonInfo struct {
Page int
Class string
}
func (c *Pagination) handleClickPrev(event input.ClickEvent) {
c.handleClickPage(event, c.Page-1)
}
func (c *Pagination) handleClickNext(event input.ClickEvent) {
c.handleClickPage(event, c.Page+1)
}
func (c *Pagination) handleClickPage(event input.ClickEvent, page int) {
if page <= 0 || page > c.Pages {
return
}
if c.Pagination != nil {
c.Pagination.PaginationHandle(PaginationEvent{
DOMEvent: event,
Page: page,
})
}
}
func (c *Pagination) Init(ctx vugu.InitCtx) {
c.Pages = 10
}
func (c *Pagination) Compute(ctx vugu.ComputeCtx) {
// Number of buttons around the current page.
// Excluding first and last page.
const AroundButtons = 2
// Number of buttons that can change, so excluding first and last page.
const DynamicButtons = AroundButtons + 1 + AroundButtons
// Number of buttons with page numbers, so excluding "previous" and "next".
const TotalButtons = 1 + DynamicButtons + 1
// Prepare buttons.
buttons := [TotalButtons]paginationButtonInfo{
0: {Page: 1},
TotalButtons - 1: {Page: c.Pages},
}
startPage := 2
if startPage < c.Page-AroundButtons {
startPage = c.Page - AroundButtons
}
if startPage > c.Pages-DynamicButtons {
startPage = c.Pages - DynamicButtons
}
for i := 0; i < DynamicButtons; i++ {
buttons[i+1] = paginationButtonInfo{Page: i + startPage}
}
// Remove duplicates and set class accordingly.
// pageCounter is used to check if pages are strictly monotonically increasing.
buttonsFiltered := make([]paginationButtonInfo, 0, len(buttons))
pageCounter := 0
for _, page := range buttons {
if pageCounter < page.Page && page.Page <= c.Pages {
pageCounter = page.Page
if page.Page == c.Page {
page.Class = "d3c-button-highlight"
}
buttonsFiltered = append(buttonsFiltered, page)
}
}
c.buttons = buttonsFiltered
}