aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2018-03-24 01:17:22 +0100
committerRobert Jaros <rjaros@finn.pl>2018-03-24 01:17:22 +0100
commit3b1eae9ed8b7e1d57f8f05968820c5eb4bbe8fe2 (patch)
tree3a1df20f676fbf2f9b293920704045f1736e2ac8 /src/main/kotlin
parent45bbfe53a19a16127adf68f23325dd139fec89ca (diff)
downloadkvision-3b1eae9ed8b7e1d57f8f05968820c5eb4bbe8fe2.tar.gz
kvision-3b1eae9ed8b7e1d57f8f05968820c5eb4bbe8fe2.tar.bz2
kvision-3b1eae9ed8b7e1d57f8f05968820c5eb4bbe8fe2.zip
Handlebars templating support.
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/KVManager.kt1
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Div.kt4
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Label.kt4
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Tag.kt10
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Template.kt19
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/progress/ProgressIndicator.kt2
6 files changed, 32 insertions, 8 deletions
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<String> = 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<String> = 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<String> = 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<String> = setOf()
) :
- Div(content ?: "", rich, align, classes) {
+ Div(content, rich, align, classes) {
/**
* The current progress.