diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-05-11 02:37:06 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-05-11 02:37:06 +0200 |
commit | 046e5586674c9debde9cdd9108e60c74265e8119 (patch) | |
tree | ab010707edc8439c655a3dc4b58630ef6040bab0 /src | |
parent | 0b91779a5baf5f78ae616db8f67dcf4d81e839cc (diff) | |
download | kvision-046e5586674c9debde9cdd9108e60c74265e8119.tar.gz kvision-046e5586674c9debde9cdd9108e60c74265e8119.tar.bz2 kvision-046e5586674c9debde9cdd9108e60c74265e8119.zip |
Add support for additional attributes in the Tag component.
Add some missing tag types.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/Tag.kt | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt index 32119a90..d9d839ba 100644 --- a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt +++ b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt @@ -25,6 +25,8 @@ 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.core.StringPair +import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.i18n.I18n import pl.treksoft.kvision.panel.SimplePanel @@ -78,7 +80,10 @@ enum class TAG(internal val tagName: String) { TD("td"), FORM("form"), - INPUT("input") + INPUT("input"), + SELECT("select"), + OPTION("option"), + BUTTON("button") } /** @@ -101,13 +106,17 @@ enum class Align(val className: String) { * @param rich determines if [content] can contain HTML code * @param align content align * @param classes a set of CSS class names + * @param attributes a map of additional attributes * @param init an initializer extension function */ open class Tag( type: TAG, content: String? = null, rich: Boolean = false, align: Align? = null, - classes: Set<String> = setOf(), init: (Tag.() -> Unit)? = null + classes: Set<String> = setOf(), attributes: Map<String, String> = mapOf(), + init: (Tag.() -> Unit)? = null ) : SimplePanel(classes), Template { + protected val attributes = attributes.toMutableMap() + /** * Tag type. */ @@ -172,6 +181,14 @@ open class Tag( return cl } + override fun getSnAttrs(): List<StringPair> { + return if (attributes.isEmpty()) { + super.getSnAttrs() + } else { + attributes.toList() + super.getSnAttrs() + } + } + operator fun String.unaryPlus() { if (content == null) content = this @@ -179,6 +196,36 @@ open class Tag( content += translate(this) } + /** + * Returns the value of an additional attribute. + * @param name the name of the attribute + * @return the value of the attribute + */ + fun getAttribute(name: String): String? { + return this.attributes[name] + } + + /** + * Sets the value of additional attribute. + * @param name the name of the attribute + * @param value the value of the attribute + */ + fun setAttribute(name: String, value: String): Widget { + this.attributes[name] = value + refresh() + return this + } + + /** + * Removes the value of additional attribute. + * @param name the name of the attribute + */ + fun removeAttribute(name: String): Widget { + this.attributes.remove(name) + refresh() + return this + } + companion object { /** * DSL builder extension function. @@ -187,9 +234,10 @@ open class Tag( */ fun Container.tag( type: TAG, content: String? = null, rich: Boolean = false, align: Align? = null, - classes: Set<String> = setOf(), init: (Tag.() -> Unit)? = null + classes: Set<String> = setOf(), attributes: Map<String, String> = mapOf(), + init: (Tag.() -> Unit)? = null ): Tag { - val tag = Tag(type, content, rich, align, classes, init) + val tag = Tag(type, content, rich, align, classes, attributes, init) this.add(tag) return tag } |