-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
103 lines (86 loc) · 2.34 KB
/
base.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
package base
import (
"fmt"
"log"
"reflect"
"strings"
"github.com/google/uuid"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type JsUtil struct{}
func (j *JsUtil) JsValueAtPath(path string) app.Value {
return j.JsValueAt(app.Window(), path, true)
}
func (j *JsUtil) JsValueAt(root app.Value, path string, warn bool) app.Value {
if root == nil || root.IsUndefined() {
return app.Undefined()
}
var current = root
for _, part := range strings.Split(path, ".") {
current = current.Get(part)
if current.IsUndefined() {
if warn {
fmt.Printf("jsutil.getValue(%q) : undefined at %q\n", path, part)
}
break
}
}
return current
}
func (j *JsUtil) JsNewAtPath(path string, args ...interface{}) app.Value {
v := j.JsValueAtPath(path)
if v.Truthy() {
return v.New(args...)
}
return app.Undefined()
}
// MDCRipple constructs a new mdc.ripple.MDCRipple from a provided element id
func (j *JsUtil) MDCRipple(id string) app.Value {
return j.MDCRippleVal(app.Window().GetElementByID(id))
}
// MDCRippleVal constructs a new mdc.ripple.MDCRipple from a provided app.Value
func (j *JsUtil) MDCRippleVal(val app.Value) app.Value {
if val == nil || val.IsUndefined() {
return app.Undefined()
}
return j.JsNewAtPath("mdc.ripple.MDCRipple", val)
}
// UUID generates a new uuid string for testing and demo purposes
func (j *JsUtil) UUID() string {
return uuid.New().String()
}
type Counter struct {
app.Compo
Label string
Count int
}
func (c *Counter) Render() app.UI {
return app.Span().Text(fmt.Sprintf("counter %q is currently %d", c.Label, c.Count))
}
type Message struct {
app.Compo
Text string
}
func (c *Message) Render() app.UI {
return app.Code().Text(c.Text)
}
func (j *JsUtil) LogWithP(o interface{}, msg string) {
ptr := reflect.ValueOf(o).Pointer()
typ := reflect.TypeOf(o)
log.Printf("%s(%d) %s\n", typ, ptr, msg)
}
func (j *JsUtil) ConsoleLog(args ...interface{}) {
app.Window().Get("console").Call("log", args...)
}
// HTMLButtonUpdate is a convenience method for applying additional operations on buttons that are used in nav
// bars or other locations.
func HTMLButtonUpdate(buttons []app.HTMLButton, modify func(button app.HTMLButton)) (result []app.UI) {
for _, button := range buttons {
if button != nil {
modify(button)
result = append(result, button)
}
}
return
}
const AddEventListener = "addEventListener"