From 474195d3aa862686712cfe6c800dc43f8fee8ec5 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 19 Dec 2018 12:14:03 +0100 Subject: An addon remote module for select component. --- build.gradle | 1 + .../kvision/remote/SpringServiceManager.kt | 8 +++ .../treksoft/kvision/remote/SpringRemoteAgent.kt | 36 ++++++++----- .../kvision/remote/SpringServiceManager.kt | 24 ++++++--- kvision-modules/kvision-select-remote/build.gradle | 14 +++++ .../package.json.d/project.info | 3 ++ .../kvision/form/select/RemoteSelectInput.kt | 60 ++++++++++++++++++++++ .../pl/treksoft/kvision/form/select/AjaxOptions.kt | 21 +++++--- .../kvision/remote/SpringServiceManager.kt | 10 ++++ .../kvision/remote/SpringServiceManager.kt | 48 +++++++++++++++++ settings.gradle | 1 + 11 files changed, 202 insertions(+), 24 deletions(-) create mode 100644 kvision-modules/kvision-select-remote/build.gradle create mode 100644 kvision-modules/kvision-select-remote/package.json.d/project.info create mode 100644 kvision-modules/kvision-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/RemoteSelectInput.kt diff --git a/build.gradle b/build.gradle index 20ef7bb6..fe6217b7 100644 --- a/build.gradle +++ b/build.gradle @@ -161,6 +161,7 @@ dokka { 'kvision-modules/kvision-handlebars/src/main/kotlin', 'kvision-modules/kvision-i18n/src/main/kotlin', 'kvision-modules/kvision-remote/src/main/kotlin', + 'kvision-modules/kvision-select-remote/src/main/kotlin', 'kvision-modules/kvision-common/src/main/kotlin', 'kvision-modules/kvision-common-types/src/main/kotlin', 'kvision-modules/kvision-server-jooby/src/main/kotlin', diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt index 90926656..bc45d72b 100644 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt @@ -99,4 +99,12 @@ expect open class SpringServiceManager(serviceClass: KClass) : Servi route: String? = null, method: RpcHttpMethod = RpcHttpMethod.POST ) + + /** + * Binds a given function of the receiver as a select options source + * @param function a function of the receiver + */ + protected fun bind( + function: T.(String) -> List + ) } diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt index fea16b99..2f6b765a 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt @@ -44,7 +44,8 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call(noinline function: suspend T.() -> RET): RET { val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] + ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { try { @Suppress("UNCHECKED_CAST") @@ -67,7 +68,8 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager List ): List { val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] + ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { try { deserializeList(it, RET::class.js.name) @@ -90,7 +92,8 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager(val serviceManager: SpringServiceManager { val data = serialize(p) val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") + serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] + ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, listOf(data), method).then { try { deserializeList(it, RET::class.js.name) @@ -138,7 +142,8 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager(val serviceManager: SpringServiceManager(it, RET::class.js.name) @@ -188,7 +194,8 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager(val serviceManager: SpringServiceManager(it, RET::class.js.name) @@ -240,7 +248,8 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager(val serviceManager: SpringServiceManager(it, RET::class.js.name) @@ -305,7 +315,8 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager(val serviceManager: SpringServiceManager(it, RET::class.js.name) diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt index 524347d7..61ae32ba 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt @@ -42,7 +42,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } /** @@ -56,7 +56,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } /** @@ -70,7 +70,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } /** @@ -84,7 +84,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } /** @@ -98,7 +98,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) } /** @@ -114,11 +114,23 @@ actual open class SpringServiceManager actual constructor(serviceClass: method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", method) + } + + /** + * Binds a given function of the receiver as a select options source + * @param function a function of the receiver + */ + protected actual fun bind( + function: T.(String) -> List + ) { + val routeDef = "route${this::class.simpleName}${counter++}" + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", RpcHttpMethod.POST) } /** * Returns the map of defined paths. */ override fun getCalls(): Map> = calls + } diff --git a/kvision-modules/kvision-select-remote/build.gradle b/kvision-modules/kvision-select-remote/build.gradle new file mode 100644 index 00000000..da912bf0 --- /dev/null +++ b/kvision-modules/kvision-select-remote/build.gradle @@ -0,0 +1,14 @@ +apply from: "../shared.gradle" + +dependencies { + compile project(":kvision-modules:kvision-select") + compile project(":kvision-modules:kvision-remote") +} + +kotlinFrontend { + + npm { + dependency("jquery-deparam", "0.5.3") + } + +} diff --git a/kvision-modules/kvision-select-remote/package.json.d/project.info b/kvision-modules/kvision-select-remote/package.json.d/project.info new file mode 100644 index 00000000..5685d581 --- /dev/null +++ b/kvision-modules/kvision-select-remote/package.json.d/project.info @@ -0,0 +1,3 @@ +{ + "description": "KVision Select remote addon module" +} diff --git a/kvision-modules/kvision-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/RemoteSelectInput.kt b/kvision-modules/kvision-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/RemoteSelectInput.kt new file mode 100644 index 00000000..5986d1fe --- /dev/null +++ b/kvision-modules/kvision-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/RemoteSelectInput.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.form.select + +import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.list +import kotlinx.serialization.stringify +import pl.treksoft.kvision.remote.JsonRpcRequest +import pl.treksoft.kvision.remote.RemoteSelectOption +import pl.treksoft.kvision.remote.SpringServiceManager +import pl.treksoft.kvision.utils.JSON +import pl.treksoft.kvision.utils.obj +import kotlin.js.JSON as NativeJSON + +external fun decodeURIComponent(encodedURI: String): String + +@UseExperimental(ImplicitReflectionSerializer::class) +open class RemoteSelectInput( + value: String? = null, + serviceManager: SpringServiceManager, + function: T.(String) -> List, + multiple: Boolean = false, + classes: Set = setOf() +) : SelectInput(null, value, multiple, null, classes) { + init { + val (url, method) = + serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] + ?: throw IllegalStateException("Function not specified!") + val data = obj { + q = "{{{q}}}" + } + ajaxOptions = AjaxOptions(url, preprocessData = { + JSON.plain.parse(RemoteSelectOption.serializer().list, it.result as String) + }, data = data, beforeSend = { _, b -> + val q = decodeURIComponent(b.data.substring(2)) + b.data = JSON.plain.stringify(JsonRpcRequest(0, url, listOf(q))) + true + }, httpType = HttpType.valueOf(method.name)) + } + +} diff --git a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt index 212dc456..fb1ad2fd 100644 --- a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt +++ b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt @@ -77,12 +77,19 @@ enum class DataType(internal val type: String) { * option */ data class AjaxOptions( - val url: String, val preprocessData: (dynamic) -> dynamic, val beforeSend: ((JQueryXHR) -> dynamic)? = null, - val data: dynamic = null, val httpType: HttpType = HttpType.GET, - val dataType: DataType = DataType.JSON, val minLength: Int = 0, - val cache: Boolean = true, val clearOnEmpty: Boolean = true, val clearOnError: Boolean = true, + val url: String, + val preprocessData: (dynamic) -> dynamic, + val beforeSend: ((JQueryXHR, dynamic) -> dynamic)? = null, + val data: dynamic = null, + val httpType: HttpType = HttpType.GET, + val dataType: DataType = DataType.JSON, + val minLength: Int = 0, + val cache: Boolean = true, + val clearOnEmpty: Boolean = true, + val clearOnError: Boolean = true, val emptyRequest: Boolean = false, - val requestDelay: Int = AJAX_REQUEST_DELAY, val restoreOnError: Boolean = false + val requestDelay: Int = AJAX_REQUEST_DELAY, + val restoreOnError: Boolean = false ) /** @@ -109,10 +116,11 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { return obj { this.ajax = obj { this.url = url - this.type = httpType.type + this.method = httpType.type this.dataType = dataType.type this.data = data this.beforeSend = beforeSend + this.contentType = "application/json" } this.preprocessData = procData this.minLength = minLength @@ -124,5 +132,6 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { this.requestDelay = requestDelay this.restoreOnError = restoreOnError this.langCode = language + this.processData = false } } diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt index 5b566423..dae150ba 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt @@ -113,4 +113,14 @@ actual open class SpringServiceManager actual constructor(val serviceCl throw IllegalStateException("This class is for Spring Boot integration.") } + /** + * Binds a given function of the receiver as a select options source + * @param function a function of the receiver + */ + protected actual fun bind( + function: T.(String) -> List + ) { + throw IllegalStateException("This class is for Spring Boot integration.") + } + } diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt index a241842c..162b92dd 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt @@ -373,6 +373,54 @@ actual open class SpringServiceManager actual constructor(val serviceCl } } + /** + * Binds a given function of the receiver as a select options source + * @param function a function of the receiver + */ + @Suppress("TooGenericExceptionCaught") + protected actual fun bind( + function: T.(String) -> List + ) { + val routeDef = "route${this::class.simpleName}${counter++}" + addRoute(RpcHttpMethod.POST, "/kv/$routeDef") { req, res -> + val service = SpringContext.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) + if (jsonRpcRequest.params.size == 1) { + val param = getParameter(jsonRpcRequest.params[0]) + try { + val result = function.invoke(service, param) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + fun addRoute( method: RpcHttpMethod, path: String, diff --git a/settings.gradle b/settings.gradle index c3360ffe..9adc0080 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,5 +12,6 @@ include 'kvision-modules:kvision-base', 'kvision-modules:kvision-handlebars', 'kvision-modules:kvision-i18n', 'kvision-modules:kvision-remote', + 'kvision-modules:kvision-select-remote', 'kvision-modules:kvision-server-jooby', 'kvision-modules:kvision-server-spring-boot' -- cgit