aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt
blob: e8975dce006aa2633865d61c1df10f0c87bcc9ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package pl.treksoft.kvision.panel

import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.Widget

open class StackPanel(private val activateLast: Boolean = true,
                      classes: Set<String> = setOf()) : SimplePanel(classes) {
    var activeIndex = -1
        set(value) {
            field = value
            refresh()
        }

    override fun childrenVNodes(): Array<VNode> {
        return if (activeIndex >= 0 && activeIndex < children.size) {
            arrayOf(children[activeIndex].renderVNode())
        } else {
            arrayOf()
        }
    }

    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<Widget>): StackPanel {
        super.addAll(children)
        if (activateLast) activeIndex = this.children.size - 1
        else if (activeIndex == -1) activeIndex = 0
        return this
    }

    override fun remove(child: Widget): StackPanel {
        super.remove(child)
        if (activeIndex > children.size - 1) activeIndex = children.size - 1
        return this
    }

    override fun removeAll(): StackPanel {
        super.removeAll()
        if (activeIndex > children.size - 1) activeIndex = children.size - 1
        return this
    }
}