aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-07-08 12:43:17 +0200
committerRobert Jaros <rjaros@finn.pl>2019-07-08 12:43:17 +0200
commit0acabe1d44111a3a118116d526b415572caf8050 (patch)
treee252f8ddd28687a36477d7fdd125686e82f85386
parentc0a51a9be91b8f438ec7605558465bf8a8aac404 (diff)
downloadkvision-0acabe1d44111a3a118116d526b415572caf8050.tar.gz
kvision-0acabe1d44111a3a118116d526b415572caf8050.tar.bz2
kvision-0acabe1d44111a3a118116d526b415572caf8050.zip
New tabulator-remote module.
-rw-r--r--build.gradle1
-rw-r--r--kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt8
-rw-r--r--kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt (renamed from kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteSelectOption.kt)9
-rw-r--r--kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt11
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt40
-rw-r--r--kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt45
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt51
-rw-r--r--kvision-modules/kvision-tabulator-remote/build.gradle6
-rw-r--r--kvision-modules/kvision-tabulator-remote/package.json.d/project.info3
-rw-r--r--kvision-modules/kvision-tabulator-remote/src/main/kotlin/pl/treksoft/kvision/tabulator/RemoteTabulator.kt114
-rw-r--r--kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt4
-rw-r--r--settings.gradle1
12 files changed, 291 insertions, 2 deletions
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
@@ -119,6 +119,14 @@ expect open class KVServiceManager<T : Any>(serviceClass: KClass<T>) {
)
/**
+ * Binds a given function of the receiver as a tabulator component source
+ * @param function a function of the receiver
+ */
+ protected inline fun <reified RET> bind(
+ noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET>
+ )
+
+ /**
* 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/RemoteTypes.kt
index 24e68f63..7285295b 100644
--- 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/RemoteTypes.kt
@@ -34,3 +34,12 @@ data class RemoteSelectOption(
val disabled: Boolean = false,
val divider: Boolean = false
)
+
+@Serializable
+data class RemoteData<T>(val data: List<T> = 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
@@ -140,6 +140,17 @@ actual open class KVServiceManager<T : Any> actual constructor(serviceClass: KCl
}
/**
+ * Binds a given function of the receiver as a tabulator component source
+ * @param function a function of the receiver
+ */
+ protected actual inline fun <reified RET> bind(
+ noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET>
+ ) {
+ 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
* @param route a web socket route
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
@@ -429,6 +429,46 @@ actual open class KVServiceManager<T : Any> 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 <reified RET> bind(
+ noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET>
+ ) {
+ 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<Int?>(jsonRpcRequest.params[0])
+ val param2 = getParameter<Int?>(jsonRpcRequest.params[1])
+ val param3 = getParameter<List<RemoteFilter>?>(jsonRpcRequest.params[2])
+ val param4 = getParameter<List<RemoteSorter>?>(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
*/
fun call(
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
@@ -457,6 +457,51 @@ actual open class KVServiceManager<T : Any> 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 <reified RET> bind(
+ noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET>
+ ) {
+ 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<JsonRpcRequest>()
+ if (jsonRpcRequest.params.size == 4) {
+ val param1 = getParameter<Int?>(jsonRpcRequest.params[0])
+ val param2 = getParameter<Int?>(jsonRpcRequest.params[1])
+ val param3 = getParameter<List<RemoteFilter>?>(jsonRpcRequest.params[2])
+ val param4 = getParameter<List<RemoteSorter>?>(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
*/
fun addRoute(
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
@@ -499,6 +499,57 @@ actual open class KVServiceManager<T : Any> 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 <reified RET> bind(
+ noinline function: T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<RET>
+ ) {
+ 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<Int?>(jsonRpcRequest.params[0])
+ val param2 = getParameter<Int?>(jsonRpcRequest.params[1])
+ val param3 = getParameter<List<RemoteFilter>?>(jsonRpcRequest.params[2])
+ val param4 = getParameter<List<RemoteSorter>?>(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
*/
fun addRoute(
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<T : Any, E : Any>(
+ serviceManager: KVServiceManager<E>,
+ function: E.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<T>,
+ options: TabulatorOptions<T> = TabulatorOptions(),
+ types: Set<TableType> = setOf(),
+ classes: Set<String> = setOf()
+) : Tabulator<T>(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<dynamic>(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 <T : Any, E : Any> Container.remoteTabulator(
+ serviceManager: KVServiceManager<E>,
+ function: E.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?) -> RemoteData<T>,
+ options: TabulatorOptions<T> = TabulatorOptions(),
+ types: Set<TableType> = setOf(),
+ classes: Set<String> = setOf(),
+ init: (RemoteTabulator<T, E>.() -> Unit)? = null
+ ): RemoteTabulator<T, E> {
+ 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<T : Any>(
val scrollToRowIfVisible: Boolean? = null,
val index: String? = null,
@Suppress("ArrayInDataClass") var data: Array<T>? = 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<Any>)? = null,
+ var ajaxRequestFunc: ((url: String, config: dynamic, params: dynamic) -> Promise<Any>)? = 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',