whispervis/widgets/force_input.go

76 lines
1.4 KiB
Go
Raw Permalink Normal View History

2018-09-06 13:54:35 +00:00
package widgets
import (
"fmt"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
"github.com/gopherjs/vecty/prop"
)
2018-09-19 13:00:53 +00:00
// ForceInput represents input widget for forces.
2018-09-06 13:54:35 +00:00
type ForceInput struct {
vecty.Core
changed bool
title string
value float64
}
2018-09-19 13:00:53 +00:00
// NewForceInput creates new input.
2018-09-06 13:54:35 +00:00
func NewForceInput(title string, value float64) *ForceInput {
return &ForceInput{
title: title,
value: value,
}
}
2018-09-19 13:00:53 +00:00
// Render implements vecty.Component interface for ForceInput.
2018-09-06 13:54:35 +00:00
func (f *ForceInput) Render() vecty.ComponentOrHTML {
value := fmt.Sprintf("%.2f", f.value)
return elem.Div(
vecty.Markup(
vecty.Class("pure-markup-group", "pure-u-1"),
),
elem.Label(
vecty.Markup(
vecty.Class("pure-u-1-2"),
),
vecty.Text(f.title),
),
elem.Input(
vecty.Markup(
prop.Value(value),
event.Input(f.onEditInput),
vecty.Class("pure-input-1-3"),
vecty.Style("float", "right"),
vecty.Style("margin-right", "10px"),
vecty.Style("text-align", "right"),
),
),
)
}
func (f *ForceInput) onEditInput(event *vecty.Event) {
2018-09-17 19:11:04 +00:00
value := event.Target.Get("value").Float()
2018-09-06 13:54:35 +00:00
f.changed = true
2018-09-17 19:11:04 +00:00
f.value = value
2018-09-06 13:54:35 +00:00
}
// Value returns the current value.
func (f *ForceInput) Value() float64 {
return f.value
}
// Changed returns if input value has been changed, and resets it's value to false.
func (f *ForceInput) Changed() bool {
if f.changed {
f.changed = false
return true
}
return false
}