-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
63 lines (51 loc) · 1.06 KB
/
label.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
package beleine
//LABEL
import (
"errors"
"fmt"
)
type Label struct {
id string
text string
size int
js string
}
//Pseudo-object creation function
func NewLabel() Label {
return Label{id: GetGlobalID()}
}
func (l *Label) GetID() string {
return l.id
}
func (l *Label) SetText(text string) {
l.text = text
}
func (l *Label) SetTextJS(text string) string {
return fmt.Sprintf(`%s.innerHTML="%s"`, l.id, text)
}
func (l *Label) SetOnClickListener(listener string) {
l.js += fmt.Sprintf("%s.onclick = function(){%s}", l.id, listener)
}
///Size in 0-6 number, 0 - default 1-6 <Hx>
func (l *Label) SetSize(size int) error {
if size > 6 {
if size < 0 {
return errors.New("value must be in 0-6 range")
}
}
l.size = size
return nil
}
func (l *Label) Render() string {
if l.size == 0 {
return fmt.Sprintf(`<p id="%s">%s</p>`, l.id, l.text)
} else {
return fmt.Sprintf(`<h%d id="%s">%s</h%d>`, l.size, l.id, l.text, l.size)
}
}
func (l *Label) RenderJS() string {
return l.js
}
func (l *Label) GetTextJS() string {
return fmt.Sprintf("%s.value", l.id)
}