-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
120 lines (105 loc) · 2.75 KB
/
page.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
package page
import (
"syscall/js"
"github.com/akosgarai/wasm-example/pkg/client/dom"
)
const (
// ContainerClassName is the class name of the container
ContainerClassName = "container"
// HeaderClassName is the class name of the header
HeaderClassName = "header"
// ContentClassName is the class name of the content
ContentClassName = "content"
)
// Page is an interface that represents a page
type Page interface {
LoadPage()
Run()
}
// Instance is a struct that implements the Page interface
type Instance struct {
Title string
document js.Value
webSocket js.Value
alert js.Value
json js.Value
}
// NewPage returns a new Instance
func NewPage(title string) *Instance {
jsDoc := js.Global().Get("document")
webSocket := js.Global().Get("WebSocket")
alert := js.Global().Get("alert")
json := js.Global().Get("JSON")
if !jsDoc.Truthy() {
panic("document is not available")
}
return &Instance{
Title: title,
document: jsDoc,
webSocket: webSocket,
alert: alert,
json: json,
}
}
// SetPageTitle sets the page title
func (p *Instance) setPageTitle() {
title := p.document.Call("querySelector", "title")
if !title.Truthy() {
return
}
title.Set("innerHTML", p.Title)
}
// GetElementByID returns the element by the given id
func (p *Instance) GetElementByID(id string) js.Value {
return p.document.Call("getElementById", id)
}
// LoadPage loads the page
func (p *Instance) LoadPage() {
p.setPageTitle()
p.buildLayout()
}
// Run runs the page
func (p *Instance) Run() {
}
// Document returns the document
func (p *Instance) Document() js.Value {
return p.document
}
// WebSocket returns the websocket
func (p *Instance) WebSocket() js.Value {
return p.webSocket
}
// Alert returns the alert
func (p *Instance) Alert() js.Value {
return p.alert
}
// JSON returns the JSON
func (p *Instance) JSON() js.Value {
return p.json
}
// buildLayout builds the layout of the page.
func (p *Instance) buildLayout() {
body := p.document.Call("querySelector", "body")
// Contaner div with class container
containerDiv := dom.Div(p.document, map[string]interface{}{
"className": ContainerClassName,
})
body.Call("appendChild", containerDiv)
// Header div with class header
headerDiv := dom.Div(p.document, map[string]interface{}{
"className": HeaderClassName,
})
containerDiv.Call("appendChild", headerDiv)
// Header div constans a h1 with the title
headerDiv.Call("appendChild", dom.H1(p.document, p.Title))
contentDiv := dom.Div(p.document, map[string]interface{}{
"className": ContentClassName,
})
containerDiv.Call("appendChild", contentDiv)
// overlay div
overlayDiv := dom.Div(p.document, map[string]interface{}{
"id": "overlay",
"className": "overlay hidden",
})
body.Call("appendChild", overlayDiv)
}