aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/html
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2018-02-12 10:50:42 +0100
committerRobert Jaros <rjaros@finn.pl>2018-02-12 10:50:42 +0100
commitbafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c (patch)
tree39288bdcb59ec48cb70f1c5820fc1854949744da /src/main/kotlin/pl/treksoft/kvision/html
parent2feea5e7cf8d492663e826ebcfb0a58e61820352 (diff)
downloadkvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.tar.gz
kvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.tar.bz2
kvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.zip
Type safe DSL builders
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/html')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Button.kt15
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Image.kt15
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Label.kt17
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Link.kt15
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/List.kt15
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Tag.kt15
6 files changed, 91 insertions, 1 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Button.kt b/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
index 874c4182..b4c6ed7c 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
@@ -23,6 +23,7 @@ package pl.treksoft.kvision.html
import com.github.snabbdom.VNode
import org.w3c.dom.events.MouseEvent
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.ResString
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.StringPair
@@ -158,4 +159,18 @@ open class Button(
}
return this
}
+
+ companion object {
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.button(
+ text: String, icon: String? = null, style: BUTTONSTYLE = BUTTONSTYLE.DEFAULT,
+ disabled: Boolean = false, classes: Set<String> = setOf(), init: (Button.() -> Unit)? = null
+ ) {
+ this.add(Button(text, icon, style, disabled, classes).apply { init?.invoke(this) })
+ }
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Image.kt b/src/main/kotlin/pl/treksoft/kvision/html/Image.kt
index 9321b4dc..c2af9bac 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/Image.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/Image.kt
@@ -22,6 +22,7 @@
package pl.treksoft.kvision.html
import com.github.snabbdom.VNode
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.ResString
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.StringPair
@@ -118,4 +119,18 @@ open class Image(
}
return cl
}
+
+ companion object {
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.image(
+ src: ResString, alt: String? = null, responsive: Boolean = false, shape: IMAGESHAPE? = null,
+ centered: Boolean = false, classes: Set<String> = setOf(), init: (Image.() -> Unit)? = null
+ ) {
+ this.add(Image(src, alt, responsive, shape, centered, classes).apply { init?.invoke(this) })
+ }
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Label.kt b/src/main/kotlin/pl/treksoft/kvision/html/Label.kt
index 1701150e..cd120cba 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/Label.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/Label.kt
@@ -21,6 +21,8 @@
*/
package pl.treksoft.kvision.html
+import pl.treksoft.kvision.core.Container
+
/**
* Simple label component rendered as *span*.
*
@@ -28,4 +30,17 @@ package pl.treksoft.kvision.html
* @param text label text
* @param rich determines if [text] can contain HTML code
*/
-open class Label(text: String, rich: Boolean = false) : Tag(TAG.SPAN, text, rich)
+open class Label(text: String, rich: Boolean = false) : Tag(TAG.SPAN, text, rich) {
+ companion object {
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.label(
+ text: String, rich: Boolean = false, init: (Label.() -> Unit)? = null
+ ) {
+ this.add(Label(text, rich).apply { init?.invoke(this) })
+ }
+ }
+}
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Link.kt b/src/main/kotlin/pl/treksoft/kvision/html/Link.kt
index ad8897d5..7572d444 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/Link.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/Link.kt
@@ -22,6 +22,7 @@
package pl.treksoft.kvision.html
import com.github.snabbdom.VNode
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.ResString
import pl.treksoft.kvision.core.StringPair
import pl.treksoft.kvision.panel.SimplePanel
@@ -81,4 +82,18 @@ open class Link(
override fun getSnAttrs(): List<StringPair> {
return super.getSnAttrs() + ("href" to url)
}
+
+ companion object {
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.link(
+ label: String, url: String, icon: String? = null, image: ResString? = null,
+ classes: Set<String> = setOf(), init: (Link.() -> Unit)? = null
+ ) {
+ this.add(Link(label, url, icon, image, classes).apply { init?.invoke(this) })
+ }
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/List.kt b/src/main/kotlin/pl/treksoft/kvision/html/List.kt
index 261555c9..29f5d18e 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/List.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/List.kt
@@ -25,6 +25,7 @@ import com.github.snabbdom.VNode
import com.github.snabbdom.h
import pl.treksoft.kvision.KVManager
import pl.treksoft.kvision.core.Component
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.panel.SimplePanel
@@ -142,4 +143,18 @@ open class ListTag(
}
return cl
}
+
+ companion object {
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.listTag(
+ type: LISTTYPE, elements: List<String>? = null, rich: Boolean = false,
+ classes: Set<String> = setOf(), init: (ListTag.() -> Unit)? = null
+ ) {
+ this.add(ListTag(type, elements, rich, classes, init))
+ }
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt
index 91fa2587..ebcc0020 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt
@@ -23,6 +23,7 @@ package pl.treksoft.kvision.html
import com.github.snabbdom.VNode
import pl.treksoft.kvision.KVManager
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.panel.SimplePanel
@@ -151,4 +152,18 @@ open class Tag(
}
return cl
}
+
+ companion object {
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.tag(
+ type: TAG, text: String? = null, rich: Boolean = false, align: ALIGN? = null,
+ classes: Set<String> = setOf(), init: (Tag.() -> Unit)? = null
+ ) {
+ this.add(Tag(type, text, rich, align, classes, init))
+ }
+ }
}