-
Notifications
You must be signed in to change notification settings - Fork 5
/
page.go
285 lines (254 loc) · 6.3 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"fmt"
"github.com/divan/graphx/layout"
"github.com/divan/whispervis/network"
"github.com/divan/whispervis/widgets"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
)
// Page is our main page component.
type Page struct {
vecty.Core
layout *layout.Layout
webgl *WebGLScene
loaded bool
isSimulating bool
loader *widgets.Loader
network *widgets.NetworkSelector
forceEditor *widgets.ForceEditor
graphics *widgets.Graphics
simulationWidget *widgets.Simulation
statsWidget *widgets.Stats
statsPage *StatsPage
faqPage *FAQPage
simulation *Simulation
activeView string
}
// NewPage creates and inits new app page.
func NewPage() *Page {
page := &Page{}
// preload defaults
page.activeView = View3D
// init widgets
page.loader = widgets.NewLoader()
page.webgl = NewWebGLScene(page.onWebGLReady)
page.network = widgets.NewNetworkSelector(page.onNetworkChange)
page.forceEditor = widgets.NewForceEditor(page.onForcesApply)
page.graphics = widgets.NewGraphics(page.webgl)
page.simulationWidget = widgets.NewSimulation("http://localhost:8084", page.startSimulation, page.replaySimulation, page.stepSimulation)
page.statsWidget = widgets.NewStats()
page.statsPage = NewStatsPage()
page.faqPage = NewFAQPage()
return page
}
// Render implements the vecty.Component interface.
func (p *Page) Render() vecty.ComponentOrHTML {
return elem.Body(
elem.Div(
vecty.Markup(
vecty.Class("columns"),
),
// Left sidebar
elem.Div(
vecty.Markup(
vecty.Class("column", "is-narrow"),
vecty.Style("width", "300px"),
),
p.header(),
elem.Div(
vecty.Markup(
vecty.MarkupIf(p.isSimulating,
vecty.Attribute("disabled", "true"),
),
),
p.network,
p.forceEditor,
p.graphics,
),
elem.Div(
vecty.Markup(
vecty.MarkupIf(!p.loaded, vecty.Style("visibility", "hidden")),
),
p.simulationWidget,
elem.Div(
vecty.Markup(
vecty.MarkupIf(p.isSimulating,
vecty.Attribute("disabled", "true"),
),
),
p.statsWidget,
),
),
),
// Right page section
elem.Div(
vecty.Markup(
vecty.Class("column"),
),
p.renderTabs(),
elem.Div(
/*
we use display:none property to hide WebGL instead of mounting/unmounting,
because we want to create only one WebGL context and reuse it. Plus,
WebGL takes time to initialize, so it can do it being hidden.
*/
vecty.Markup(
vecty.MarkupIf(!p.loaded || p.activeView != View3D,
vecty.Class("is-invisible"),
vecty.Style("height", "0px"),
),
),
p.webgl,
),
vecty.If(p.activeView == ViewStats,
p.statsPage,
),
vecty.If(p.activeView == ViewFAQ,
p.faqPage,
),
vecty.If(!p.loaded,
elem.Div(
vecty.Markup(
vecty.Class("has-text-centered"),
),
p.loader,
),
),
),
),
vecty.Markup(
event.KeyDown(p.KeyListener),
event.MouseMove(p.MouseMoveListener),
event.VisibilityChange(p.VisibilityListener),
),
)
}
// onForcesApply executes when Force Editor click is fired.
func (p *Page) onForcesApply() {
if !p.loaded {
return
}
p.UpdateGraph()
}
func (p *Page) onNetworkChange(network *network.Network) {
fmt.Println("Network changed:", network)
// reset view on network switch
p.switchView(View3D)
// reset simulation and stats
p.simulation = nil
p.simulationWidget.Reset()
p.statsWidget.Reset()
config := p.forceEditor.Config()
p.layout = layout.New(network.Data, config.Config)
// set forced positions if found in network
if network.Positions != nil {
p.layout.SetPositions(network.Positions)
go p.RecreateObjects()
return
}
// else, recalculate positions
go p.UpdateGraph()
}
// startSimulation is called on the end of each simulation round.
// Returns numnber of timesteps for the simulation.
// TODO(divan): maybe sim widget need to have access to whole simulation?
func (p *Page) startSimulation() (int, error) {
p.isSimulating = true
vecty.Rerender(p)
defer func() {
p.isSimulating = false
vecty.Rerender(p)
}()
backend := p.simulationWidget.Address()
ttl := p.simulationWidget.TTL()
sim, err := p.runSimulation(backend, ttl)
if err != nil {
return 0, err
}
// calculate stats and update stats widget
stats := p.RecalculateStats(sim.plog)
p.statsWidget.Update(stats)
sim.stats = stats
p.simulation = sim
p.statsPage.UpdateStats(p.network.Current().Data, p.simulation.plog)
p.replaySimulation()
return len(sim.plog.Timestamps), nil
}
// replaySimulation animates last simulation.
func (p *Page) replaySimulation() {
if p.simulation == nil {
return
}
p.webgl.AnimatePropagation(p.simulation.plog)
}
func (p *Page) header() *vecty.HTML {
return elem.Section(
elem.Heading2(
vecty.Markup(
vecty.Class("title", "has-text-weight-light"),
),
vecty.Text("Whisper Simulator"),
),
elem.Heading6(
vecty.Markup(
vecty.Class("subtitle", "has-text-weight-light"),
),
vecty.Text("This simulator shows message propagation in the Whisper network."),
),
)
}
// onWebGLReady is executed when WebGL context is up and ready to render scene.
func (p *Page) onWebGLReady() {
p.onNetworkChange(p.network.Current())
}
func (p *Page) renderTabs() *vecty.HTML {
return elem.Div(
vecty.Markup(
vecty.Class("tabs", "is-marginless", "is-boxed", "is-fullwidth"),
),
elem.UnorderedList(
elem.ListItem(
vecty.Markup(
vecty.MarkupIf(p.activeView == View3D,
vecty.Class("is-active"),
),
event.Click(p.onTabSwitch(View3D)),
),
elem.Anchor(
vecty.Text("3D view"),
),
),
elem.ListItem(
vecty.Markup(
vecty.MarkupIf(p.activeView == ViewStats,
vecty.Class("is-active"),
),
event.Click(p.onTabSwitch(ViewStats)),
),
elem.Anchor(
vecty.Text("Stats view"),
),
),
elem.ListItem(
vecty.Markup(
vecty.MarkupIf(p.activeView == ViewFAQ,
vecty.Class("is-active"),
),
event.Click(p.onTabSwitch(ViewFAQ)),
),
elem.Anchor(
vecty.Text("FAQ"),
),
),
),
)
}
// stepSimulation animates a single step from the last simulation.
func (p *Page) stepSimulation(step int) {
if p.simulation == nil {
return
}
p.webgl.AnimateOneStep(p.simulation.plog, step)
}