aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/panel/SimplePanel.kt
blob: 51c42a060788d8e2784fe6fc44cf224bb20402ba (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
58
59
60
package pl.treksoft.kvision.panel

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

open class SimplePanel(classes: Set<String> = setOf()) : Widget(classes), Container {
    internal val children: MutableList<Component> = mutableListOf()

    override fun render(): VNode {
        return kvh("div", childrenVNodes())
    }

    protected open fun childrenVNodes(): Array<VNode> {
        return children.filter { it.visible }.map { it.renderVNode() }.toTypedArray()
    }

    protected fun addInternal(child: Component): SimplePanel {
        children.add(child)
        child.parent = this
        refresh()
        return this
    }

    override fun add(child: Component): SimplePanel {
        return addInternal(child)
    }

    override fun addAll(children: List<Component>): SimplePanel {
        this.children.addAll(children)
        children.map { it.parent = this }
        refresh()
        return this
    }

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

    override fun dispose() {
        children.forEach { it.dispose() }
        removeAll()
    }
}