From 06f297d68887c7934e66d2c757abc8bf619df66a Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 28 Oct 2017 23:45:26 +0200 Subject: Databinding components Event handlers refactoring --- .../kotlin/pl/treksoft/kvision/panel/DockPanel.kt | 13 +++-- .../kotlin/pl/treksoft/kvision/panel/FlexPanel.kt | 16 +++--- .../kotlin/pl/treksoft/kvision/panel/GridPanel.kt | 17 +++--- .../treksoft/kvision/panel/ResponsiveGridPanel.kt | 63 +++++++++++----------- .../pl/treksoft/kvision/panel/SimplePanel.kt | 59 ++++++++++++++++++++ .../kotlin/pl/treksoft/kvision/panel/SplitPanel.kt | 3 +- .../kotlin/pl/treksoft/kvision/panel/StackPanel.kt | 11 ++-- .../kotlin/pl/treksoft/kvision/panel/TabPanel.kt | 33 +++++++++--- 8 files changed, 146 insertions(+), 69 deletions(-) create mode 100644 src/main/kotlin/pl/treksoft/kvision/panel/SimplePanel.kt (limited to 'src/main/kotlin/pl/treksoft/kvision/panel') diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/DockPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/DockPanel.kt index 6d86727c..6e037c28 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/DockPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/DockPanel.kt @@ -1,6 +1,5 @@ package pl.treksoft.kvision.panel -import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget enum class SIDE { @@ -11,7 +10,7 @@ enum class SIDE { DOWN } -open class DockPanel(classes: Set = setOf()) : Container(classes = classes) { +open class DockPanel(classes: Set = setOf()) : SimplePanel(classes = classes) { protected var left: Widget? = null protected var center: Widget? = null protected var right: Widget? = null @@ -59,16 +58,16 @@ open class DockPanel(classes: Set = setOf()) : Container(classes = class return this } - override fun add(child: Widget): Container { + override fun add(child: Widget): DockPanel { return this.add(child, SIDE.CENTER) } - override fun addAll(children: List): Container { + override fun addAll(children: List): DockPanel { children.forEach { this.add(it) } return this } - override fun remove(child: Widget): Container { + override fun remove(child: Widget): DockPanel { if (child == left) removeAt(SIDE.LEFT) if (child == center) removeAt(SIDE.CENTER) if (child == right) removeAt(SIDE.RIGHT) @@ -77,7 +76,7 @@ open class DockPanel(classes: Set = setOf()) : Container(classes = class return this } - open fun removeAt(position: SIDE): Container { + open fun removeAt(position: SIDE): DockPanel { when (position) { SIDE.UP -> { up?.let { mainContainer.remove(it) } @@ -103,7 +102,7 @@ open class DockPanel(classes: Set = setOf()) : Container(classes = class return this } - override fun removeAll(): Container { + override fun removeAll(): DockPanel { removeAt(SIDE.LEFT) removeAt(SIDE.CENTER) removeAt(SIDE.RIGHT) diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt index 10483a8d..6b6e11fb 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt @@ -1,6 +1,5 @@ package pl.treksoft.kvision.panel -import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.core.WidgetWrapper import pl.treksoft.kvision.snabbdom.StringPair @@ -46,7 +45,7 @@ enum class FLEXALIGNCONTENT(val alignContent: String) { open class FlexPanel(direction: FLEXDIR? = null, wrap: FLEXWRAP? = null, justify: FLEXJUSTIFY? = null, alignItems: FLEXALIGNITEMS? = null, alignContent: FLEXALIGNCONTENT? = null, - classes: Set = setOf()) : Container(classes) { + classes: Set = setOf()) : SimplePanel(classes) { var direction = direction set(value) { field = value @@ -75,20 +74,21 @@ open class FlexPanel(direction: FLEXDIR? = null, wrap: FLEXWRAP? = null, justify @Suppress("LongParameterList") fun add(child: Widget, order: Int? = null, grow: Int? = null, shrink: Int? = null, - basis: Int? = null, alignSelf: FLEXALIGNITEMS? = null, classes: Set = setOf()): Container { - return addInternal(FlexWrapper(child, order, grow, shrink, basis, alignSelf, classes)) + basis: Int? = null, alignSelf: FLEXALIGNITEMS? = null, classes: Set = setOf()): FlexPanel { + addInternal(FlexWrapper(child, order, grow, shrink, basis, alignSelf, classes)) + return this } - override fun add(child: Widget): Container { + override fun add(child: Widget): FlexPanel { return add(child, null) } - override fun addAll(children: List): Container { + override fun addAll(children: List): FlexPanel { children.forEach { add(it, null) } return this } - override fun remove(child: Widget): Container { + override fun remove(child: Widget): FlexPanel { children.find { (it as FlexWrapper).delegate == child }?.let { super.remove(it) it.dispose() @@ -96,7 +96,7 @@ open class FlexPanel(direction: FLEXDIR? = null, wrap: FLEXWRAP? = null, justify return this } - override fun removeAll(): Container { + override fun removeAll(): FlexPanel { children.map { it.clearParent() it.dispose() diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/GridPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/GridPanel.kt index 44ba509f..cac5c79b 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/GridPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/GridPanel.kt @@ -1,6 +1,5 @@ package pl.treksoft.kvision.panel -import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.core.WidgetWrapper import pl.treksoft.kvision.snabbdom.StringPair @@ -50,7 +49,7 @@ open class GridPanel(autoColumns: String? = null, autoRows: String? = null, auto templateColumns: String? = null, templateRows: String? = null, templateAreas: List? = null, columnGap: Int? = null, rowGap: Int? = null, justifyItems: GRIDJUSTIFY? = null, alignItems: GRIDALIGN? = null, justifyContent: GRIDJUSTIFYCONTENT? = null, - alignContent: GRIDALIGNCONTENT? = null, classes: Set = setOf()) : Container(classes) { + alignContent: GRIDALIGNCONTENT? = null, classes: Set = setOf()) : SimplePanel(classes) { var autoColumns = autoColumns set(value) { field = value @@ -115,21 +114,21 @@ open class GridPanel(autoColumns: String? = null, autoRows: String? = null, auto @Suppress("LongParameterList") fun add(child: Widget, columnStart: Int? = null, rowStart: Int? = null, columnEnd: String? = null, rowEnd: String? = null, area: String? = null, justifySelf: GRIDJUSTIFY? = null, - alignSelf: GRIDALIGN? = null, classes: Set = setOf()): Container { - return addInternal(GridWrapper(child, columnStart, rowStart, columnEnd, rowEnd, area, justifySelf, - alignSelf, classes)) + alignSelf: GRIDALIGN? = null, classes: Set = setOf()): GridPanel { + addInternal(GridWrapper(child, columnStart, rowStart, columnEnd, rowEnd, area, justifySelf, alignSelf, classes)) + return this } - override fun add(child: Widget): Container { + override fun add(child: Widget): GridPanel { return add(child, null, null) } - override fun addAll(children: List): Container { + override fun addAll(children: List): GridPanel { children.forEach { add(it, null, null) } return this } - override fun remove(child: Widget): Container { + override fun remove(child: Widget): GridPanel { children.find { (it as GridWrapper).delegate == child }?.let { super.remove(it) it.dispose() @@ -137,7 +136,7 @@ open class GridPanel(autoColumns: String? = null, autoRows: String? = null, auto return this } - override fun removeAll(): Container { + override fun removeAll(): GridPanel { children.map { it.clearParent() it.dispose() diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/ResponsiveGridPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/ResponsiveGridPanel.kt index 17b3803f..3587ea8e 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/ResponsiveGridPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/ResponsiveGridPanel.kt @@ -1,6 +1,5 @@ package pl.treksoft.kvision.panel -import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.core.WidgetWrapper import pl.treksoft.kvision.html.ALIGN @@ -20,7 +19,7 @@ internal data class WidgetParam(val widget: Widget, val size: Int, val offset: I open class ResponsiveGridPanel(private val gridsize: GRIDSIZE = GRIDSIZE.MD, private var rows: Int = 0, private var cols: Int = 0, align: ALIGN? = null, - classes: Set = setOf()) : Container(classes) { + classes: Set = setOf()) : SimplePanel(classes) { protected var align = align set(value) { field = value @@ -30,7 +29,7 @@ open class ResponsiveGridPanel(private val gridsize: GRIDSIZE = GRIDSIZE.MD, internal val map = mutableMapOf>() private var auto: Boolean = true - open fun add(child: Widget, row: Int, col: Int, size: Int = 0, offset: Int = 0): Container { + open fun add(child: Widget, row: Int, col: Int, size: Int = 0, offset: Int = 0): ResponsiveGridPanel { val cRow = if (row < 0) 0 else row val cCol = if (col < 0) 0 else col if (row > rows - 1) rows = cRow + 1 @@ -41,17 +40,17 @@ open class ResponsiveGridPanel(private val gridsize: GRIDSIZE = GRIDSIZE.MD, return this } - override fun add(child: Widget): Container { + override fun add(child: Widget): ResponsiveGridPanel { return this.add(child, 0, this.cols) } - override fun addAll(children: List): Container { + override fun addAll(children: List): ResponsiveGridPanel { children.forEach { this.add(it) } return this } @Suppress("NestedBlockDepth") - override fun remove(child: Widget): Container { + override fun remove(child: Widget): ResponsiveGridPanel { for (i in 0 until rows) { val row = map[i] if (row != null) { @@ -67,7 +66,7 @@ open class ResponsiveGridPanel(private val gridsize: GRIDSIZE = GRIDSIZE.MD, return this } - open fun removeAt(row: Int, col: Int): Container { + open fun removeAt(row: Int, col: Int): ResponsiveGridPanel { map[row]?.remove(col) refreshRowContainers() return this @@ -75,38 +74,40 @@ open class ResponsiveGridPanel(private val gridsize: GRIDSIZE = GRIDSIZE.MD, @Suppress("ComplexMethod", "NestedBlockDepth") protected open fun refreshRowContainers() { - clearRowContainers() - val num = MAX_COLUMNS / cols - for (i in 0 until rows) { - val rowContainer = Container(setOf("row")) - val row = map[i] - if (row != null) { - for (j in 0 until cols) { - val wp = row[j] - if (auto) { - val widget = wp?.widget?.let { - WidgetWrapper(it, setOf("col-" + gridsize.size + "-" + num)) - } ?: Tag(TAG.DIV, classes = setOf("col-" + gridsize.size + "-" + num)) - align?.let { - widget.addCssClass(it.className) - } - rowContainer.add(widget) - } else { - if (wp != null) { - val s = if (wp.size > 0) wp.size else num - val widget = WidgetWrapper(wp.widget, setOf("col-" + gridsize.size + "-" + s)) - if (wp.offset > 0) { - widget.addCssClass("col-" + gridsize.size + "-offset-" + wp.offset) - } + singleRender { + clearRowContainers() + val num = MAX_COLUMNS / cols + for (i in 0 until rows) { + val rowContainer = SimplePanel(setOf("row")) + val row = map[i] + if (row != null) { + for (j in 0 until cols) { + val wp = row[j] + if (auto) { + val widget = wp?.widget?.let { + WidgetWrapper(it, setOf("col-" + gridsize.size + "-" + num)) + } ?: Tag(TAG.DIV, classes = setOf("col-" + gridsize.size + "-" + num)) align?.let { widget.addCssClass(it.className) } rowContainer.add(widget) + } else { + if (wp != null) { + val s = if (wp.size > 0) wp.size else num + val widget = WidgetWrapper(wp.widget, setOf("col-" + gridsize.size + "-" + s)) + if (wp.offset > 0) { + widget.addCssClass("col-" + gridsize.size + "-offset-" + wp.offset) + } + align?.let { + widget.addCssClass(it.className) + } + rowContainer.add(widget) + } } } } + addInternal(rowContainer) } - addInternal(rowContainer) } } diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/SimplePanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/SimplePanel.kt new file mode 100644 index 00000000..e9cf503b --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/panel/SimplePanel.kt @@ -0,0 +1,59 @@ +package pl.treksoft.kvision.panel + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.core.Widget + +open class SimplePanel(classes: Set = setOf()) : Widget(classes), Container { + internal val children: MutableList = mutableListOf() + + override fun render(): VNode { + return kvh("div", childrenVNodes()) + } + + protected open fun childrenVNodes(): Array { + return children.filter { it.visible }.map { it.renderVNode() }.toTypedArray() + } + + protected fun addInternal(child: Widget): SimplePanel { + children.add(child) + child.parent = this + refresh() + return this + } + + override fun add(child: Widget): SimplePanel { + return addInternal(child) + } + + override fun addAll(children: List): SimplePanel { + this.children.addAll(children) + children.map { it.parent = this } + refresh() + return this + } + + override fun remove(child: Widget): SimplePanel { + if (children.remove(child)) { + child.clearParent() + refresh() + } + return this + } + + override fun removeAll(): SimplePanel { + children.map { it.clearParent() } + children.clear() + refresh() + return this + } + + override fun getChildren(): List { + return ArrayList(children) + } + + override fun dispose() { + children.forEach { it.dispose() } + removeAll() + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/SplitPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/SplitPanel.kt index 03740222..02811639 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/SplitPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/SplitPanel.kt @@ -3,7 +3,6 @@ package pl.treksoft.kvision.panel import com.github.snabbdom.VNode import pl.treksoft.jquery.JQuery import pl.treksoft.jquery.JQueryEventObject -import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.UNIT import pl.treksoft.kvision.html.TAG import pl.treksoft.kvision.html.Tag @@ -15,7 +14,7 @@ enum class DIRECTION(val dir: String) { } open class SplitPanel(private val direction: DIRECTION = DIRECTION.VERTICAL, - classes: Set = setOf()) : Container(classes + ("splitpanel-" + direction.dir)) { + classes: Set = setOf()) : SimplePanel(classes + ("splitpanel-" + direction.dir)) { @Suppress("LeakingThis") internal val splitter = Splitter(this, direction) diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt index c5162c0a..e8975dce 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt @@ -1,11 +1,10 @@ package pl.treksoft.kvision.panel import com.github.snabbdom.VNode -import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget open class StackPanel(private val activateLast: Boolean = true, - classes: Set = setOf()) : Container(classes) { + classes: Set = setOf()) : SimplePanel(classes) { var activeIndex = -1 set(value) { field = value @@ -20,27 +19,27 @@ open class StackPanel(private val activateLast: Boolean = true, } } - override fun add(child: Widget): Container { + override fun add(child: Widget): StackPanel { super.add(child) if (activateLast) activeIndex = children.size - 1 else if (activeIndex == -1) activeIndex = 0 return this } - override fun addAll(children: List): Container { + override fun addAll(children: List): StackPanel { super.addAll(children) if (activateLast) activeIndex = this.children.size - 1 else if (activeIndex == -1) activeIndex = 0 return this } - override fun remove(child: Widget): Container { + override fun remove(child: Widget): StackPanel { super.remove(child) if (activeIndex > children.size - 1) activeIndex = children.size - 1 return this } - override fun removeAll(): Container { + override fun removeAll(): StackPanel { super.removeAll() if (activeIndex > children.size - 1) activeIndex = children.size - 1 return this diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt index 817ecaa0..4190dbae 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt @@ -1,15 +1,12 @@ package pl.treksoft.kvision.panel -import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.ResString import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.html.Link import pl.treksoft.kvision.html.TAG import pl.treksoft.kvision.html.Tag -open class TabPanel : Container(setOf()) { - private var nav = Tag(TAG.UL, classes = setOf("nav", "nav-tabs")) - private var content = StackPanel(false) +open class TabPanel : SimplePanel(setOf()) { var activeIndex get() = content.activeIndex set(value) { @@ -20,9 +17,12 @@ open class TabPanel : Container(setOf()) { } } + private var nav = Tag(TAG.UL, classes = setOf("nav", "nav-tabs")) + private var content = StackPanel(false) + init { - this.add(nav) - this.add(content) + this.addInternal(nav) + this.addInternal(content) } open fun addTab(title: String, panel: Widget, icon: String? = null, @@ -52,4 +52,25 @@ open class TabPanel : Container(setOf()) { activeIndex = content.activeIndex return this } + + override fun add(child: Widget): TabPanel { + return addTab("", child) + } + + override fun addAll(children: List): TabPanel { + children.forEach { add(it) } + return this + } + + override fun remove(child: Widget): TabPanel { + val index = content.children.indexOf(child) + return removeTab(index) + } + + override fun removeAll(): TabPanel { + content.removeAll() + nav.removeAll() + refresh() + return this + } } -- cgit