-
Notifications
You must be signed in to change notification settings - Fork 0
/
element_list.go
160 lines (127 loc) · 3.27 KB
/
element_list.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package dom
import "github.com/dairaga/js"
// ElementList is a collection of elements.
type ElementList []*Element
var emptyNodeList = []*Element{}
// ElementListOf returns a element list.
func ElementListOf(v js.Value) ElementList {
size := v.Length()
if size <= 0 {
return emptyNodeList
}
lst := make([]*Element, size)
for i := 0; i < size; i++ {
lst[i] = ElementOf(v.Index(i))
}
return lst
}
// Length returns the size of list.
func (nl ElementList) Length() int {
return len(nl)
}
// Foreach apply function on each element.
func (nl ElementList) Foreach(fn func(idx int, v *Element)) {
size := nl.Length()
for i := 0; i < size; i++ {
fn(i, nl[i])
}
}
// Append add a new element.
func (nl ElementList) Append(x ...*Element) ElementList {
return append(nl, x...)
}
// AppendList appends other list.
func (nl ElementList) AppendList(other ElementList) ElementList {
return append(nl, other...)
}
// Attr returns some attribute values.
func (nl ElementList) Attr(name string) []string {
var result []string
nl.Foreach(func(_ int, e *Element) {
result = append(result, e.Attr(name))
})
return result
}
// SetAttr sets attribute to all elements.
func (nl ElementList) SetAttr(name, value string) ElementList {
nl.Foreach(func(_ int, e *Element) {
e.SetAttr(name, value)
})
return nl
}
// Prop returns some property values.
func (nl ElementList) Prop(name string) []js.Value {
var result []js.Value
nl.Foreach(func(_ int, e *Element) {
result = append(result, e.Prop(name))
})
return result
}
// SetProp sets property to all elements.
func (nl ElementList) SetProp(name string, flag bool) ElementList {
nl.Foreach(func(_ int, e *Element) {
e.SetProp(name, flag)
})
return nl
}
// AddClass adds class to all elements.
func (nl ElementList) AddClass(names ...string) ElementList {
nl.Foreach(func(_ int, e *Element) {
e.AddClass(names...)
})
return nl
}
// RemoveClass remove some class from all elements.
func (nl ElementList) RemoveClass(names ...string) ElementList {
nl.Foreach(func(_ int, e *Element) {
e.RemoveClass(names...)
})
return nl
}
// ToggleClass toggles some class on all elements.
func (nl ElementList) ToggleClass(name string) ElementList {
nl.Foreach(func(_ int, e *Element) {
e.ToggleClass(name)
})
return nl
}
// ReplaceClass replace some class with new one on all elements.
func (nl ElementList) ReplaceClass(oldName, newName string) ElementList {
nl.Foreach(func(_ int, e *Element) {
e.ReplaceClass(oldName, newName)
})
return nl
}
// HasClass returns elements which has specific class.
func (nl ElementList) HasClass(name string) ElementList {
var result []*Element
nl.Foreach(func(_ int, e *Element) {
if e.HasClass(name) {
result = append(result, e)
}
})
return result
}
// TagName returns all tag names.
func (nl ElementList) TagName() []string {
var result []string
nl.Foreach(func(_ int, e *Element) {
result = append(result, e.TagName())
})
return result
}
// Val returns all values.
func (nl ElementList) Val() []string {
var result []string
nl.Foreach(func(_ int, e *Element) {
result = append(result, e.Val())
})
return result
}
// SetVal set value to all elements.
func (nl ElementList) SetVal(val interface{}) ElementList {
nl.Foreach(func(_ int, e *Element) {
nl.SetVal(val)
})
return nl
}