aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt
blob: 18946b2712cb588c129d1c97f146720bc6a9b02e (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
package pl.treksoft.kvision.dropdown

import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.ResString
import pl.treksoft.kvision.html.BUTTONSIZE
import pl.treksoft.kvision.html.BUTTONSTYLE
import pl.treksoft.kvision.html.Button
import pl.treksoft.kvision.html.LIST
import pl.treksoft.kvision.html.Link
import pl.treksoft.kvision.html.ListTag
import pl.treksoft.kvision.html.TAG
import pl.treksoft.kvision.html.Tag
import pl.treksoft.kvision.snabbdom.StringPair

enum class DD(val POS: String) {
    HEADER("DD#HEADER"),
    DISABLED("DD#DISABLED"),
    SEPARATOR("DD#SEPARATOR")
}

open class DropDown(text: String, elements: List<StringPair>, icon: String? = null,
                    style: BUTTONSTYLE = BUTTONSTYLE.DEFAULT, size: BUTTONSIZE? = null,
                    block: Boolean = false, disabled: Boolean = false, image: ResString? = null,
                    dropup: Boolean = false, classes: Set<String> = setOf()) : Container(classes) {
    val idc = "kv_dropdown_" + counter
    val button: DropDownButton = DropDownButton(idc, text, icon, style, size, block,
            disabled, image, setOf("dropdown"))
    val list: DropDownListTag = DropDownListTag(idc, setOf("dropdown-menu"))

    init {
        this.addCssClass(if (dropup) "dropup" else "dropdown")
        val children = elements.map {
            when (it.second) {
                DD.HEADER.POS -> Tag(TAG.LI, it.first, classes = setOf("dropdown-header"))
                DD.SEPARATOR.POS -> {
                    val tag = Tag(TAG.LI, it.first, classes = setOf("divider"))
                    tag.role = "separator"
                    tag
                }
                DD.DISABLED.POS -> {
                    val tag = Tag(TAG.LI, classes = setOf("disabled"))
                    tag.add(Link(it.first, "#"))
                    tag
                }
                else -> Link(it.first, it.second)
            }
        }
        list.addAll(children)
        this.add(button)
        this.add(list)
        counter++
    }

    companion object {
        var counter = 0
    }
}

open class DropDownButton(id: String, text: String, icon: String? = null, style: BUTTONSTYLE = BUTTONSTYLE.DEFAULT,
                          size: BUTTONSIZE? = null, block: Boolean = false, disabled: Boolean = false,
                          image: ResString? = null, classes: Set<String> = setOf()) :
        Button(text, icon, style, size, block, disabled, image, classes) {

    init {
        this.id = id
    }

    override fun getSnAttrs(): List<StringPair> {
        return super.getSnAttrs() + listOf("data-toggle" to "dropdown", "aria-haspopup" to "true",
                "aria-expanded" to "false")
    }
}

open class DropDownListTag(val ariaId: String, classes: Set<String> = setOf()) : ListTag(LIST.UL, null,
        false, classes) {
    override fun getSnAttrs(): List<StringPair> {
        return super.getSnAttrs() + listOf("aria-labelledby" to ariaId)
    }
}