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 | |
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')
7 files changed, 31 insertions, 20 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) diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 006aba89..9f07948d 100644 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -115,7 +115,7 @@ expect open class KVServiceManager<T : Any>(serviceClass: KClass<T>) { * @param function a function of the receiver */ protected fun bind( - function: T.(String?, String?) -> List<RemoteOption> + function: T.(String?, String?, String?) -> List<RemoteOption> ) /** diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 829ccbd6..dd54a979 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -133,7 +133,7 @@ actual open class KVServiceManager<T : Any> actual constructor(serviceClass: KCl * @param function a function of the receiver */ protected actual fun bind( - function: T.(String?, String?) -> List<RemoteOption> + function: T.(String?, String?, String?) -> List<RemoteOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", HttpMethod.POST) diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index e2a26967..fd95be3e 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -417,20 +417,21 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: */ @Suppress("TooGenericExceptionCaught") protected actual fun bind( - function: T.(String?, String?) -> List<RemoteOption> + function: T.(String?, String?, String?) -> List<RemoteOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" routes.add { call(HttpMethod.POST, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) - if (jsonRpcRequest.params.size == 2) { + if (jsonRpcRequest.params.size == 3) { val param1 = getParameter<String?>(jsonRpcRequest.params[0]) val param2 = getParameter<String?>(jsonRpcRequest.params[1]) + val param3 = getParameter<String?>(jsonRpcRequest.params[2]) val injector = req.require(Injector::class.java) val service = injector.getInstance(serviceClass.java) GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { - val result = function.invoke(service, param1, param2) + val result = function.invoke(service, param1, param2, param3) res.send( JsonRpcResponse( id = jsonRpcRequest.id, diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 36a7897e..e550be9c 100644 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -443,17 +443,18 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: */ @Suppress("TooGenericExceptionCaught") protected actual fun bind( - function: T.(String?, String?) -> List<RemoteOption> + function: T.(String?, String?, String?) -> List<RemoteOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { val service = call.injector.createChildInjector(DummyWsSessionModule()).getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() - if (jsonRpcRequest.params.size == 2) { + if (jsonRpcRequest.params.size == 3) { val param1 = getParameter<String?>(jsonRpcRequest.params[0]) val param2 = getParameter<String?>(jsonRpcRequest.params[1]) + val param3 = getParameter<String?>(jsonRpcRequest.params[2]) try { - val result = function.invoke(service, param1, param2) + val result = function.invoke(service, param1, param2, param3) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 14360abc..25ad52ed 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -507,18 +507,19 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: */ @Suppress("TooGenericExceptionCaught") protected actual fun bind( - function: T.(String?, String?) -> List<RemoteOption> + function: T.(String?, String?, String?) -> List<RemoteOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(HttpMethod.POST, "/kv/$routeDef") { req, ctx -> val service = ctx.getBean(serviceClass.java) initializeService(service, req) val jsonRpcRequest = req.awaitBody<JsonRpcRequest>() - if (jsonRpcRequest.params.size == 2) { + if (jsonRpcRequest.params.size == 3) { val param1 = getParameter<String?>(jsonRpcRequest.params[0]) val param2 = getParameter<String?>(jsonRpcRequest.params[1]) + val param3 = getParameter<String?>(jsonRpcRequest.params[2]) try { - val result = function.invoke(service, param1, param2) + val result = function.invoke(service, param1, param2, param3) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( |