From 3b1eae9ed8b7e1d57f8f05968820c5eb4bbe8fe2 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 24 Mar 2018 01:17:22 +0100 Subject: Handlebars templating support. --- src/main/kotlin/pl/treksoft/kvision/KVManager.kt | 1 + src/main/kotlin/pl/treksoft/kvision/html/Div.kt | 4 ++-- src/main/kotlin/pl/treksoft/kvision/html/Label.kt | 4 ++-- src/main/kotlin/pl/treksoft/kvision/html/Tag.kt | 10 +++++++--- src/main/kotlin/pl/treksoft/kvision/html/Template.kt | 19 +++++++++++++++++++ .../pl/treksoft/kvision/progress/ProgressIndicator.kt | 2 +- 6 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/pl/treksoft/kvision/html/Template.kt (limited to 'src/main') diff --git a/src/main/kotlin/pl/treksoft/kvision/KVManager.kt b/src/main/kotlin/pl/treksoft/kvision/KVManager.kt index 7f79155d..121f0ab7 100644 --- a/src/main/kotlin/pl/treksoft/kvision/KVManager.kt +++ b/src/main/kotlin/pl/treksoft/kvision/KVManager.kt @@ -121,6 +121,7 @@ internal object KVManager { private val resizable = require("jquery-resizable-dom") internal val fecha = require("fecha") + private val handlebars = require("handlebars/dist/handlebars.runtime.min.js") private val sdPatch = Snabbdom.init( arrayOf( classModule, attributesModule, propsModule, styleModule, diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Div.kt b/src/main/kotlin/pl/treksoft/kvision/html/Div.kt index 6acc9d5b..42d6b496 100644 --- a/src/main/kotlin/pl/treksoft/kvision/html/Div.kt +++ b/src/main/kotlin/pl/treksoft/kvision/html/Div.kt @@ -34,7 +34,7 @@ import pl.treksoft.kvision.core.Container * @param init an initializer extension function */ open class Div( - content: String, + content: String? = null, rich: Boolean = false, align: Align? = null, classes: Set = setOf(), @@ -54,7 +54,7 @@ open class Div( * It takes the same parameters as the constructor of the built component. */ fun Container.div( - content: String, + content: String? = null, rich: Boolean = false, align: Align? = null, classes: Set = setOf(), diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Label.kt b/src/main/kotlin/pl/treksoft/kvision/html/Label.kt index 2ec45274..41406cf2 100644 --- a/src/main/kotlin/pl/treksoft/kvision/html/Label.kt +++ b/src/main/kotlin/pl/treksoft/kvision/html/Label.kt @@ -30,7 +30,7 @@ import pl.treksoft.kvision.core.Container * @param content label text * @param rich determines if [content] can contain HTML code */ -open class Label(content: String, rich: Boolean = false) : Tag(TAG.SPAN, content, rich) { +open class Label(content: String? = null, rich: Boolean = false) : Tag(TAG.SPAN, content, rich) { companion object { /** * DSL builder extension function. @@ -38,7 +38,7 @@ open class Label(content: String, rich: Boolean = false) : Tag(TAG.SPAN, content * It takes the same parameters as the constructor of the built component. */ fun Container.label( - content: String, rich: Boolean = false, init: (Label.() -> Unit)? = null + content: String? = null, rich: Boolean = false, init: (Label.() -> Unit)? = null ): Label { val label = Label(content, rich).apply { init?.invoke(this) } this.add(label) diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt index fce5acaa..936d7b89 100644 --- a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt +++ b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt @@ -104,7 +104,7 @@ enum class Align(val className: String) { open class Tag( type: TAG, content: String? = null, rich: Boolean = false, align: Align? = null, classes: Set = setOf(), init: (Tag.() -> Unit)? = null -) : SimplePanel(classes) { +) : SimplePanel(classes), Template { /** * Tag type. @@ -113,15 +113,19 @@ open class Tag( /** * Text content of the tag. */ - var content by refreshOnUpdate(content) + override var content by refreshOnUpdate(content) /** * Determines if [content] can contain HTML code. */ - var rich by refreshOnUpdate(rich) + override var rich by refreshOnUpdate(rich) /** * Text align. */ var align by refreshOnUpdate(align) + /** + * Handlebars template. + */ + override var template: (Any?) -> String by refreshOnUpdate() init { @Suppress("LeakingThis") diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Template.kt b/src/main/kotlin/pl/treksoft/kvision/html/Template.kt new file mode 100644 index 00000000..f54c6115 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Template.kt @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2018. Robert Jaros + */ +package pl.treksoft.kvision.html + +interface Template { + var content: String? + var rich: Boolean + var template: (Any?) -> String + + var templateData: Any? + get() { + return null + } + set(value) { + if (!rich) rich = true + content = template(value) + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/progress/ProgressIndicator.kt b/src/main/kotlin/pl/treksoft/kvision/progress/ProgressIndicator.kt index 10413ebb..02a4c354 100644 --- a/src/main/kotlin/pl/treksoft/kvision/progress/ProgressIndicator.kt +++ b/src/main/kotlin/pl/treksoft/kvision/progress/ProgressIndicator.kt @@ -44,7 +44,7 @@ internal class ProgressIndicator( content: String? = null, rich: Boolean = false, align: Align? = null, classes: Set = setOf() ) : - Div(content ?: "", rich, align, classes) { + Div(content, rich, align, classes) { /** * The current progress. -- cgit