forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
82 lines (76 loc) · 1.78 KB
/
simple.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package paginator
import (
"gnd.la/html"
"strings"
)
type SimplePager struct {
Tag string
Wrapper string
Next string
Prev string
Separator string
NextClass string
PrevClass string
CurrentClass string
DisabledClass string
SeparatorClass string
Func Func
}
func (p *SimplePager) Root() *html.Node {
return &html.Node{Tag: p.Tag}
}
func (p *SimplePager) Href(base string, page int) string {
return p.Func(base, page)
}
func (p *SimplePager) Node(n *html.Node, page int, flags int) *html.Node {
var classes []string
if flags&NEXT != 0 {
if p.NextClass != "" {
classes = append(classes, p.NextClass)
}
n.Children = html.Text(p.Next)
} else if flags&PREVIOUS != 0 {
if p.PrevClass != "" {
classes = append(classes, p.PrevClass)
}
n.Children = html.Text(p.Prev)
} else if flags&SEPARATOR != 0 {
if p.SeparatorClass != "" {
classes = append(classes, p.SeparatorClass)
}
n.Children = html.Text(p.Separator)
}
if flags&CURRENT != 0 {
if p.CurrentClass != "" {
classes = append(classes, p.CurrentClass)
}
} else if flags&DISABLED != 0 {
if p.DisabledClass != "" {
classes = append(classes, p.DisabledClass)
}
}
if p.Wrapper != "" {
var attrs html.Attrs
if classes != nil {
attrs = html.Attrs{"class": strings.Join(classes, " ")}
}
return &html.Node{Tag: p.Wrapper, Attrs: attrs, Children: n}
}
if classes != nil {
if n.Attrs == nil {
n.Attrs = html.Attrs{}
}
n.Attrs["class"] = strings.Join(classes, " ")
}
return n
}
func NewSimple(base string, current, count int, next, prev, sep string, f Func) *Paginator {
pager := &SimplePager{
Tag: "div",
Next: next,
Prev: prev,
Separator: sep,
Func: f,
}
return New(base, current, count, pager)
}