From 69958b1a08f46aecd6ceda54780f362019150052 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Tue, 22 Oct 2019 23:18:59 +0200 Subject: Send additional state with SelectRemote request. --- .../pl/treksoft/kvision/form/select/SelectRemote.kt | 11 +++++++---- .../pl/treksoft/kvision/form/select/SelectRemoteInput.kt | 15 ++++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) (limited to 'kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin') 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 930bea32..1cabb5dd 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 @@ -40,6 +40,7 @@ import pl.treksoft.kvision.utils.SnOn * @param value selected value * @param serviceManager multiplatform service manager * @param function multiplatform service method returning the list of options + * @param stateFunction a function to generate the state object passed with the remote request * @param name the name attribute of the generated HTML input element * @param multiple allows multiple value selection (multiple values are comma delimited) * @param ajaxOptions additional options for remote data source @@ -50,7 +51,8 @@ import pl.treksoft.kvision.utils.SnOn open class SelectRemote( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?) -> List, + function: T.(String?, String?, String?) -> List, + stateFunction: (() -> String)? = null, name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, @@ -148,7 +150,7 @@ open class SelectRemote( private val idc = "kv_form_SelectRemote_$counter" final override val input: SelectRemoteInput = SelectRemoteInput( - value, serviceManager, function, multiple, ajaxOptions, + value, serviceManager, function, stateFunction, multiple, ajaxOptions, setOf("form-control") ).apply { this.id = idc @@ -256,8 +258,8 @@ open class SelectRemote( fun Container.selectRemote( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?) -> List, name: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, + function: T.(String?, String?, String?) -> List, stateFunction: (() -> String)? = null, + name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, rich: Boolean = false, init: (SelectRemote.() -> Unit)? = null ): SelectRemote { val selectRemote = @@ -265,6 +267,7 @@ fun Container.selectRemote( value, serviceManager, function, + stateFunction, name, multiple, ajaxOptions, diff --git a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt index 5a795c9c..e44c63d5 100644 --- a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt +++ b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt @@ -45,6 +45,7 @@ external fun decodeURIComponent(encodedURI: String): String * @param value selected value * @param serviceManager multiplatform service manager * @param function multiplatform service method returning the list of options + * @param stateFunction a function to generate the state object passed with the remote request * @param multiple allows multiple value selection (multiple values are comma delimited) * @param ajaxOptions additional options for remote data source * @param classes a set of CSS class names @@ -53,7 +54,8 @@ external fun decodeURIComponent(encodedURI: String): String open class SelectRemoteInput( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?) -> List, + function: T.(String?, String?, String?) -> List, + stateFunction: (() -> String)? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, classes: Set = setOf() @@ -85,15 +87,17 @@ open class SelectRemoteInput( }, data = data, beforeSend = { _, b -> @Suppress("UnsafeCastFromDynamic") val q = decodeURIComponent(b.data.substring(2)) - b.data = JSON.plain.stringify(JsonRpcRequest(0, url, listOf(q, this.value))) + val state = stateFunction?.invoke() + b.data = JSON.plain.stringify(JsonRpcRequest(0, url, listOf(q, this.value, state))) true }, httpType = HttpType.valueOf(method.name), cache = false, preserveSelected = true) if (value != null) { GlobalScope.launch { val callAgent = CallAgent() + val state = stateFunction?.invoke() val initials = callAgent.remoteCall( url, - JSON.plain.stringify(JsonRpcRequest(0, url, listOf(null, value))), + JSON.plain.stringify(JsonRpcRequest(0, url, listOf(null, value, state))), HttpMethod.POST ).asDeferred().await() JSON.plain.parse(RemoteOption.serializer().list, initials.result as String).map { @@ -123,13 +127,14 @@ open class SelectRemoteInput( fun Container.selectRemoteInput( value: String? = null, serviceManager: KVServiceManager, - function: T.(String?, String?) -> List, + function: T.(String?, String?, String?) -> List, + stateFunction: (() -> String)? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, classes: Set = setOf(), init: (SelectRemoteInput.() -> Unit)? = null ): SelectRemoteInput { val selectRemoteInput = - SelectRemoteInput(value, serviceManager, function, multiple, ajaxOptions, classes).apply { + SelectRemoteInput(value, serviceManager, function, stateFunction, multiple, ajaxOptions, classes).apply { init?.invoke(this) } this.add(selectRemoteInput) -- cgit