diff options
author | Robert Jaros <rjaros@finn.pl> | 2020-05-20 17:12:56 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2020-05-20 17:12:56 +0200 |
commit | ef1abc3b975e1c77fc8663c5e5e95769fc934551 (patch) | |
tree | 343236378e2f24514635fb5e892043a054c86170 /kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft | |
parent | 35628c364d9768a5589559b99cb698ebc2b56eba (diff) | |
download | kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.tar.gz kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.tar.bz2 kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.zip |
Make all stateful components implement ObservableState interface
Diffstat (limited to 'kvision-modules/kvision-bootstrap-spinner/src/main/kotlin/pl/treksoft')
2 files changed, 23 insertions, 3 deletions
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<Number?> { /** * 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<String> = setOf() -) : Widget(classes + "form-control"), FormInput { +) : Widget(classes + "form-control"), FormInput, ObservableState<Number?> { + + 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 + } + } } /** |