blob: 7ef297547e1913b9a6c94b7fe2c1ee55167ebd63 (
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
package pl.treksoft.kvision.form
import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.CssSize
import pl.treksoft.kvision.html.BUTTONSTYLE
import pl.treksoft.kvision.panel.SimplePanel
import pl.treksoft.kvision.snabbdom.StringBoolPair
import pl.treksoft.kvision.snabbdom.StringPair
private val _aKVNULL = "#kvnull"
enum class SELECTWIDTHTYPE(val value: String) {
AUTO("auto"),
FIT("fit")
}
class SelectInput(options: List<StringPair>? = null, override var value: String? = null,
multiple: Boolean = false, classes: Set<String> = setOf()) : SimplePanel(classes), StringFormField {
internal var options = options
set(value) {
field = value
setChildrenFromOptions()
}
@Suppress("LeakingThis")
var startValue: String? = value
set(value) {
field = value
this.value = value
refresh()
}
var name: String? = null
set(value) {
field = value
refresh()
}
var multiple: Boolean = multiple
set(value) {
field = value
refresh()
}
var maxOptions: Int? = null
set(value) {
field = value
refresh()
}
var liveSearch: Boolean = false
set(value) {
field = value
refresh()
}
var placeholder: String? = null
set(value) {
field = value
refresh()
}
var style: BUTTONSTYLE? = null
set(value) {
field = value
refresh()
}
var selectWidth: CssSize? = null
set(value) {
field = value
refresh()
}
var selectWidthType: SELECTWIDTHTYPE? = null
set(value) {
field = value
refresh()
}
var emptyOption: Boolean = false
set(value) {
field = value
setChildrenFromOptions()
}
override var disabled: Boolean = false
set(value) {
field = value
refresh()
}
var autofocus: Boolean? = null
set(value) {
field = value
refresh()
}
override var size: INPUTSIZE? = null
set(value) {
field = value
refresh()
}
init {
setChildrenFromOptions()
this.setInternalEventListener<SelectInput> {
change = {
val v = getElementJQuery()?.`val`()
self.value = v?.let {
if (self.multiple) {
@Suppress("UNCHECKED_CAST")
val arr = it as? Array<String>
if (arr != null && arr.isNotEmpty()) {
arr.joinToString()
} else {
null
}
} else {
val vs = it as String
if (_aKVNULL == vs) {
null
} else {
vs
}
}
}
}
}
}
override fun render(): VNode {
return kvh("select", childrenVNodes())
}
private fun setChildrenFromOptions() {
this.removeAll()
if (emptyOption) {
this.add(SelectOption(_aKVNULL, ""))
}
options?.let {
val c = it.map {
SelectOption(it.first, it.second)
}
this.addAll(c)
}
}
override fun getSnClass(): List<StringBoolPair> {
val cl = super.getSnClass().toMutableList()
cl.add("selectpicker" to true)
size?.let {
cl.add(it.className to true)
}
return cl
}
override fun getSnAttrs(): List<StringPair> {
val sn = super.getSnAttrs().toMutableList()
name?.let {
sn.add("name" to it)
}
if (multiple) {
sn.add("multiple" to "true")
}
maxOptions?.let {
sn.add("data-max-options" to "" + it)
}
if (liveSearch) {
sn.add("data-live-search" to "true")
}
placeholder?.let {
sn.add("title" to it)
}
autofocus?.let {
if (it) {
sn.add("autofocus" to "autofocus")
}
}
if (disabled) {
sn.add("disabled" to "true")
}
val btnStyle = style?.className ?: "btn-default"
when (size) {
INPUTSIZE.LARGE -> {
sn.add("data-style" to "$btnStyle btn-lg")
}
INPUTSIZE.SMALL -> {
sn.add("data-style" to "$btnStyle btn-sm")
}
else -> {
sn.add("data-style" to btnStyle)
}
}
selectWidthType?.let {
sn.add("data-width" to it.value)
} ?: selectWidth?.let {
sn.add("data-width" to it.first.toString() + it.second.unit)
}
return sn
}
@Suppress("UnsafeCastFromDynamic")
override fun afterInsert(node: VNode) {
getElementJQueryD()?.selectpicker("render")
value?.let {
if (multiple) {
getElementJQueryD()?.selectpicker("val", it.split(",").toTypedArray())
} else {
getElementJQueryD()?.selectpicker("val", it)
}
}
}
}
|