whispervis/widgets/force_input.go

89 lines
1.6 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
description string
value float64
2018-09-06 16:54:35 +03:00
}
2018-09-19 16:00:53 +03:00
// NewForceInput creates new input.
func NewForceInput(title, description string, value float64) *ForceInput {
f := &ForceInput{
title: title,
description: description,
value: value,
2018-09-06 16:54:35 +03:00
}
return f
2018-09-06 16:54:35 +03:00
}
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("%.4f", f.value)
2018-10-24 17:37:39 +02:00
return InputField(f.title, f.description,
elem.Input(
2018-09-06 16:54:35 +03:00
vecty.Markup(
2018-10-24 17:37:39 +02:00
vecty.Class("input", "is-small"),
vecty.Style("text-align", "right"),
prop.Value(value),
event.Input(f.onEditInput),
2018-10-19 22:10:56 +02:00
),
),
)
}
// helper for wrapping many divs
2018-10-24 17:37:39 +02:00
func fieldControl(element vecty.MarkupOrChild) *vecty.HTML {
2018-10-19 22:10:56 +02:00
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.Paragraph(
2018-10-19 22:10:56 +02:00
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
}