-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.go
118 lines (104 loc) · 1.97 KB
/
badge.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
package beleine
//BADGE
import (
"errors"
"fmt"
)
/*
Badge types:
0-Primary
1-Secondary
2-Success
3-Danger
4-Warning
5-Info
6-Light
7-Dark
*/
type Badge struct {
id string
text string
style int
pill bool
js string
}
//Creates new Badge struct
func NewBadge() Badge {
return Badge{id: GetGlobalID()}
}
//Returns ID of the Badge
func (b *Badge) GetID() string {
return b.id
}
//Sets text of Badge
func (b *Badge) SetText(text string) {
b.text = text
}
//Enable/Disable more rounded Badge
func (b *Badge) SetPill(pill bool) {
b.pill = pill
}
//Sets Badge style
//Badge styles:
//0-Primary
//1-Secondary
//2-Success
//3-Danger
//4-Warning
//5-Info
//6-Light
//7-Dark
//
//Returns error if value is not in 0-7 range
func (b *Badge) SetStyle(styleNumber int) error {
if styleNumber > 7 {
if styleNumber < 0 {
return errors.New("value must be in 0-7 range")
}
}
b.style = styleNumber
return nil
}
//Returns JS code as string, that sets Badge text
func (b *Badge) SetTextJS(text string) string {
return fmt.Sprintf(`%s.innerHTML="%s"`, b.id, text)
}
//Returns JS code as string, that returns Badge text
func (b *Badge) GetTextJS(text string) string {
return fmt.Sprintf(`%s.innerHTML`, b.id)
}
//Sets JS code function, to be executed after click
func (b *Badge) SetOnClickListener(listener string) {
b.js += fmt.Sprintf("%s.onclick = function(){%s}", b.id, listener)
}
func (b *Badge) Render() string {
pill := ""
if b.pill {
pill = "badge-pill "
}
return fmt.Sprintf(`<span id="%s" class="badge %s">%s</span>`, b.id, pill+b.getStyleClass(), b.text)
}
func (b *Badge) RenderJS() string {
return b.js
}
func (b *Badge) getStyleClass() string {
switch b.style {
case 0:
return "badge-primary"
case 1:
return "badge-secondary"
case 2:
return "badge-success"
case 3:
return "badge-danger"
case 4:
return "badge-warning"
case 5:
return "badge-info"
case 6:
return "badge-light"
case 7:
return "badge-dark"
}
panic("invalid style class number")
}