aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/html/Button.kt
blob: ec0fd881481a32ee66697792bfb36bc28549347d (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
package pl.treksoft.kvision.html

import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.ResString
import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.snabbdom.StringBoolPair
import pl.treksoft.kvision.snabbdom.StringPair

enum class BUTTONSTYLE(val className: String) {
    DEFAULT("btn-default"),
    PRIMARY("btn-primary"),
    SUCCESS("btn-success"),
    INFO("btn-info"),
    WARNING("btn-warning"),
    DANGER("btn-danger"),
    LINK("btn-link")
}

enum class BUTTONSIZE(val className: String) {
    LARGE("btn-lg"),
    SMALL("btn-sm"),
    XSMALL("btn-xs")
}

open class Button(
    text: String, icon: String? = null, style: BUTTONSTYLE = BUTTONSTYLE.DEFAULT,
    disabled: Boolean = false, classes: Set<String> = setOf()
) : Widget(classes) {
    var text = text
        set(value) {
            field = value
            refresh()
        }
    var icon = icon
        set(value) {
            field = value
            refresh()
        }
    var style = style
        set(value) {
            field = value
            refresh()
        }
    var disabled = disabled
        set(value) {
            field = value
            refresh()
        }
    var image: ResString? = null
        set(value) {
            field = value
            refresh()
        }
    var size: BUTTONSIZE? = null
        set(value) {
            field = value
            refresh()
        }
    var block = false
        set(value) {
            field = value
            refresh()
        }

    override fun render(): VNode {
        val t = createLabelWithIcon(text, icon, image)
        return kvh("button", t)
    }

    override fun getSnClass(): List<StringBoolPair> {
        val cl = super.getSnClass().toMutableList()
        cl.add("btn" to true)
        cl.add(style.className to true)
        size?.let {
            cl.add(it.className to true)
        }
        if (block) {
            cl.add("btn-block" to true)
        }
        if (disabled) {
            cl.add("disabled" to true)
        }
        return cl
    }

    override fun getSnAttrs(): List<StringPair> {
        return super.getSnAttrs() + ("type" to "button")
    }

}