-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
96 lines (75 loc) · 2.71 KB
/
root.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
// Copyright (C) 2021 David Vogel
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/vugu/vgrouter"
"github.com/vugu/vugu"
js "github.com/vugu/vugu/js"
)
type Root struct {
vgrouter.NavigatorRef `json:"-"`
sidebarDisplay string
Body vugu.Builder
}
func (r *Root) handleSidebarOpen(event vugu.DOMEvent) {
r.sidebarDisplay = "block"
}
func (r *Root) handleSidebarClose(event vugu.DOMEvent) {
r.sidebarDisplay = "none"
}
func (r *Root) handleDownload(event vugu.DOMEvent) {
data, err := json.MarshalIndent(globalSite, "", "\t")
if err != nil {
log.Printf("json.Marshal failed: %v", err)
}
browserDownload(fmt.Sprintf("%v.D3survey", globalSite.Name), data, "application/octet-stream")
}
func (r *Root) handleUploadClick(event vugu.DOMEvent) {
js.Global().Get("document").Call("getElementById", "site-upload").Call("click")
}
func (r *Root) handleUpload(event vugu.DOMEvent) {
fileReader := js.Global().Get("FileReader").New()
fileReader.Call("addEventListener", "loadend", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
buffer := fileReader.Get("result")
uint8Array := js.Global().Get("Uint8Array").New(buffer)
jsonData := make([]byte, uint8Array.Length())
js.CopyBytesToGo(jsonData, uint8Array)
event.EventEnv().Lock()
defer event.EventEnv().UnlockRender()
newSite, err := NewSiteFromJSON(jsonData)
if err != nil {
log.Printf("NewSiteFromJSON failed: %v", err)
// TODO: Somehow tell the user the file couldn't be loaded
return js.Undefined()
}
globalSite = newSite
r.Navigate("/", nil)
return js.Undefined()
}))
imgFiles := js.Global().Get("document").Call("getElementById", "site-upload").Get("files")
if imgFiles.Length() != 1 {
log.Printf("Wrong amount of files: Expected %v, got %v", 1, imgFiles.Length())
// TODO: Somehow forward the error to the user
return
}
fileReader.Call("readAsArrayBuffer", imgFiles.Index(0))
}
func (r *Root) handleExport(event vugu.DOMEvent) {
data := generateObj(globalSite)
browserDownload(fmt.Sprintf("%v.obj", globalSite.Name), data, "application/octet-stream")
}