diff options
27 files changed, 1441 insertions, 232 deletions
@@ -70,6 +70,10 @@ CSS grid and Bootstrap responsive 12 columns gid). Bootstrap progress bar component. +# Package pl.treksoft.kvision.remote + +A set of components for creating multiplatform automatic connectivity with backend servers. + # Package pl.treksoft.kvision.routing Simple and easy to use JavaScript router wrapper. @@ -82,6 +86,10 @@ Clasess supporting HTML tables. Toolbar and button group components. +# Package pl.treksoft.kvision.types + +Multiplatform type definitions. + # Package pl.treksoft.kvision.utils Interfaces and helper functions for Snabbdom virtual dom implementation and a few useful extension functions. diff --git a/dokka/kvision-dokka-helper.jar b/dokka/kvision-dokka-helper.jar Binary files differindex ce048ea2..d600abd6 100644 --- a/dokka/kvision-dokka-helper.jar +++ b/dokka/kvision-dokka-helper.jar diff --git a/kvision-common/build.gradle b/kvision-common/build.gradle new file mode 100644 index 00000000..af3703e6 --- /dev/null +++ b/kvision-common/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'kotlin-platform-common' +apply plugin: 'kotlinx-serialization' + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion" + compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion" + compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion" + testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion" + testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion" +} diff --git a/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt b/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt new file mode 100644 index 00000000..918b1d1f --- /dev/null +++ b/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt @@ -0,0 +1,32 @@ +/* + * 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 + +/** + * A Jooby based server. + */ +expect open class JoobyServer + +/** + * A server request. + */ +expect interface Request diff --git a/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt new file mode 100644 index 00000000..4a6be56b --- /dev/null +++ b/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt @@ -0,0 +1,99 @@ +/* + * 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.coroutines.experimental.Deferred + +const val SERVICE_PREFIX = "/kv_service" + +/** + * Multiplatform service manager. + */ +expect open class ServiceManager<out T>(service: T? = null) { + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected inline fun <reified RET> bind(route: String, noinline function: T.(Request?) -> Deferred<RET>) + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected inline fun <reified PAR, reified RET> bind( + route: String, + noinline function: T.(PAR, Request?) -> Deferred<RET> + ) + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected inline fun <reified PAR1, reified PAR2, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET> + ) + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET> + ) + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET> + ) + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET> + ) + + /** + * Applies all defined routes to the given server. + * @param k a Jooby server + */ + fun applyRoutes(k: JoobyServer) + + /** + * Returns the list of defined bindings. + */ + fun getCalls(): Map<String, String> +} diff --git a/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt b/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt new file mode 100644 index 00000000..583323de --- /dev/null +++ b/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt @@ -0,0 +1,49 @@ +/* + * 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.types + +import kotlinx.serialization.Serializable + +expect val KDATE_FORMAT: String + +/** + * A serializable wrapper for a multiplatform Date type. + */ +@Serializable +data class KDate(val time: Long) { + constructor() : this(now().time) + constructor(str: String) : this(str.toKDateF(KDATE_FORMAT).time) + + override fun toString(): String { + return this.toStringF(KDATE_FORMAT) + } + + companion object { + fun now() = nowDate() + } +} + +internal expect fun nowDate(): KDate + +internal expect fun String.toKDateF(format: String): KDate + +internal expect fun KDate.toStringF(format: String): String diff --git a/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt b/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt new file mode 100644 index 00000000..ce4adca4 --- /dev/null +++ b/kvision-common/src/main/kotlin/pl/treksoft/kvision/types/KFile.kt @@ -0,0 +1,34 @@ +/* + * 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.types + +import kotlinx.serialization.Serializable + +/** + * A serializable class for a multiplatform File type. + */ +@Serializable +data class KFile( + val name: String, + val size: Int, + val content: String? = null +) diff --git a/kvision-server/build.gradle b/kvision-server/build.gradle new file mode 100644 index 00000000..8be34ecf --- /dev/null +++ b/kvision-server/build.gradle @@ -0,0 +1,29 @@ +apply plugin: "io.spring.dependency-management" +apply plugin: 'kotlin-platform-jvm' +apply plugin: 'kotlinx-serialization' + +dependencyManagement { + imports { + mavenBom "org.jooby:jooby-bom:${joobyVersion}" + } +} + +dependencies { + expectedBy project(":kvision-common") + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" + compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion" + compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion" + compile "org.jooby:jooby-lang-kotlin" + compile "org.jooby:jooby-jackson" + compile "com.fasterxml.jackson.module:jackson-module-kotlin:${jacksonModuleKotlinVersion}" + testCompile "org.jetbrains.kotlin:kotlin-test:$kotlinVersion" + testCompile project(":kvision-common") +} + +compileKotlin { + targetCompatibility = javaVersion + sourceCompatibility = javaVersion + kotlinOptions { + jvmTarget = javaVersion + } +} diff --git a/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt new file mode 100644 index 00000000..14b2b376 --- /dev/null +++ b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt @@ -0,0 +1,68 @@ +/* + * 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. + */ +@file:Suppress("EXPERIMENTAL_FEATURE_WARNING") + +package pl.treksoft.kvision.remote + +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import kotlinx.coroutines.experimental.Deferred +import org.jooby.Kooby +import org.jooby.Session +import org.jooby.json.Jackson +import kotlinx.coroutines.experimental.async as coroutinesAsync + +/** + * A Jooby based server. + */ +actual open class JoobyServer(init: JoobyServer.() -> Unit) : Kooby() { + init { + val mapper = jacksonObjectMapper() + @Suppress("LeakingThis") + use(Jackson(mapper)) + @Suppress("LeakingThis") + init.invoke(this) + } +} + +/** + * A server request. + */ +actual typealias Request = org.jooby.Request + +/** + * A helper extension function for asynchronous request processing. + */ +fun <RESP> Request?.async(block: (Request) -> RESP): Deferred<RESP> = this?.let { req -> + coroutinesAsync { + block(req) + } +} ?: throw IllegalStateException("Request not set!") + +/** + * A helper extension function for asynchronous request processing with session. + */ +fun <RESP> Request?.asyncSession(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req -> + val session = req.session() + coroutinesAsync { + block(req, session) + } +} ?: throw IllegalStateException("Request not set!") diff --git a/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt new file mode 100644 index 00000000..45a995ac --- /dev/null +++ b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt @@ -0,0 +1,230 @@ +/* + * 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 com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import kotlinx.coroutines.experimental.Deferred +import kotlinx.coroutines.experimental.runBlocking +import org.jooby.Status + +/** + * Multiplatform service manager. + */ +@Suppress("EXPERIMENTAL_FEATURE_WARNING") +actual open class ServiceManager<out T> actual constructor(val service: T?) { + + protected val routes: MutableList<JoobyServer.() -> Unit> = mutableListOf() + val mapper = jacksonObjectMapper() + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun <reified RET> bind( + route: String, + noinline function: T.(Request?) -> Deferred<RET> + ) { + routes.add({ + post("$SERVICE_PREFIX/$route") { req, res -> + if (service != null) { + try { + res.send(runBlocking { function.invoke(service, req).await() }) + } catch (e: Exception) { + e.printStackTrace() + res.status(500).send(e.message ?: "Error") + } + } else { + res.status(Status.BAD_REQUEST) + } + } + }) + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun <reified PAR, reified RET> bind( + route: String, + noinline function: T.(PAR, Request?) -> Deferred<RET> + ) { + routes.add({ + post("$SERVICE_PREFIX/$route") { req, res -> + val param = try { + req.body(PAR::class.java) + } catch (e: Exception) { + null as PAR + } + if (service != null) { + try { + res.send(runBlocking { function.invoke(service, param, req).await() }) + } catch (e: Exception) { + e.printStackTrace() + res.status(500).send(e.message ?: "Error") + } + } else { + res.status(Status.BAD_REQUEST) + } + } + }) + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET> + ) { + routes.add({ + post("$SERVICE_PREFIX/$route") { req, res -> + val str = req.body(String::class.java) + val tree = mapper.readTree(str) + if (tree.size() == 2 && service != null) { + val p1 = mapper.treeToValue(tree[0], PAR1::class.java) + val p2 = mapper.treeToValue(tree[1], PAR2::class.java) + try { + res.send(runBlocking { function.invoke(service, p1, p2, req).await() }) + } catch (e: Exception) { + e.printStackTrace() + res.status(500).send(e.message ?: "Error") + } + } else { + res.status(Status.BAD_REQUEST) + } + } + }) + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET> + ) { + routes.add({ + post("$SERVICE_PREFIX/$route") { req, res -> + val str = req.body(String::class.java) + val tree = mapper.readTree(str) + if (tree.size() == 3 && service != null) { + val p1 = mapper.treeToValue(tree[0], PAR1::class.java) + val p2 = mapper.treeToValue(tree[1], PAR2::class.java) + val p3 = mapper.treeToValue(tree[2], PAR3::class.java) + try { + res.send(runBlocking { function.invoke(service, p1, p2, p3, req).await() }) + } catch (e: Exception) { + e.printStackTrace() + res.status(500).send(e.message ?: "Error") + } + } else { + res.status(Status.BAD_REQUEST) + } + } + }) + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET> + ) { + routes.add({ + post("$SERVICE_PREFIX/$route") { req, res -> + val str = req.body(String::class.java) + val tree = mapper.readTree(str) + if (tree.size() == 4 && service != null) { + val p1 = mapper.treeToValue(tree[0], PAR1::class.java) + val p2 = mapper.treeToValue(tree[1], PAR2::class.java) + val p3 = mapper.treeToValue(tree[2], PAR3::class.java) + val p4 = mapper.treeToValue(tree[3], PAR4::class.java) + try { + res.send(runBlocking { function.invoke(service, p1, p2, p3, p4, req).await() }) + } catch (e: Exception) { + e.printStackTrace() + res.status(500).send(e.message ?: "Error") + } + } else { + res.status(Status.BAD_REQUEST) + } + } + }) + } + + /** + * Binds a given route with a function of the receiver. + * @param route a route + * @param function a function of the receiver + */ + protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind( + route: String, + noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET> + ) { + routes.add({ + post("$SERVICE_PREFIX/$route") { req, res -> + val str = req.body(String::class.java) + val tree = mapper.readTree(str) + if (tree.size() == 5 && service != null) { + val p1 = mapper.treeToValue(tree[0], PAR1::class.java) + val p2 = mapper.treeToValue(tree[1], PAR2::class.java) + val p3 = mapper.treeToValue(tree[2], PAR3::class.java) + val p4 = mapper.treeToValue(tree[3], PAR4::class.java) + val p5 = mapper.treeToValue(tree[4], PAR5::class.java) + try { + res.send(runBlocking { function.invoke(service, p1, p2, p3, p4, p5, req).await() }) + } catch (e: Exception) { + e.printStackTrace() + res.status(500).send(e.message ?: "Error") + } + } else { + res.status(Status.BAD_REQUEST) + } + } + }) + } + + /** + * Applies all defined routes to the given server. + * @param k a Jooby server + */ + actual fun applyRoutes(k: JoobyServer) { + routes.forEach { + it.invoke(k) + } + } + + /** + * Returns the list of defined bindings. + * Not used on the jvm platform. + */ + actual fun getCalls(): Map<String, String> = mapOf() +} diff --git a/kvision-server/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt b/kvision-server/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt new file mode 100644 index 00000000..1d174f88 --- /dev/null +++ b/kvision-server/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt @@ -0,0 +1,42 @@ +/* + * 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.types + +import java.text.SimpleDateFormat +import java.util.* + +/** + * A serializable wrapper for a multiplatform Date type. + */ +@Suppress("MayBeConstant") +actual val KDATE_FORMAT = "yyyy-MM-dd HH:mm:ss" + +internal actual fun nowDate(): KDate = + KDate(Date().time) + +internal actual fun String.toKDateF(format: String): KDate = + KDate(SimpleDateFormat(format).parse(this).time) + +internal actual fun KDate.toStringF(format: String) = + SimpleDateFormat(format).format(this.toJava()) + +fun KDate.toJava(): java.util.Date = java.util.Date(this.time) diff --git a/settings.gradle b/settings.gradle index 829fca7b..2216afa8 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,3 @@ rootProject.name = 'kvision' + +include 'kvision-common', 'kvision-server' diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index 65161303..18acdf9f 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -21,8 +21,14 @@ */ package pl.treksoft.kvision.form -import org.w3c.files.File -import kotlin.js.Date +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Mapper +import kotlinx.serialization.json.JSON +import kotlinx.serialization.serializer +import pl.treksoft.kvision.form.upload.Upload +import pl.treksoft.kvision.types.KDate +import pl.treksoft.kvision.types.KFile +import pl.treksoft.kvision.utils.getContent import kotlin.js.Json import kotlin.reflect.KProperty1 @@ -41,16 +47,45 @@ internal data class FieldParams<in F : FormControl>( * @constructor Creates a form with a given modelFactory function * @param K model class type * @param panel optional instance of [FormPanel] - * @param modelFactory function transforming a Map<String, Any?> to a data model of class K + * @param serializer a serializer for model type */ @Suppress("TooManyFunctions") -class Form<K>(private val panel: FormPanel<K>? = null, private val modelFactory: (Map<String, Any?>) -> K) { +class Form<K : Any>(private val panel: FormPanel<K>? = null, private val serializer: KSerializer<K>) { + internal val modelFactory: (Map<String, Any?>) -> K internal val fields: MutableMap<String, FormControl> = mutableMapOf() internal val fieldsParams: MutableMap<String, Any> = mutableMapOf() internal var validatorMessage: ((Form<K>) -> String?)? = null internal var validator: ((Form<K>) -> Boolean?)? = null + init { + modelFactory = { + val map = it.flatMap { entry -> + when (entry.value) { + is KDate -> { + listOf(entry.key to entry.value, "${entry.key}.time" to (entry.value as KDate).time) + } + is List<*> -> { + @Suppress("UNCHECKED_CAST") + (entry.value as? List<KFile>)?.let { + listOf(entry.key to entry.value, "${entry.key}.size" to it.size) + + it.mapIndexed { index, kFile -> + listOf( + "${entry.key}.${index + 1}.name" to kFile.name, + "${entry.key}.${index + 1}.size" to kFile.size, + "${entry.key}.${index + 1}.content" to kFile.content + ) + }.flatten() + } ?: listOf() + } + else -> listOf(entry.key to entry.value) + } + }.toMap().withDefault { null } + val mapper = Mapper.InNullableMapper(map) + mapper.read(serializer) + } + } + internal fun <C : FormControl> addInternal( key: KProperty1<K, *>, control: C, required: Boolean = false, validatorMessage: ((C) -> String?)? = null, @@ -121,8 +156,8 @@ class Form<K>(private val panel: FormPanel<K>? = null, priva |
