diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-10-22 23:18:59 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-10-22 23:18:59 +0200 |
commit | 69958b1a08f46aecd6ceda54780f362019150052 (patch) | |
tree | f42d65c65b832a9aef1a7e2e40f8bf8ac6e7224d /kvision-modules/kvision-bootstrap-select-remote/src | |
parent | 5f109a1e92cd02df8b5588743ed141bc93542817 (diff) | |
download | kvision-69958b1a08f46aecd6ceda54780f362019150052.tar.gz kvision-69958b1a08f46aecd6ceda54780f362019150052.tar.bz2 kvision-69958b1a08f46aecd6ceda54780f362019150052.zip |
Send additional state with SelectRemote request.
Diffstat (limited to 'kvision-modules/kvision-bootstrap-select-remote/src')
2 files changed, 17 insertions, 9 deletions
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<T : Any>( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?) -> List<RemoteOption>, + function: T.(String?, String?, String?) -> List<RemoteOption>, + stateFunction: (() -> String)? = null, name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, @@ -148,7 +150,7 @@ open class SelectRemote<T : Any>( private val idc = "kv_form_SelectRemote_$counter" final override val input: SelectRemoteInput<T> = 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<T : Any>( fun <T : Any> Container.selectRemote( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?) -> List<RemoteOption>, name: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, + function: T.(String?, String?, String?) -> List<RemoteOption>, stateFunction: (() -> String)? = null, + name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, rich: Boolean = false, init: (SelectRemote<T>.() -> Unit)? = null ): SelectRemote<T> { val selectRemote = @@ -265,6 +267,7 @@ fun <T : Any> 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<T : Any>( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?) -> List<RemoteOption>, + function: T.(String?, String?, String?) -> List<RemoteOption>, + stateFunction: (() -> String)? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, classes: Set<String> = setOf() @@ -85,15 +87,17 @@ open class SelectRemoteInput<T : Any>( }, 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<T : Any>( fun <T : Any> Container.selectRemoteInput( value: String? = null, serviceManager: KVServiceManager<T>, - function: T.(String?, String?) -> List<RemoteOption>, + function: T.(String?, String?, String?) -> List<RemoteOption>, + stateFunction: (() -> String)? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, classes: Set<String> = setOf(), init: (SelectRemoteInput<T>.() -> Unit)? = null ): SelectRemoteInput<T> { 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) |