diff options
author | Robert Jaros <rjaros@finn.pl> | 2018-12-19 12:14:03 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2018-12-19 12:14:03 +0100 |
commit | 474195d3aa862686712cfe6c800dc43f8fee8ec5 (patch) | |
tree | 2c89307d8740b3efa7648061dbeb57a822c40e6d | |
parent | 161264957dc1b41cd6716ee7777139c5e29589f5 (diff) | |
download | kvision-474195d3aa862686712cfe6c800dc43f8fee8ec5.tar.gz kvision-474195d3aa862686712cfe6c800dc43f8fee8ec5.tar.bz2 kvision-474195d3aa862686712cfe6c800dc43f8fee8ec5.zip |
An addon remote module for select component.
11 files changed, 202 insertions, 24 deletions
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<T : Any>(serviceClass: KClass<T>) : 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<RemoteSelectOption> + ) } 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<T : Any>(val serviceManager: SpringServiceManager<T */ suspend inline fun <reified RET : Any, T> 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<T : Any>(val serviceManager: SpringServiceManager<T noinline function: suspend T.() -> List<RET> ): List<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 { deserializeList<RET>(it, RET::class.js.name) @@ -90,7 +92,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T ): RET { 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 { @Suppress("UNCHECKED_CAST") @@ -114,7 +117,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T ): List<RET> { 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<RET>(it, RET::class.js.name) @@ -138,7 +142,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data1 = serialize(p1) val data2 = serialize(p2) 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(data1, data2), method).then { try { @Suppress("UNCHECKED_CAST") @@ -163,7 +168,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data1 = serialize(p1) val data2 = serialize(p2) 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(data1, data2), method).then { try { deserializeList<RET>(it, RET::class.js.name) @@ -188,7 +194,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data2 = serialize(p2) val data3 = serialize(p3) 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(data1, data2, data3), method).then { try { @Suppress("UNCHECKED_CAST") @@ -214,7 +221,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data2 = serialize(p2) val data3 = serialize(p3) 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(data1, data2, data3), method).then { try { deserializeList<RET>(it, RET::class.js.name) @@ -240,7 +248,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data3 = serialize(p3) val data4 = serialize(p4) 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(data1, data2, data3, data4), method).then { try { @Suppress("UNCHECKED_CAST") @@ -271,7 +280,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data3 = serialize(p3) val data4 = serialize(p4) 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(data1, data2, data3, data4), method).then { try { deserializeList<RET>(it, RET::class.js.name) @@ -305,7 +315,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data4 = serialize(p4) val data5 = serialize(p5) 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(data1, data2, data3, data4, data5), method).then { try { @Suppress("UNCHECKED_CAST") @@ -340,7 +351,8 @@ open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T val data4 = serialize(p4) val data5 = serialize(p5) 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(data1, data2, data3, data4, data5), method).then { try { deserializeList<RET>(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<T : Any> 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<T : Any> 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<T : Any> 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<T : Any> 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<T : Any> 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<T : Any> 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<RemoteSelectOption> + ) { + 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<String, Pair<String, RpcHttpMethod>> = 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<T : Any>( + value: String? = null, + serviceManager: SpringServiceManager<T>, + function: T.(String) -> List<RemoteSelectOption>, + multiple: Boolean = false, + classes: Set<String> = 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<T : Any> 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<RemoteSelectOption> + ) { + 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<T : Any> 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<RemoteSelectOption> + ) { + 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<String>(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' |