diff options
Diffstat (limited to 'kvision-modules')
8 files changed, 87 insertions, 10 deletions
diff --git a/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTime.kt b/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTime.kt index 68ec96a2..115781cd 100644 --- a/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTime.kt +++ b/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTime.kt @@ -28,6 +28,7 @@ import pl.treksoft.kvision.form.DateFormControl import pl.treksoft.kvision.form.FieldLabel import pl.treksoft.kvision.form.InvalidFeedback import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.SnOn import kotlin.js.Date @@ -44,7 +45,7 @@ import kotlin.js.Date open class DateTime( value: Date? = null, name: String? = null, format: String = "YYYY-MM-DD HH:mm", label: String? = null, rich: Boolean = false -) : SimplePanel(setOf("form-group")), DateFormControl { +) : SimplePanel(setOf("form-group")), DateFormControl, ObservableState<Date?> { /** * Date/time input value. @@ -264,6 +265,12 @@ open class DateTime( input.blur() } + override fun getState(): Date? = input.getState() + + override fun subscribe(observer: (Date?) -> Unit): () -> Unit { + return input.subscribe(observer) + } + companion object { internal var counter = 0 } diff --git a/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt b/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt index 967ca9db..d870947b 100644 --- a/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt +++ b/kvision-modules/kvision-bootstrap-datetime/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt @@ -33,6 +33,7 @@ import pl.treksoft.kvision.html.icon import pl.treksoft.kvision.html.span import pl.treksoft.kvision.i18n.I18n import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.types.toDateF import pl.treksoft.kvision.types.toStringF import pl.treksoft.kvision.utils.obj @@ -53,7 +54,7 @@ internal const val DEFAULT_STEPPING = 5 open class DateTimeInput( value: Date? = null, format: String = "YYYY-MM-DD HH:mm", classes: Set<String> = setOf() -) : SimplePanel(classes + "input-group" + "date"), FormInput { +) : SimplePanel(classes + "input-group" + "date"), FormInput, ObservableState<Date?> { private var initialized = false @@ -404,6 +405,14 @@ open class DateTimeInput( override fun blur() { input.blur() } + + override fun getState(): Date? = input.getState()?.toDateF(format) + + override fun subscribe(observer: (Date?) -> Unit): () -> Unit { + return input.subscribe { str -> + observer(str?.toDateF(format)) + } + } } /** diff --git a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt index b4e4738c..18c522d5 100644 --- a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt +++ b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt @@ -31,6 +31,7 @@ import pl.treksoft.kvision.form.StringFormControl import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.remote.KVServiceManager import pl.treksoft.kvision.remote.RemoteOption +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.SnOn /** @@ -60,7 +61,7 @@ open class SelectRemote<T : Any>( preload: Boolean = false, label: String? = null, rich: Boolean = false -) : SimplePanel(setOf("form-group")), StringFormControl { +) : SimplePanel(setOf("form-group")), StringFormControl, ObservableState<String?> { /** * A value of the selected option. */ @@ -254,6 +255,12 @@ open class SelectRemote<T : Any>( input.blur() } + override fun getState(): String? = input.getState() + + override fun subscribe(observer: (String?) -> Unit): () -> Unit { + return input.subscribe(observer) + } + companion object { internal var counter = 0 } diff --git a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt index 4ffece7d..5fb92ab8 100644 --- a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt +++ b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt @@ -30,6 +30,7 @@ import pl.treksoft.kvision.form.FieldLabel import pl.treksoft.kvision.form.InvalidFeedback import pl.treksoft.kvision.form.StringFormControl import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.SnOn /** @@ -52,7 +53,7 @@ open class Select( options: List<StringPair>? = null, value: String? = null, name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, rich: Boolean = false -) : SimplePanel(setOf("form-group")), StringFormControl { +) : SimplePanel(setOf("form-group")), StringFormControl, ObservableState<String?> { /** * A list of options (value to label pairs) for the select control. @@ -272,6 +273,12 @@ open class Select( input.blur() } + override fun getState(): String? = input.getState() + + override fun subscribe(observer: (String?) -> Unit): () -> Unit { + return input.subscribe(observer) + } + companion object { internal var counter = 0 } diff --git a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt index ced0945d..5cba791c 100644 --- a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -33,6 +33,7 @@ import pl.treksoft.kvision.form.InputSize import pl.treksoft.kvision.form.ValidationStatus import pl.treksoft.kvision.html.ButtonStyle import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.asString import pl.treksoft.kvision.utils.obj import pl.treksoft.kvision.utils.set @@ -72,7 +73,9 @@ open class SelectInput( options: List<StringPair>? = null, value: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, classes: Set<String> = setOf() -) : SimplePanel(classes), FormInput { +) : SimplePanel(classes), FormInput, ObservableState<String?> { + + protected val observers = mutableListOf<(String?) -> Unit>() /** * A list of options (value to label pairs) for the select control. @@ -82,7 +85,7 @@ open class SelectInput( /** * A value of the selected option. */ - var value by refreshOnUpdate(value) { refreshState() } + var value by refreshOnUpdate(value) { refreshState(); observers.forEach { ob -> ob(it) } } /** * The name attribute of the generated HTML select element. @@ -396,6 +399,16 @@ open class SelectInput( override fun blur() { getElementJQuery()?.blur() } + + override fun getState(): String? = value + + override fun subscribe(observer: (String?) -> Unit): () -> Unit { + observers += observer + observer(value) + return { + observers -= observer + } + } } /** 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 + } + } } /** diff --git a/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt b/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt index 7424159f..79f4d1a0 100644 --- a/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt +++ b/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt @@ -27,6 +27,7 @@ import org.w3c.dom.HTMLElement import pl.treksoft.kvision.KVManagerReact import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.set import react.RBuilder import react.dom.render as ReactRender @@ -41,12 +42,15 @@ class React<S>( state: S, classes: Set<String> = setOf(), private val builder: RBuilder.(getState: () -> S, changeState: ((S) -> S) -> Unit) -> Unit -) : Widget(classes) { +) : Widget(classes), ObservableState<S> { + + private val observers = mutableListOf<(S) -> Unit>() var state = state set(value) { field = value refreshFunction?.invoke() + observers.forEach { it(state) } } private var refreshFunction: (() -> Unit)? = null @@ -76,6 +80,16 @@ class React<S>( KVManagerReact.reactDom.unmountComponentAtNode(it) } } + + override fun getState(): S = state + + override fun subscribe(observer: (S) -> Unit): () -> Unit { + observers += observer + observer(state) + return { + observers -= observer + } + } } /** |