8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / site / pager.go
1.1 KB
1package site 2 3import "strconv" 4 5// PagerItem is one link of a pager: an arrow, a page number, or the dots for a gap, which have no Href. 6type PagerItem struct { 7 Text, Href, Label string 8 Here bool // the page being shown 9} 10 11// Pager is the links to the pages around page, of pages: arrows, the first and last pages, the current one and its 12// neighbours, and dots for the gaps. href(n) is the address of page n. It is nil for a single page. 13func Pager(page, pages int, href func(int) string) []PagerItem { 14 if pages < 2 { 15 return nil 16 } 17 arrow := func(n int, text, label string) PagerItem { 18 if n < 1 || n > pages { 19 return PagerItem{Text: text, Label: label} 20 } 21 return PagerItem{Text: text, Href: href(n), Label: label} 22 } 23 items := []PagerItem{arrow(page-1, "←", "Previous page")} 24 for n, last := 1, 0; n <= pages; n++ { 25 if n > 1 && n < pages && (n < page-1 || n > page+1) { 26 continue 27 } 28 if n-last > 1 { 29 items = append(items, PagerItem{Text: "…"}) 30 } 31 items, last = append(items, PagerItem{Text: strconv.Itoa(n), Href: href(n), Here: n == page}), n 32 } 33 return append(items, arrow(page+1, "→", "Next page")) 34}