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. --- .../treksoft/kvision/tabulator/RemoteTabulator.kt | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt (limited to 'kvision-modules/kvision-tabulator-remote/src/main') 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 + } + } + +} -- cgit