aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt
blob: 88634bea324764b172be8ede12c86025d6eee0eb (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.KVManager
import pl.treksoft.kvision.modal.Modal
import pl.treksoft.kvision.core.StringBoolPair

class Root(id: String, private val fixed: Boolean = false) : SimplePanel() {
    private val modals: MutableList<Modal> = mutableListOf()
    private var rootVnode: VNode = renderVNode()

    internal var renderDisabled = false

    init {
        rootVnode = KVManager.patch(id, this.renderVNode())
        this.id = id
        roots.add(this)
    }

    override fun render(): VNode {
        return kvh("div#" + id, childrenVNodes() + modalsVNodes())
    }

    internal fun addModal(modal: Modal) {
        modals.add(modal)
        modal.parent = this
        refresh()
    }

    private fun modalsVNodes(): Array<VNode> {
        return modals.filter { it.visible }.map { it.renderVNode() }.toTypedArray()
    }

    override fun getSnClass(): List<StringBoolPair> {
        val css = if (!fixed) "container-fluid" else "container"
        return super.getSnClass() + (css to true)
    }

    internal fun reRender(): Root {
        if (!renderDisabled) {
            rootVnode = KVManager.patch(rootVnode, renderVNode())
        }
        return this
    }

    override fun getRoot(): Root? {
        return this
    }

    companion object {
        private val roots: MutableList<Root> = mutableListOf()

        internal fun getLastRoot(): Root? {
            return if (roots.size > 0)
                roots[roots.size - 1]
            else
                null
        }
    }
}