From 0acabe1d44111a3a118116d526b415572caf8050 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 8 Jul 2019 12:43:17 +0200 Subject: New tabulator-remote module. --- build.gradle | 1 + .../pl/treksoft/kvision/remote/KVServiceManager.kt | 8 ++ .../treksoft/kvision/remote/RemoteSelectOption.kt | 36 ------- .../pl/treksoft/kvision/remote/RemoteTypes.kt | 45 ++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 11 ++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 40 ++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 45 ++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 51 +++++++++ .../kvision-tabulator-remote/build.gradle | 6 ++ .../package.json.d/project.info | 3 + .../treksoft/kvision/tabulator/RemoteTabulator.kt | 114 +++++++++++++++++++++ .../pl/treksoft/kvision/tabulator/Options.kt | 4 +- settings.gradle | 1 + 13 files changed, 327 insertions(+), 38 deletions(-) delete mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt create mode 100644 kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt create mode 100644 kvision-modules/kvision-tabulator-remote/build.gradle create mode 100644 kvision-modules/kvision-tabulator-remote/package.json.d/project.info create mode 100644 kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt diff --git a/build.gradle b/build.gradle index 180406e4..bfeea65d 100644 --- a/build.gradle +++ b/build.gradle @@ -175,6 +175,7 @@ dokka { 'kvision-modules/kvision-pace/src/main/kotlin', 'kvision-modules/kvision-remote/src/main/kotlin', 'kvision-modules/kvision-select-remote/src/main/kotlin', + 'kvision-modules/kvision-tabulator-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/KVServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index e89744f6..786143e8 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 @@ -118,6 +118,14 @@ expect open class KVServiceManager(serviceClass: KClass) { function: T.(String?, String?) -> List ) + /** + * Binds a given function of the receiver as a tabulator component source + * @param function a function of the receiver + */ + protected inline fun bind( + noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + ) + /** * Binds a given function of the receiver as a web socket connection * @param function a function of the receiver diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt deleted file mode 100644 index 24e68f63..00000000 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.remote - -import kotlinx.serialization.Serializable - -@Serializable -data class RemoteSelectOption( - val value: String? = null, - val text: String? = null, - val className: String? = null, - val subtext: String? = null, - val icon: String? = null, - val content: String? = null, - val disabled: Boolean = false, - val divider: Boolean = false -) diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt new file mode 100644 index 00000000..7285295b --- /dev/null +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt @@ -0,0 +1,45 @@ +/* + * 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.remote + +import kotlinx.serialization.Serializable + +@Serializable +data class RemoteSelectOption( + val value: String? = null, + val text: String? = null, + val className: String? = null, + val subtext: String? = null, + val icon: String? = null, + val content: String? = null, + val disabled: Boolean = false, + val divider: Boolean = false +) + +@Serializable +data class RemoteData(val data: List = listOf(), val last_page: Int = 0) + +@Serializable +data class RemoteFilter(val field: String, val type: String, val value: String?) + +@Serializable +data class RemoteSorter(val field: String, val dir: String) 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 7db690b2..5b5ae87f 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 @@ -139,6 +139,17 @@ actual open class KVServiceManager actual constructor(serviceClass: KCl calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", HttpMethod.POST) } + /** + * Binds a given function of the receiver as a tabulator component source + * @param function a function of the receiver + */ + protected actual inline fun bind( + noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + ) { + val routeDef = "route${this::class.simpleName}${counter++}" + calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", HttpMethod.POST) + } + /** * Binds a given web socket connetion with a function of the receiver. * @param function a function of the receiver 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 c3c179fc..a2bb2386 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 @@ -428,6 +428,46 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given function of the receiver as a tabulator component source + * @param function a function of the receiver + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + ) { + 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 == 4) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter?>(jsonRpcRequest.params[2]) + val param4 = getParameter?>(jsonRpcRequest.params[3]) + 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, param3, param4) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } + } + } else { + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) + } + }.invoke(this) + } + } + /** * @suppress Internal method */ 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 3c5391b6..c5c579a6 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 @@ -456,6 +456,51 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given function of the receiver as a tabulator component source + * @param function a function of the receiver + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + ) { + 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() + if (jsonRpcRequest.params.size == 4) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter?>(jsonRpcRequest.params[2]) + val param4 = getParameter?>(jsonRpcRequest.params[3]) + try { + val result = function.invoke(service, param1, param2, param3, param4) + call.respond( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + call.respond( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + } + } else { + call.respond( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + } + } + } + /** * @suppress Internal method */ 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 d2b4f7ea..ab64c50d 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 @@ -498,6 +498,57 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given function of the receiver as a tabulator component source + * @param function a function of the receiver + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + ) { + val routeDef = "route${this::class.simpleName}${counter++}" + addRoute(HttpMethod.POST, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) + if (jsonRpcRequest.params.size == 4) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter?>(jsonRpcRequest.params[2]) + val param4 = getParameter?>(jsonRpcRequest.params[3]) + try { + val result = function.invoke(service, param1, param2, param3, param4) + 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" + ) + ) + ) + } + } + } + /** * @suppress internal function */ diff --git a/kvision-modules/kvision-tabulator-remote/build.gradle b/kvision-modules/kvision-tabulator-remote/build.gradle new file mode 100644 index 00000000..d498e275 --- /dev/null +++ b/kvision-modules/kvision-tabulator-remote/build.gradle @@ -0,0 +1,6 @@ +apply from: "../shared.gradle" + +dependencies { + compile project(":kvision-modules:kvision-tabulator") + compile project(":kvision-modules:kvision-remote") +} diff --git a/kvision-modules/kvision-tabulator-remote/package.json.d/project.info b/kvision-modules/kvision-tabulator-remote/package.json.d/project.info new file mode 100644 index 00000000..c6af34cc --- /dev/null +++ b/kvision-modules/kvision-tabulator-remote/package.json.d/project.info @@ -0,0 +1,3 @@ +{ + "description": "KVision Tabulator remote addon module" +} diff --git a/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt b/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt new file mode 100644 index 00000000..8070a187 --- /dev/null +++ b/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt @@ -0,0 +1,114 @@ +/* + * 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.tabulator + +import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.stringify +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.remote.JsonRpcRequest +import pl.treksoft.kvision.remote.KVServiceManager +import pl.treksoft.kvision.remote.RemoteData +import pl.treksoft.kvision.remote.RemoteFilter +import pl.treksoft.kvision.remote.RemoteSorter +import pl.treksoft.kvision.rest.HttpMethod +import pl.treksoft.kvision.rest.RestClient +import pl.treksoft.kvision.table.TableType +import pl.treksoft.kvision.utils.JSON + +/** + * Tabulator component connected to the multiplatform service. + * + * @constructor + * @param T type of row data + * @param E type of service manager + * @param serviceManager multiplatform service manager + * @param function multiplatform service method returning tabulator rows data + * @param options tabulator options + * @param types a set of table types + * @param classes a set of CSS class names + */ +@UseExperimental(ImplicitReflectionSerializer::class) +open class RemoteTabulator( + serviceManager: KVServiceManager, + function: E.(Int?, Int?, List?, List?) -> RemoteData, + options: TabulatorOptions = TabulatorOptions(), + types: Set = setOf(), + classes: Set = setOf() +) : Tabulator(null, false, options, types, classes) { + init { + val (url, method) = + serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] + ?: throw IllegalStateException("Function not specified!") + + val restClient = RestClient() + options.ajaxURL = url + options.ajaxRequestFunc = { _, _, params -> + val page = params.page + val size = params.size + @Suppress("UnsafeCastFromDynamic") + val filters = if (params.filters != null) { + kotlin.js.JSON.stringify(params.filters) + } else { + null + } + @Suppress("UnsafeCastFromDynamic") + val sorters = if (params.sorters != null) { + kotlin.js.JSON.stringify(params.sorters) + } else { + null + } + @Suppress("UnsafeCastFromDynamic") + val data = JSON.plain.stringify(JsonRpcRequest(0, url, listOf(page, size, filters, sorters))) + restClient.remoteCall(url, data, method = HttpMethod.valueOf(method.name)).then { r: dynamic -> + val result = kotlin.js.JSON.parse(r.result as String) + @Suppress("UnsafeCastFromDynamic") + if (page != null) { + result + } else { + result.data + } + } + } + } + + companion object { + /** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.remoteTabulator( + serviceManager: KVServiceManager, + function: E.(Int?, Int?, List?, List?) -> RemoteData, + options: TabulatorOptions = TabulatorOptions(), + types: Set = setOf(), + classes: Set = setOf(), + init: (RemoteTabulator.() -> Unit)? = null + ): RemoteTabulator { + val remoteTabulator = RemoteTabulator(serviceManager, function, options, types, classes) + init?.invoke(remoteTabulator) + this.add(remoteTabulator) + return remoteTabulator + } + } + +} diff --git a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt index a6f9c740..251c2f92 100644 --- a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt +++ b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt @@ -562,12 +562,12 @@ data class TabulatorOptions( val scrollToRowIfVisible: Boolean? = null, val index: String? = null, @Suppress("ArrayInDataClass") var data: Array? = null, - val ajaxURL: String? = null, + var ajaxURL: String? = null, val ajaxParams: dynamic = null, val ajaxConfig: dynamic = null, val ajaxContentType: dynamic = null, val ajaxURLGenerator: ((url: String, config: dynamic, params: dynamic) -> String)? = null, - val ajaxRequestFunc: ((url: String, config: dynamic, params: dynamic) -> Promise)? = null, + var ajaxRequestFunc: ((url: String, config: dynamic, params: dynamic) -> Promise)? = null, val ajaxFiltering: Boolean? = null, val ajaxSorting: Boolean? = null, val ajaxProgressiveLoad: ProgressiveMode? = null, diff --git a/settings.gradle b/settings.gradle index 20498616..c3be4fc2 100644 --- a/settings.gradle +++ b/settings.gradle @@ -20,6 +20,7 @@ include 'kvision-modules:kvision-base', 'kvision-modules:kvision-pace', 'kvision-modules:kvision-remote', 'kvision-modules:kvision-select-remote', + 'kvision-modules:kvision-tabulator-remote', 'kvision-modules:kvision-server-jooby', 'kvision-modules:kvision-server-ktor', 'kvision-modules:kvision-server-spring-boot', -- cgit