-
Notifications
You must be signed in to change notification settings - Fork 6
/
list_ordered.go
95 lines (81 loc) · 2.68 KB
/
list_ordered.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
83
84
85
86
87
88
89
90
91
92
93
94
95
package control
import (
"bytes"
"context"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
html2 "html"
"strconv"
)
// OrderedList is a dynamically generated html ordered list (ol). Such lists are often used as the basis for
// javascript and css widgets. If you use a data provider to set the data, you should call AddItems to the list
// in your GetData function.
type OrderedList struct {
UnorderedList
}
const (
OrderedListNumberTypeNumber = "1" // default
OrderedListNumberTypeUpperLetter = "A"
OrderedListNumberTypeLowerLetter = "a"
OrderedListNumberTypeUpperRoman = "I"
OrderedListNumberTypeLowerRoman = "i"
)
// NewOrderedList creates a new ordered list (ol tag).
func NewOrderedList(parent page.ControlI, id string) *OrderedList {
t := &OrderedList{}
t.Init(t, parent, id)
return t
}
func (l *OrderedList) Init(self page.ControlI, parent page.ControlI, id string) {
l.UnorderedList.Init(self, parent, id)
l.Tag = "ol"
}
// SetNumberType sets the top level number style for the list. Choose from the OrderedListNumberType* constants.
// To set a number type for a sublevel, set the "type" attribute on the list item that is the parent of the sub list.
func (l *OrderedList) SetNumberType(t string) *OrderedList {
l.SetAttribute("type", l)
return l
}
// SetStart sets the starting number for the numbers in the top level list. To set the start of a sub-list, set
// the "start" attribute on the list item that is the parent of the sub-list.
func (l *OrderedList) SetStart(start int) *OrderedList {
l.SetAttribute("start", strconv.Itoa(start))
return l
}
// NumberType returns the string used for the type attribute.
func (l *OrderedList) NumberType() string {
if a := l.Attribute("type"); a == "" {
return OrderedListNumberTypeNumber
} else {
return a
}
}
func (l *OrderedList) ΩDrawInnerHtml(ctx context.Context, buf *bytes.Buffer) (err error) {
h := l.getItemsHtml(l.items)
buf.WriteString(h)
return nil
}
func (l *OrderedList) getItemsHtml(items []ListItemI) string {
var h = ""
for _, item := range items {
if item.HasChildItems() {
innerhtml := l.getItemsHtml(item.ListItems())
a := item.Attributes().Copy()
// Certain attributes apply to the sub list and not the list item, so we split them here
a2 := html.NewAttributes()
if a.Has("type") {
a2.Set("type", a.Get("type"))
a.RemoveAttribute("type")
}
if a.Has("start") {
a2.Set("start", a.Get("start"))
a.RemoveAttribute("start")
}
innerhtml = html.RenderTag(l.Tag, a2, innerhtml)
h += html.RenderTag(l.itemTag, a, item.Label()+" "+innerhtml)
} else {
h += html.RenderTag(l.itemTag, item.Attributes(), html2.EscapeString(item.Label()))
}
}
return h
}