From a804e0aa277efc1b46c1111b0ad8302d267684b2 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 13 Jul 2019 00:21:33 +0200 Subject: Refactor RemoteSelect -> SelectRemote and RemoteTabulator -> TabulatorRemote --- .../treksoft/kvision/tabulator/RemoteTabulator.kt | 114 --------------------- .../treksoft/kvision/tabulator/TabulatorRemote.kt | 114 +++++++++++++++++++++ 2 files changed, 114 insertions(+), 114 deletions(-) delete mode 100644 kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt create mode 100644 kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.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 deleted file mode 100644 index 8070a187..00000000 --- a/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt +++ /dev/null @@ -1,114 +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.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-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.kt b/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.kt new file mode 100644 index 00000000..4478b942 --- /dev/null +++ b/kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/TabulatorRemote.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 TabulatorRemote( + 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.tabulatorRemote( + serviceManager: KVServiceManager, + function: E.(Int?, Int?, List?, List?) -> RemoteData, + options: TabulatorOptions = TabulatorOptions(), + types: Set = setOf(), + classes: Set = setOf(), + init: (TabulatorRemote.() -> Unit)? = null + ): TabulatorRemote { + val tabulatorRemote = TabulatorRemote(serviceManager, function, options, types, classes) + init?.invoke(tabulatorRemote) + this.add(tabulatorRemote) + return tabulatorRemote + } + } + +} -- cgit