aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/tabs/Tabs.kt
blob: 4bc4d4b6eed0e7c3333557ef4ea037723a85de28 (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
package pl.treksoft.kvision.tabs

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
import pl.treksoft.kvision.panel.StackPanel

open class Tabs : Container(setOf()) {
    internal var nav = Tag(TAG.UL, classes = setOf("nav", "nav-tabs"))
    internal var content = StackPanel(false)
    var activeIndex
        get() = content.activeIndex
        set(value) {
            content.activeIndex = value
            nav.children.forEach { it.removeCssClass("active") }
            if (content.activeIndex >= 0 && content.activeIndex <= nav.children.size) {
                nav.children[content.activeIndex].addCssClass("active")
            }
        }

    init {
        this.add(nav)
        this.add(content)
    }

    open fun addTab(title: String, panel: Widget, icon: String? = null,
                    image: ResString? = null): Tabs {
        val tag = Tag(TAG.LI)
        tag.role = "presentation"
        tag.add(Link(title, "#", icon, image))
        val index = nav.children.size
        tag.setEventListener {
            click = { e ->
                activeIndex = index
                e.preventDefault()
            }
        }
        nav.add(tag)
        if (nav.children.size == 1) {
            tag.addCssClass("active")
            activeIndex = 0
        }
        content.add(panel)
        return this
    }

}