whispervis/widgets/simulation.go

127 lines
2.7 KiB
Go
Raw Normal View History

2018-09-19 16:00:53 +03:00
package widgets
import (
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
"github.com/gopherjs/vecty/prop"
)
// Simulation represents configuration panel for propagation simulation.
type Simulation struct {
vecty.Core
startSimulation func() error
2018-10-19 16:01:49 +02:00
replay func()
2018-09-19 16:00:53 +03:00
address string // backend host address
errMsg string
hasResults bool
inProgress bool
2018-09-19 16:00:53 +03:00
}
// NewSimulation creates new simulation configuration panel. If simulation
// backend host address is not specified, it'll use 'localhost:8084' as a default.
func NewSimulation(address string, startSimulation func() error, replay func()) *Simulation {
2018-09-19 16:00:53 +03:00
if address == "" {
address = "http://localhost:8084"
}
return &Simulation{
2018-10-19 16:01:49 +02:00
address: address,
startSimulation: startSimulation,
replay: replay,
2018-09-19 16:00:53 +03:00
}
}
// Render implements vecty.Component interface for Simulation.
func (s *Simulation) Render() vecty.ComponentOrHTML {
2018-10-20 14:47:19 +02:00
return Widget(
2018-09-19 16:00:53 +03:00
elem.Div(
2018-10-19 22:10:56 +02:00
Header("Simulation backend:"),
elem.Div(
elem.Input(
vecty.Markup(
2018-10-19 22:10:56 +02:00
vecty.MarkupIf(s.inProgress,
vecty.Attribute("disabled", "true"),
),
prop.Value(s.address),
2018-10-19 22:10:56 +02:00
vecty.Attribute("placehoder", "backend url"),
event.Input(s.onEditInput),
2018-10-19 22:10:56 +02:00
vecty.Class("input", "is-small"),
),
2018-09-19 16:00:53 +03:00
),
),
),
elem.Div(
2018-09-19 16:00:53 +03:00
elem.Button(
vecty.Markup(
vecty.MarkupIf(s.inProgress,
2018-10-19 22:10:56 +02:00
vecty.Attribute("disabled", "true"),
vecty.Class("is-loading"),
),
2018-10-19 22:10:56 +02:00
vecty.Class("button", "is-info", "is-small"),
2018-09-19 16:00:53 +03:00
event.Click(s.onSimulateClick),
),
vecty.Text("Start simulation"),
),
vecty.If(s.hasResults,
elem.Button(
vecty.Markup(
2018-10-19 22:10:56 +02:00
vecty.Class("button", "is-success", "is-small"),
event.Click(s.onRestartClick),
),
vecty.Text("Replay"),
2018-09-20 22:04:13 +03:00
),
),
elem.Break(),
2018-10-19 22:10:56 +02:00
vecty.If(s.inProgress, elem.Div(
vecty.Markup(
vecty.Class("notification", "is-success"),
),
vecty.Text("Running simulation..."),
)),
elem.Div(
2018-10-19 22:10:56 +02:00
vecty.If(s.errMsg != "", elem.Div(
vecty.Markup(
2018-10-19 22:10:56 +02:00
vecty.Class("notification", "is-danger"),
),
vecty.Text(s.errMsg),
)),
2018-09-20 22:04:13 +03:00
),
2018-09-19 16:00:53 +03:00
),
)
}
func (s *Simulation) onEditInput(event *vecty.Event) {
value := event.Target.Get("value").String()
s.address = value
}
// Address returns the current backend address.
func (s *Simulation) Address() string {
return s.address
}
func (s *Simulation) onSimulateClick(e *vecty.Event) {
go func() {
s.errMsg = ""
s.hasResults = false
s.inProgress = true
vecty.Rerender(s)
err := s.startSimulation()
if err != nil {
s.errMsg = err.Error()
}
s.hasResults = err == nil
s.inProgress = false
vecty.Rerender(s)
}()
2018-09-19 21:06:30 +03:00
}
2018-09-20 22:04:13 +03:00
func (s *Simulation) onRestartClick(e *vecty.Event) {
2018-10-19 16:01:49 +02:00
go s.replay()
2018-09-19 16:00:53 +03:00
}