diff options
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/html')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/Button.kt | 99 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/Image.kt | 66 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/InTag.kt | 48 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/List.kt | 62 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/Tag.kt | 69 |
5 files changed, 344 insertions, 0 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Button.kt b/src/main/kotlin/pl/treksoft/kvision/html/Button.kt new file mode 100644 index 00000000..81df4eaf --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Button.kt @@ -0,0 +1,99 @@ +package pl.treksoft.kvision.html + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.KVManager +import pl.treksoft.kvision.core.ResString +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.snabbdom.StringBoolPair +import pl.treksoft.kvision.snabbdom.StringPair + +enum class BUTTON_STYLE(val className: String) { + DEFAULT("btn-default"), + PRIMARY("btn-primary"), + SUCCESS("btn-success"), + INFO("btn-info"), + WARNING("btn-warning"), + DANGER("btn-danger"), + LINK("btn-link") +} + +enum class BUTTON_SIZE(val className: String) { + LARGE("btn-lg"), + SMALL("btn-sm"), + XSMALL("btn-xs") +} + +open class Button(text: String, icon: String? = null, style: BUTTON_STYLE = BUTTON_STYLE.DEFAULT, size: BUTTON_SIZE? = null, + block: Boolean = false, disabled: Boolean = false, image: ResString? = null, classes: Set<String> = setOf()) : Widget(classes) { + var text = text + set(value) { + field = value + refresh() + } + var icon = icon + set(value) { + field = value + refresh() + } + var style = style + set(value) { + field = value + refresh() + } + var size = size + set(value) { + field = value + refresh() + } + var block = block + set(value) { + field = value + refresh() + } + var disabled = disabled + set(value) { + field = value + refresh() + } + var image = image + set(value) { + field = value + refresh() + } + + override fun render(): VNode { + val t = if (icon != null) { + if (icon?.startsWith("fa-") == true) { + arrayOf(KVManager.virtualize("<i class='fa $icon fa-lg'></i>"), " " + text) + } else { + arrayOf(KVManager.virtualize("<span class='glyphicon glyphicon-$icon'></span>"), " " + text) + } + } else if (image != null) { + arrayOf(KVManager.virtualize("<img src='$image' alt='' />"), " " + text) + } else { + arrayOf(text) + } + return kvh("button", t) + } + + override fun getSnClass(): List<StringBoolPair> { + val cl = super.getSnClass().toMutableList() + cl.add("btn" to true) + cl.add(style.className to true) + if (size != null) { + cl.add(size?.className.orEmpty() to true) + } + if (block) { + cl.add("btn-block" to true) + } + if (disabled) { + cl.add("disabled" to true) + } + return cl + } + + override fun getSnAttrs(): List<StringPair> { + return super.getSnAttrs() + ("type" to "button") + } + +}
\ No newline at end of file diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Image.kt b/src/main/kotlin/pl/treksoft/kvision/html/Image.kt new file mode 100644 index 00000000..3e9fe5d6 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Image.kt @@ -0,0 +1,66 @@ +package pl.treksoft.kvision.html + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.ResString +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.snabbdom.StringBoolPair +import pl.treksoft.kvision.snabbdom.StringPair + +enum class IMAGE_SHAPE(val className: String) { + ROUNDED("img-rounded"), + CIRCLE("img-circle"), + THUMBNAIL("img-thumbnail") +} + +open class Image(src: ResString, alt: String? = null, responsive: Boolean = false, shape: IMAGE_SHAPE? = null, centered: Boolean = false, classes: Set<String> = setOf()) : Widget(classes) { + var src = src + set(value) { + field = value + refresh() + } + var alt = alt + set(value) { + field = value + refresh() + } + var responsive = responsive + set(value) { + field = value + refresh() + } + var shape = shape + set(value) { + field = value + refresh() + } + var centered = centered + set(value) { + field = value + refresh() + } + + override fun render(): VNode { + return kvh("img") + } + + override fun getSnAttrs(): List<StringPair> { + val pr = super.getSnAttrs().toMutableList() + pr.add("src" to src) + if (alt != null) pr.add("alt" to alt.orEmpty()) + return pr + } + + override fun getSnClass(): List<StringBoolPair> { + val cl = super.getSnClass().toMutableList() + if (responsive) { + cl.add("img-responsive" to true) + } + if (centered) { + cl.add("center-block" to true) + } + if (shape != null) { + cl.add(shape?.className.orEmpty() to true) + } + return cl + } +}
\ No newline at end of file diff --git a/src/main/kotlin/pl/treksoft/kvision/html/InTag.kt b/src/main/kotlin/pl/treksoft/kvision/html/InTag.kt new file mode 100644 index 00000000..88f69435 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/InTag.kt @@ -0,0 +1,48 @@ +package pl.treksoft.kvision.html + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.KVManager +import pl.treksoft.kvision.core.Widget + +enum class INTAG(val tagName: String) { + MARK("mark"), + DEL("del"), + S("s"), + INS("ins"), + U("u"), + SMALL("small"), + STRONG("strong"), + EM("em"), + CITE("cite"), + CODE("code"), + KBD("kbd"), + VAR("var"), + SAMP("samp"), + SPAN("span") +} + +open class InTag(type: INTAG, text: String, rich: Boolean = false, classes: Set<String> = setOf()) : Widget(classes) { + var type = type + set(value) { + field = value + refresh() + } + var text = text + set(value) { + field = value + refresh() + } + var rich = rich + set(value) { + field = value + refresh() + } + + override fun render(): VNode { + if (rich) { + return kvh(type.tagName, arrayOf(KVManager.virtualize("<span>$text</span>"))) + } else { + return kvh(type.tagName, arrayOf(text)) + } + } +}
\ No newline at end of file diff --git a/src/main/kotlin/pl/treksoft/kvision/html/List.kt b/src/main/kotlin/pl/treksoft/kvision/html/List.kt new file mode 100644 index 00000000..918f1576 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/List.kt @@ -0,0 +1,62 @@ +package pl.treksoft.kvision.html + +import com.github.snabbdom.VNode +import com.github.snabbdom.h +import pl.treksoft.kvision.core.KVManager +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.snabbdom.StringBoolPair + +enum class LIST(val tagName: String) { + UL("ul"), + OL("ol"), + UNSTYLED("ul"), + INLINE("ul"), + DL("dl"), + DL_HORIZ("dl") +} + +open class ListTag(type: LIST, elements: List<String>, rich: Boolean = false, classes: Set<String> = setOf()) : Widget(classes) { + var type = type + set(value) { + field = value + refresh() + } + var elements = elements + set(value) { + field = value + refresh() + } + var rich = rich + set(value) { + field = value + refresh() + } + + 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) + } + + private fun element(name: String, value: String, rich: Boolean): VNode { + if (rich) { + return h(name, arrayOf(KVManager.virtualize("<span>$value</span>"))) + } else { + return h(name, value) + } + } + + override fun getSnClass(): List<StringBoolPair> { + val cl = super.getSnClass().toMutableList() + if (type == LIST.UNSTYLED) { + cl.add("list-unstyled" to true) + } else if (type == LIST.INLINE) { + cl.add("list-inline" to true) + } else if (type == LIST.DL_HORIZ) { + cl.add("dl-horizontal" to true) + } + return cl + } +}
\ No newline at end of file diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt new file mode 100644 index 00000000..60910707 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt @@ -0,0 +1,69 @@ +package pl.treksoft.kvision.html + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.KVManager +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.snabbdom.StringBoolPair + +enum class TAG(val tagName: String) { + H1("h1"), + H2("h2"), + H3("h3"), + H4("h4"), + H5("h5"), + H6("h6"), + P("p"), + ABBR("abbr"), + ADDRESS("address"), + BLOCKQUOTE("blockquote"), + FOOTER("footer"), + PRE("pre") +} + +enum class ALIGN(val className: String) { + NONE(""), + LEFT("text-left"), + CENTER("text-center"), + RIGHT("text-right"), + JUSTIFY("text-justify"), + NOWRAP("text-nowrap") +} + +open class Tag(type: TAG, text: String, rich: Boolean = false, align: ALIGN = ALIGN.NONE, classes: Set<String> = setOf()) : Widget(classes) { + var type = type + set(value) { + field = value + refresh() + } + var text = text + set(value) { + field = value + refresh() + } + var rich = rich + set(value) { + field = value + refresh() + } + var align = align + set(value) { + field = value + refresh() + } + + override fun render(): VNode { + if (rich) { + return kvh(type.tagName, arrayOf(KVManager.virtualize("<span>$text</span>"))) + } else { + return kvh(type.tagName, arrayOf(text)) + } + } + + override fun getSnClass(): List<StringBoolPair> { + val cl = super.getSnClass().toMutableList() + if (align != ALIGN.NONE) { + cl.add(align.className to true) + } + return cl + } +}
\ No newline at end of file |