aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2020-05-15 16:53:44 +0200
committerRobert Jaros <rjaros@finn.pl>2020-05-15 16:53:44 +0200
commitb8b20bce36f30f7be7246bf9d44a2ca9e116569f (patch)
treeff800c5408a30caab53cf579b71064c8d38b1d9a /src/main/kotlin
parent07822bb3820629d86af637a05d656f9fb0a15a9f (diff)
downloadkvision-b8b20bce36f30f7be7246bf9d44a2ca9e116569f.tar.gz
kvision-b8b20bce36f30f7be7246bf9d44a2ca9e116569f.tar.bz2
kvision-b8b20bce36f30f7be7246bf9d44a2ca9e116569f.zip
Add new TextNode component.
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/core/Container.kt9
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/Tag.kt7
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/html/TextNode.kt80
3 files changed, 91 insertions, 5 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Container.kt b/src/main/kotlin/pl/treksoft/kvision/core/Container.kt
index 653f4a79..41d9426a 100644
--- a/src/main/kotlin/pl/treksoft/kvision/core/Container.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/core/Container.kt
@@ -21,6 +21,8 @@
*/
package pl.treksoft.kvision.core
+import pl.treksoft.kvision.html.TextNode
+
/**
* Base interface for all containers.
*/
@@ -66,4 +68,11 @@ interface Container : Component {
* @return list of children
*/
fun getChildren(): List<Component>
+
+ /**
+ * An operator to add a text node to the container.
+ */
+ operator fun String.unaryPlus() {
+ add(TextNode(this))
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt
index f133401d..b1dc4257 100644
--- a/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/html/Tag.kt
@@ -199,11 +199,8 @@ open class Tag(
return cl
}
- operator fun String.unaryPlus() {
- if (content == null)
- content = this
- else
- content += translate(this)
+ override operator fun String.unaryPlus() {
+ add(TextNode(this, rich))
}
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/TextNode.kt b/src/main/kotlin/pl/treksoft/kvision/html/TextNode.kt
new file mode 100644
index 00000000..33bb767f
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/html/TextNode.kt
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2017-present Robert Jaros
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+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.Widget
+
+/**
+ * Simple component for rendering text DOM node.
+ *
+ * @constructor
+ * @param content node text
+ * @param rich determines if [content] can contain HTML code
+ * @param init an initializer extension function
+ */
+open class TextNode(
+ content: String, rich: Boolean = false,
+ init: (TextNode.() -> Unit)? = null
+) : Widget() {
+
+ /**
+ * Text content of the tag.
+ */
+ var content by refreshOnUpdate(content)
+
+ /**
+ * Determines if [content] can contain HTML code.
+ */
+ var rich by refreshOnUpdate(rich)
+
+ init {
+ @Suppress("LeakingThis")
+ init?.invoke(this)
+ }
+
+ @Suppress("UnsafeCastFromDynamic")
+ override fun render(): VNode {
+ val translatedContent = translate(content)
+ return if (rich) {
+ KVManager.virtualize("<span>$translatedContent</span>")
+ } else {
+ translatedContent.asDynamic()
+ }
+ }
+}
+
+/**
+ * DSL builder extension function.
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+fun Container.textNode(
+ content: String, rich: Boolean = false,
+ init: (TextNode.() -> Unit)? = null
+): TextNode {
+ val textNode = TextNode(content, rich).apply { init?.invoke(this) }
+ this.add(textNode)
+ return textNode
+}