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

import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.panel.SimplePanel
import pl.treksoft.kvision.snabbdom.SnOn
import pl.treksoft.kvision.snabbdom.StringBoolPair

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

open class Radio(value: Boolean = false, extraValue: String? = null, label: String? = null,
                 rich: Boolean = false) : SimplePanel(), BoolFormField {

    override var value
        get() = input.value
        set(value) {
            input.value = value
        }
    var startValue
        get() = input.startValue
        set(value) {
            input.startValue = value
        }
    var extraValue
        get() = input.extraValue
        set(value) {
            input.extraValue = value
        }
    var name
        get() = input.name
        set(value) {
            input.name = value
        }
    override var disabled
        get() = input.disabled
        set(value) {
            input.disabled = value
        }
    var label
        get() = flabel.text
        set(value) {
            flabel.text = value
        }
    var rich
        get() = flabel.rich
        set(value) {
            flabel.rich = value
        }
    var style: RADIOSTYLE? = null
        set(value) {
            field = value
            refresh()
        }
    var squared: Boolean = false
        set(value) {
            field = value
            refresh()
        }
    var inline: Boolean = false
        set(value) {
            field = value
            refresh()
        }
    override var size
        get() = input.size
        set(value) {
            input.size = value
        }

    private val idc = "kv_form_radio_" + counter
    val input: CheckInput = CheckInput(CHECKINPUTTYPE.RADIO, value).apply {
        this.id = idc
        this.extraValue = extraValue
    }
    val flabel: FieldLabel = FieldLabel(idc, label, rich)

    init {
        @Suppress("LeakingThis")
        input.eventTarget = this
        this.addInternal(input)
        this.addInternal(flabel)
        counter++
    }

    companion object {
        var counter = 0
    }

    @Suppress("UNCHECKED_CAST")
    override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget {
        input.setEventListener(block)
        return this
    }

    override fun setEventListener(block: SnOn<Widget>.() -> Unit): Widget {
        input.setEventListener(block)
        return this
    }

    override fun removeEventListeners(): Widget {
        input.removeEventListeners()
        return this
    }

    override fun getSnClass(): List<StringBoolPair> {
        val cl = super.getSnClass().toMutableList()
        if (!squared) {
            cl.add("radio" to true)
            style?.let {
                cl.add(it.className to true)
            }
            if (inline) {
                cl.add("radio-inline" to true)
            }
        } else {
            cl.add("checkbox" to true)
            style?.let {
                cl.add(it.className.replace("radio", "checkbox") to true)
            }
            if (inline) {
                cl.add("checkbox-inline" to true)
            }
        }
        return cl
    }
}