-
Notifications
You must be signed in to change notification settings - Fork 3
/
elements.go
122 lines (103 loc) · 2.39 KB
/
elements.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
package elements
import (
"sort"
"strings"
)
//Element is an interface for html components
type Element interface {
String() string
}
//RawElement is an elementa to return raw html code
type RawElement struct {
Raw string
}
//String emit html text for RawElement
func (raw *RawElement) String() string {
return raw.Raw
}
//Base is an type base for all elements. This means that all Elements can use the attributes listed below.
type Base struct {
ID string
Classes []string
Style map[string]string
Data map[string]string
}
//AddClass add a new class string for element
func (base *Base) AddClass(class string) {
base.Classes = append(base.Classes, class)
}
//RemoveClass delete an class string in Classes slice
func (base *Base) RemoveClass(class string) {
newCls := []string{}
for _, c := range base.Classes {
if class != c {
newCls = append(newCls, c)
}
}
base.Classes = newCls
}
//SetStyle set a style value for a key
func (base *Base) SetStyle(key string, value string) {
if base.Style == nil {
base.Style = make(map[string]string)
}
base.Style[key] = value
}
//RemoveStyle delete a value in Style map
func (base *Base) RemoveStyle(key string) {
delete(base.Style, key)
}
//SetData set a data value for a key
func (base *Base) SetData(key string, value string) {
if base.Data == nil {
base.Data = make(map[string]string)
}
base.Data[key] = value
}
//RemoveData delete a value in Data map
func (base *Base) RemoveData(key string) {
delete(base.Data, key)
}
//Attrs return string for ID, class info and style
func (base Base) Attrs() string {
var ret string
if base.ID != "" {
ret += " id='" + base.ID + "'"
}
//Classes
if len(base.Classes) != 0 {
ret += " class='"
ret += strings.Join(base.Classes, " ")
ret += "'"
}
//Style
if len(base.Style) != 0 {
ret += " style='"
var keys []string
for key := range base.Style {
keys = append(keys, key)
}
sort.Strings(keys)
for _, val := range keys {
ret += val + ":" + base.Style[val] + ";"
}
ret += "'"
}
var keys []string
for key := range base.Data {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
ret += " " + key + "='" + base.Data[key] + "'"
}
return ret
}
//Container is an base to elements with body
type Container struct {
Body []Element
}
//AddElement put a new element on Body
func (c *Container) AddElement(el Element) {
c.Body = append(c.Body, el)
}