-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
166 lines (150 loc) · 3.34 KB
/
button.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
161
162
163
164
165
166
package beleine
//BUTTON
import (
"errors"
"fmt"
)
/*
Button types:
PRIMARY = 0
SECONDARY = 1
SUCCES = 2
DANGER = 3
WARNING = 4
IFNO = 5
LIGHT = 6
DARK = 7
LINK = 8
*/
type Button struct {
id string
text string
size string
btnType string
outline string
state bool
dropdown bool
ddItems map[string]Dropdown
collapse bool
cItem Collapse
js string
}
//Creates new Button struct
func NewButton() Button {
return Button{id: GetGlobalID(), btnType: "-primary", state: true}
}
//Returns the button's id
func (b *Button) GetID() string {
return b.id
}
//Sets text of the button
func (b *Button) SetText(text string) {
b.text = text
}
//Sets text of the button via JS
func (b *Button) SetTextJS(text string) string {
return fmt.Sprintf(`%s.innerHTML="%s"`, b.id, text)
}
/*
Sets size of the button
Size in 0-2 number, 1 - default
*/
func (b *Button) SetSize(size int) error {
switch size {
case 0:
b.size = "btn-sm"
case 1:
b.size = ""
case 2:
b.size = "btn-lg"
default:
return errors.New("value must be in 0-2 range")
}
return nil
}
/*
Sets type of the button
Size in 0-8 number, 0 - default (primary)
*/
func (b *Button) SetButtonType(btnType int) error {
switch btnType {
case 0:
b.btnType = "-primary"
case 1:
b.btnType = "-secondary"
case 2:
b.btnType = "-success"
case 3:
b.btnType = "-danger"
case 4:
b.btnType = "-warning"
case 5:
b.btnType = "-info"
case 6:
b.btnType = "-light"
case 7:
b.btnType = "-dark"
case 8:
b.btnType = "-link"
default:
return errors.New("color doesn't exist")
}
return nil
}
//Sets outline (default false)
func (b *Button) SetOutline(outline bool) {
if outline {
b.outline = "-outline"
} else {
b.outline = ""
}
}
//Sets state of the button (default enabled (true))
func (b *Button) SetState(state bool) {
b.state = state
}
//Sets JS code function, to be executed after click
func (b *Button) SetOnClickListener(listener string) {
b.js += fmt.Sprintf("%s.onclick = function(){%s}", b.id, listener)
}
func (b *Button) Render() string {
if b.state {
if b.dropdown {
dItems := ""
for _, v := range b.ddItems {
dItems += fmt.Sprintf(`<a id="%s" class="dropdown-item" href="#">%s</a>`, v.id, v.name)
}
return fmt.Sprintf(`<div class="dropdown">
<button class="btn btn%s%s %s dropdown-toggle" type="button" id="%s" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
%s
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
%s
</div>
</div>`, b.outline, b.btnType, b.size, b.id, b.text, dItems)
}
if b.collapse {
return fmt.Sprintf(`
<p>
<button id="%s" class="btn btn%s%s %s" type="button" data-toggle="collapse" data-target="#%s" aria-expanded="false" aria-controls="%s">
%s
</button>
</p>
<div class="collapse" id="%s">
<div class="card card-body">
%s
</div>
</div>
`, b.id, b.outline, b.btnType, b.size, b.cItem.id, b.cItem.id, b.text, b.cItem.id, b.cItem.text)
}
return fmt.Sprintf(`<button id="%s" type="button" class="btn btn%s%s %s">%s</button>`, b.id, b.outline, b.btnType, b.size, b.text)
} else {
return fmt.Sprintf(`<button id="%s" type="button" class="btn btn%s%s %s" disabled>%s</button>`, b.id, b.outline, b.btnType, b.size, b.text)
}
}
func (b *Button) RenderJS() string {
return b.js
}
func (b *Button) GetTextJS() string {
return fmt.Sprintf("%s.value", b.id)
}