aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/navbar/Navbar.kt
blob: 7e83c76410d7e3532ef7dfb4aa357c80bcc2afd1 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (c) 2018. Robert Jaros
 */
package pl.treksoft.kvision.navbar

import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.Component
import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.StringPair
import pl.treksoft.kvision.html.Link
import pl.treksoft.kvision.html.TAG
import pl.treksoft.kvision.html.Tag.Companion.tag
import pl.treksoft.kvision.panel.SimplePanel

/**
 * Navbar types.
 */
enum class NavbarType(internal val navbarType: String) {
    FIXEDTOP("navbar-fixed-top"),
    FIXEDBOTTOM("navbar-fixed-bottom"),
    STATICTOP("navbar-static-top")
}

/**
 * The Bootstrap Navbar container.
 *
 * @constructor
 * @param label the navbar label
 * @param type the navbar type
 * @param inverted determines if the navbar is inverted
 * @param classes a set of CSS class names
 * @param init an initializer extension function
 */
open class Navbar(
    label: String = "",
    type: NavbarType? = null,
    inverted: Boolean = false,
    classes: Set<String> = setOf(), init: (Navbar.() -> Unit)? = null
) : SimplePanel(classes) {

    /**
     * The navbar header label.
     */
    var label
        get() = brandLink.label
        set(value) {
            brandLink.label = value
        }

    /**
     * The navbar type.
     */
    var type by refreshOnUpdate(type)
    /**
     * Determines if the navbar is inverted.
     */
    var inverted by refreshOnUpdate(inverted)

    private val idc = "kv_navbar_$counter"

    private val brandLink = Link(label, "#", classes = setOf("navbar-brand"))
    internal val container = SimplePanel(setOf("collapse", "navbar-collapse")) {
        id = idc
    }

    init {
        val c = SimplePanel(setOf("container-fluid")) {
            simplePanel(setOf("navbar-header")) {
                add(NavbarButton(idc))
                add(brandLink)
            }
            add(container)
        }
        addInternal(c)
        counter++
        @Suppress("LeakingThis")
        init?.invoke(this)
    }

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

    override fun add(child: Component): Navbar {
        container.add(child)
        return this
    }

    override fun addAll(children: List<Component>): Navbar {
        container.addAll(children)
        return this
    }

    override fun remove(child: Component): Navbar {
        container.remove(child)
        return this
    }

    override fun removeAll(): Navbar {
        container.removeAll()
        return this
    }

    override fun getChildren(): List<Component> {
        return container.getChildren()
    }

    override fun getSnClass(): List<StringBoolPair> {
        val cl = super.getSnClass().toMutableList()
        cl.add("navbar" to true)
        type?.let {
            cl.add(it.navbarType to true)
        }
        if (inverted) {
            cl.add("navbar-inverse" to true)
        } else {
            cl.add("navbar-default" to true)
        }
        return cl
    }

    companion object {
        internal var counter = 0

        /**
         * DSL builder extension function.
         *
         * It takes the same parameters as the constructor of the built component.
         */
        fun Container.navbarPanel(
            text: String = "",
            type: NavbarType? = null,
            inverted: Boolean = false,
            classes: Set<String> = setOf(), init: (Navbar.() -> Unit)? = null
        ): Navbar {
            val navbarPanel = Navbar(text, type, inverted, classes, init)
            this.add(navbarPanel)
            return navbarPanel
        }
    }
}

/**
 * @suppress
 * Internal component.
 * The Bootstrap Navbar header button.
 */
internal class NavbarButton(private val idc: String, toggle: String = "Toggle navigation") :
    SimplePanel(setOf("navbar-toggle", "collapsed")) {

    init {
        tag(TAG.SPAN, toggle, classes = setOf("sr-only"))
        tag(TAG.SPAN, classes = setOf("icon-bar"))
        tag(TAG.SPAN, classes = setOf("icon-bar"))
        tag(TAG.SPAN, classes = setOf("icon-bar"))
    }

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

    override fun getSnAttrs(): List<StringPair> {
        return super.getSnAttrs() + listOf(
            "type" to "button",
            "data-toggle" to "collapse",
            "data-target" to "#$idc",
            "aria-expanded" to "false"
        )
    }
}