From 659b87f5c1c297c68b125f67bc0b29b547debfd0 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 23 Sep 2017 19:41:48 +0200 Subject: Composable components methods --- .../kotlin/pl/treksoft/kvision/core/Container.kt | 19 +++++++++------ src/main/kotlin/pl/treksoft/kvision/core/Root.kt | 3 ++- src/main/kotlin/pl/treksoft/kvision/core/Widget.kt | 27 ++++++++++++++-------- .../pl/treksoft/kvision/dropdown/DropDown.kt | 10 ++++---- src/main/kotlin/pl/treksoft/kvision/modal/Alert.kt | 4 +++- src/main/kotlin/pl/treksoft/kvision/modal/Modal.kt | 22 +++++++++++------- 6 files changed, 55 insertions(+), 30 deletions(-) (limited to 'src/main/kotlin/pl/treksoft') diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Container.kt b/src/main/kotlin/pl/treksoft/kvision/core/Container.kt index 2d81f630..61199a00 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Container.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Container.kt @@ -13,37 +13,42 @@ open class Container(classes: Set = setOf()) : Widget(classes) { return children.filter { it.visible }.map { it.render() }.toTypedArray() } - protected fun addInternal(child: Widget) { + protected fun addInternal(child: Widget): Container { children.add(child) child.parent = this refresh() + return this } - open fun add(child: Widget) { - addInternal(child) + open fun add(child: Widget): Container { + return addInternal(child) } - open fun addAll(children: List) { + open fun addAll(children: List): Container { this.children.addAll(children) children.map { it.parent = this } refresh() + return this } - open fun remove(child: Widget) { + open fun remove(child: Widget): Container { children.remove(child) child.clearParent() refresh() + return this } - open fun removeAt(index: Int) { + open fun removeAt(index: Int): Container { children.removeAt(index).clearParent() refresh() + return this } - open fun removeAll() { + open fun removeAll(): Container { children.map { it.clearParent() } children.clear() refresh() + return this } } diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Root.kt b/src/main/kotlin/pl/treksoft/kvision/core/Root.kt index abb5d3cf..b5562b3f 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Root.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Root.kt @@ -33,8 +33,9 @@ class Root(id: String, private val fluid: Boolean = false) : Container() { return super.getSnClass() + (css to true) } - override fun refresh() { + override fun refresh(): Widget { rootVnode = KVManager.patch(rootVnode, render()) + return this } override fun getRoot(): Root? { diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt index 438370cf..38d39814 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt @@ -124,37 +124,44 @@ open class Widget(classes: Set = setOf()) : KVObject { } @Suppress("UNCHECKED_CAST") - open fun setEventListener(block: SnOn.() -> Unit) { + open fun setEventListener(block: SnOn.() -> Unit): Widget { listeners.add(block as SnOn.() -> Unit) refresh() + return this } - open fun setEventListener(block: SnOn.() -> Unit) { + open fun setEventListener(block: SnOn.() -> Unit): Widget { listeners.add(block) refresh() + return this } - open fun removeEventListeners() { + open fun removeEventListeners(): Widget { listeners.clear() refresh() + return this } - open fun show() { + open fun show(): Widget { visible = true + return this } - open fun hide() { + open fun hide(): Widget { visible = false + return this } - open fun addCssClass(css: String) { + open fun addCssClass(css: String): Widget { this.classes.add(css) refresh() + return this } - open fun removeCssClass(css: String) { + open fun removeCssClass(css: String): Widget { this.classes.remove(css) refresh() + return this } open fun getElement(): Node? { @@ -169,12 +176,14 @@ open class Widget(classes: Set = setOf()) : KVObject { return getElement()?.let { jQuery(it).asDynamic() } } - internal fun clearParent() { + internal fun clearParent(): Widget { this.parent = null + return this } - protected open fun refresh() { + protected open fun refresh(): Widget { this.parent?.refresh() + return this } protected open fun afterInsert(node: VNode) { diff --git a/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt b/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt index 00e8fd9b..eb7a0b50 100644 --- a/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt +++ b/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt @@ -96,12 +96,14 @@ open class DropDown(text: String, elements: List? = null, icon: Stri var counter = 0 } - override fun add(child: Widget) { + override fun add(child: Widget): Container { list.add(child) + return this } - override fun addAll(children: List) { + override fun addAll(children: List): Container { list.addAll(children) + return this } private fun setChildrenFromElements() { @@ -189,9 +191,9 @@ open class DropDownListTag(private val ariaId: String, classes: Set = se return super.getSnAttrs() + listOf("aria-labelledby" to ariaId) } - override fun hide() { + override fun hide(): Widget { if (visible) hideInternal() - super.hide() + return super.hide() } override fun afterInsert(node: VNode) { diff --git a/src/main/kotlin/pl/treksoft/kvision/modal/Alert.kt b/src/main/kotlin/pl/treksoft/kvision/modal/Alert.kt index 5e0e910a..a7820b30 100644 --- a/src/main/kotlin/pl/treksoft/kvision/modal/Alert.kt +++ b/src/main/kotlin/pl/treksoft/kvision/modal/Alert.kt @@ -1,5 +1,6 @@ package pl.treksoft.kvision.modal +import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.html.ALIGN import pl.treksoft.kvision.html.BUTTONSTYLE import pl.treksoft.kvision.html.Button @@ -38,9 +39,10 @@ open class Alert(caption: String? = null, text: String? = null, rich: Boolean = this.addButton(okButton) } - override fun hide() { + override fun hide(): Widget { super.hide() this.callback?.invoke() + return this } companion object { diff --git a/src/main/kotlin/pl/treksoft/kvision/modal/Modal.kt b/src/main/kotlin/pl/treksoft/kvision/modal/Modal.kt index 1a0bce1a..d9a0466f 100644 --- a/src/main/kotlin/pl/treksoft/kvision/modal/Modal.kt +++ b/src/main/kotlin/pl/treksoft/kvision/modal/Modal.kt @@ -87,28 +87,34 @@ open class Modal(caption: String? = null, closeButton: Boolean = true, } } - override fun add(child: Widget) { + override fun add(child: Widget): Container { body.add(child) + return this } - override fun addAll(children: List) { + override fun addAll(children: List): Container { body.addAll(children) + return this } - open fun addButton(button: Button) { + open fun addButton(button: Button): Modal { footer.add(button) + return this } - open fun removeButton(button: Button) { + open fun removeButton(button: Button): Modal { footer.remove(button) + return this } - open fun removeButtonAt(index: Int) { + open fun removeButtonAt(index: Int): Modal { footer.removeAt(index) + return this } - open fun removeAllButtons() { + open fun removeAllButtons(): Modal { footer.removeAll() + return this } override fun getSnAttrs(): List { @@ -151,9 +157,9 @@ open class Modal(caption: String? = null, closeButton: Boolean = true, }) } - override fun hide() { + override fun hide(): Widget { if (visible) hideInternal() - super.hide() + return super.hide() } open fun toggle() { -- cgit