diff options
author | Robert Jaros <rjaros@finn.pl> | 2017-09-03 02:40:21 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2017-09-03 02:40:21 +0200 |
commit | c056275c522db3f2f391ce44a405da0cedae60ca (patch) | |
tree | 2fe29b341182223b20bc50fd0909e94f6e7641ef /src/main/kotlin/pl/treksoft/kvision/html | |
parent | 64e2bca354a1eb6018086427365336c3acb75cb8 (diff) | |
download | kvision-c056275c522db3f2f391ce44a405da0cedae60ca.tar.gz kvision-c056275c522db3f2f391ce44a405da0cedae60ca.tar.bz2 kvision-c056275c522db3f2f391ce44a405da0cedae60ca.zip |
Link widget
Tag, List, DropDown widget refactoring
Unit tests
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/html')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/Link.kt | 26 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/List.kt | 27 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/Tag.kt | 14 |
3 files changed, 56 insertions, 11 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Link.kt b/src/main/kotlin/pl/treksoft/kvision/html/Link.kt new file mode 100644 index 00000000..e201da48 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Link.kt @@ -0,0 +1,26 @@ +package pl.treksoft.kvision.html + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.snabbdom.StringPair + +open class Link(label: String, url: String, classes: Set<String> = setOf()) : Container(classes) { + var label = label + set(value) { + field = value + refresh() + } + var url = url + set(value) { + field = value + refresh() + } + + override fun render(): VNode { + return kvh("a", arrayOf(label) + childrenVNodes()) + } + + override fun getSnAttrs(): List<StringPair> { + return super.getSnAttrs() + ("href" to url) + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/html/List.kt b/src/main/kotlin/pl/treksoft/kvision/html/List.kt index 918f1576..b2801ce9 100644 --- a/src/main/kotlin/pl/treksoft/kvision/html/List.kt +++ b/src/main/kotlin/pl/treksoft/kvision/html/List.kt @@ -1,7 +1,9 @@ package pl.treksoft.kvision.html import com.github.snabbdom.VNode +import com.github.snabbdom.array import com.github.snabbdom.h +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.KVManager import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.snabbdom.StringBoolPair @@ -15,7 +17,7 @@ enum class LIST(val tagName: String) { DL_HORIZ("dl") } -open class ListTag(type: LIST, elements: List<String>, rich: Boolean = false, classes: Set<String> = setOf()) : Widget(classes) { +open class ListTag(type: LIST, elements: List<String>? = null, rich: Boolean = false, classes: Set<String> = setOf()) : Container(classes) { var type = type set(value) { field = value @@ -33,11 +35,24 @@ open class ListTag(type: LIST, elements: List<String>, rich: Boolean = false, cl } override fun render(): VNode { - val children = when (type) { - LIST.UL, LIST.OL, LIST.UNSTYLED, LIST.INLINE -> elements.map { el -> element("li", el, rich) } - LIST.DL, LIST.DL_HORIZ -> elements.mapIndexed { index, el -> element(if (index % 2 == 0) "dt" else "dd", el, rich) } - }.toTypedArray() - return kvh(type.tagName, children) + val childrenElements = when (type) { + LIST.UL, LIST.OL, LIST.UNSTYLED, LIST.INLINE -> elements?.map { el -> element("li", el, rich) } + LIST.DL, LIST.DL_HORIZ -> elements?.mapIndexed { index, el -> element(if (index % 2 == 0) "dt" else "dd", el, rich) } + }?.toTypedArray() + if (childrenElements != null) { + return kvh(type.tagName, childrenElements + childrenVNodes()) + } else { + return kvh(type.tagName, childrenVNodes()) + } + } + + override fun childrenVNodes(): Array<VNode> { + val childrenElements = children.filter { it.visible } + val res = when (type) { + LIST.UL, LIST.OL, LIST.UNSTYLED, LIST.INLINE -> childrenElements.map { v -> h("li", arrayOf(v.render())) } + LIST.DL, LIST.DL_HORIZ -> childrenElements.mapIndexed { index, v -> h(if (index % 2 == 0) "dt" else "dd", arrayOf(v.render())) } + } + return res.toTypedArray() } private fun element(name: String, value: String, rich: Boolean): VNode { diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt index 4178fbfb..cbaa611e 100644 --- a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt +++ b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt @@ -1,8 +1,8 @@ package pl.treksoft.kvision.html import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.KVManager -import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.snabbdom.StringBoolPair enum class TAG(val tagName: String) { @@ -44,7 +44,7 @@ enum class ALIGN(val className: String) { NOWRAP("text-nowrap") } -open class Tag(type: TAG, text: String, rich: Boolean = false, align: ALIGN = ALIGN.NONE, classes: Set<String> = setOf()) : Widget(classes) { +open class Tag(type: TAG, text: String? = null, rich: Boolean = false, align: ALIGN = ALIGN.NONE, classes: Set<String> = setOf()) : Container(classes) { var type = type set(value) { field = value @@ -67,10 +67,14 @@ open class Tag(type: TAG, text: String, rich: Boolean = false, align: ALIGN = AL } override fun render(): VNode { - if (rich) { - return kvh(type.tagName, arrayOf(KVManager.virtualize("<span>$text</span>"))) + if (text != null) { + if (rich) { + return kvh(type.tagName, arrayOf(KVManager.virtualize("<span>$text</span>")) + childrenVNodes()) + } else { + return kvh(type.tagName, arrayOf(text) + childrenVNodes()) + } } else { - return kvh(type.tagName, arrayOf(text)) + return kvh(type.tagName, childrenVNodes()) } } |