whispervis/widgets/force_input.go

103 lines
1.8 KiB
Go
Raw Normal View History

2018-09-06 16:54:35 +03: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 16:00:53 +03:00
// ForceInput represents input widget for forces.
2018-09-06 16:54:35 +03:00
type ForceInput struct {
vecty.Core
changed bool
title string
value float64
}
2018-09-19 16:00:53 +03:00
// NewForceInput creates new input.
2018-09-06 16:54:35 +03:00
func NewForceInput(title string, value float64) *ForceInput {
return &ForceInput{
title: title,
value: value,
}
}
2018-09-19 16:00:53 +03:00
// Render implements vecty.Component interface for ForceInput.
2018-09-06 16:54:35 +03:00
func (f *ForceInput) Render() vecty.ComponentOrHTML {
value := fmt.Sprintf("%.2f", f.value)
return elem.Div(
vecty.Markup(
2018-10-19 22:10:56 +02:00
vecty.Class("field", "is-horizontal", "is-paddingless", "is-marginless"),
2018-09-06 16:54:35 +03:00
),
2018-10-19 22:10:56 +02:00
elem.Div(
2018-09-06 16:54:35 +03:00
vecty.Markup(
2018-10-19 22:10:56 +02:00
vecty.Class("field-label"),
),
elem.Label(
vecty.Markup(
vecty.Class("label"),
),
vecty.Text(f.title),
2018-09-06 16:54:35 +03:00
),
),
2018-10-19 22:10:56 +02:00
fieldControl(
elem.Input(
vecty.Markup(
vecty.Class("input", "is-small"),
prop.Value(value),
event.Input(f.onEditInput),
),
),
),
)
}
// helper for wrapping many divs
func fieldControl(element *vecty.HTML) *vecty.HTML {
return elem.Div(
vecty.Markup(
vecty.Class("field-body"),
),
elem.Div(
2018-09-06 16:54:35 +03:00
vecty.Markup(
2018-10-19 22:10:56 +02:00
vecty.Class("field"),
),
elem.Div(
vecty.Markup(
vecty.Class("control"),
),
element,
2018-09-06 16:54:35 +03:00
),
),
)
}
func (f *ForceInput) onEditInput(event *vecty.Event) {
2018-09-17 22:11:04 +03:00
value := event.Target.Get("value").Float()
2018-09-06 16:54:35 +03:00
f.changed = true
2018-09-17 22:11:04 +03:00
f.value = value
2018-09-06 16:54:35 +03: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
}