aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt
blob: e69cc416ed8f8f90f62fd3b3143bc42643cbfe5d (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
48
49
50
51
52
53
54
55
56
57
package pl.treksoft.kvision.panel

import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.Component
import pl.treksoft.kvision.routing.routing

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()
        }
    }

    open fun add(panel: Component, route: String): StackPanel {
        add(panel)
        val currentIndex = children.size - 1
        routing.on(route, { _ -> activeIndex = currentIndex }).resolve()
        return this
    }

    override fun add(child: Component): StackPanel {
        super.add(child)
        if (activateLast) activeIndex = children.size - 1
        else if (activeIndex == -1) activeIndex = 0
        return this
    }

    override fun addAll(children: List<Component>): StackPanel {
        super.addAll(children)
        if (activateLast) activeIndex = this.children.size - 1
        else if (activeIndex == -1) activeIndex = 0
        return this
    }

    override fun remove(child: Component): 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
    }
}