From ef1abc3b975e1c77fc8663c5e5e95769fc934551 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 20 May 2020 17:12:56 +0200 Subject: Make all stateful components implement ObservableState interface --- .../kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt | 9 ++++++++- .../pl/treksoft/kvision/form/spinner/SpinnerInput.kt | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft') diff --git a/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt b/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt index 3901cc4f..2dbcd443 100644 --- a/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt +++ b/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt @@ -30,6 +30,7 @@ import pl.treksoft.kvision.form.InvalidFeedback import pl.treksoft.kvision.form.NumberFormControl import pl.treksoft.kvision.html.ButtonStyle import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.SnOn /** @@ -52,7 +53,7 @@ open class Spinner( decimals: Int = 0, val buttonsType: ButtonsType = ButtonsType.VERTICAL, forceType: ForceType = ForceType.NONE, buttonStyle: ButtonStyle? = null, label: String? = null, rich: Boolean = false -) : SimplePanel(setOf("form-group")), NumberFormControl { +) : SimplePanel(setOf("form-group")), NumberFormControl, ObservableState { /** * Spinner value. @@ -252,6 +253,12 @@ open class Spinner( invalidFeedback.addCssClass("col-sm-${horizontalRatio.fields}") } + override fun getState(): Number? = input.getState() + + override fun subscribe(observer: (Number?) -> Unit): () -> Unit { + return input.subscribe(observer) + } + companion object { internal var counter = 0 } diff --git a/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt b/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt index 98b7be62..9ed9d707 100644 --- a/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt +++ b/kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt @@ -31,6 +31,7 @@ import pl.treksoft.kvision.form.FormInput import pl.treksoft.kvision.form.InputSize import pl.treksoft.kvision.form.ValidationStatus import pl.treksoft.kvision.html.ButtonStyle +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.obj import pl.treksoft.kvision.utils.set @@ -75,12 +76,14 @@ open class SpinnerInput( decimals: Int = 0, val buttonsType: ButtonsType = ButtonsType.VERTICAL, forceType: ForceType = ForceType.NONE, buttonStyle: ButtonStyle? = null, classes: Set = setOf() -) : Widget(classes + "form-control"), FormInput { +) : Widget(classes + "form-control"), FormInput, ObservableState { + + protected val observers = mutableListOf<(Number?) -> Unit>() /** * Spinner value. */ - var value by refreshOnUpdate(value) { refreshState() } + var value by refreshOnUpdate(value) { refreshState(); observers.forEach { ob -> ob(it) } } /** * The value attribute of the generated HTML input element. @@ -328,6 +331,16 @@ open class SpinnerInput( override fun blur() { getElementJQuery()?.blur() } + + override fun getState(): Number? = value + + override fun subscribe(observer: (Number?) -> Unit): () -> Unit { + observers += observer + observer(value) + return { + observers -= observer + } + } } /** -- cgit