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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
package pl.treksoft.kvision.form
import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.CssSize
import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.html.BUTTONSTYLE
import pl.treksoft.kvision.panel.SimplePanel
import pl.treksoft.kvision.snabbdom.StringBoolPair
import pl.treksoft.kvision.snabbdom.StringPair
import pl.treksoft.kvision.snabbdom.obj
private val _aKVNULL = "#kvnull"
enum class SELECTWIDTHTYPE(val value: String) {
AUTO("auto"),
FIT("fit")
}
open class SelectInput(options: List<StringPair>? = null, value: String? = null,
multiple: Boolean = false, classes: Set<String> = setOf()) : SimplePanel(classes), StringFormField {
internal var options = options
set(value) {
field = value
setChildrenFromOptions()
}
@Suppress("LeakingThis")
override var value: String? = value
set(value) {
field = value
refreshState()
}
@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())
}
override fun add(child: Widget): SimplePanel {
super.add(child)
refreshSelectInput()
return this
}
override fun addAll(children: List<Widget>): SimplePanel {
super.addAll(children)
refreshSelectInput()
return this
}
override fun remove(child: Widget): SimplePanel {
super.remove(child)
refreshSelectInput()
return this
}
override fun removeAll(): SimplePanel {
super.removeAll()
refreshSelectInput()
return this
}
private fun setChildrenFromOptions() {
super.removeAll()
if (emptyOption) {
super.add(SelectOption(_aKVNULL, ""))
}
options?.let {
val c = it.map {
SelectOption(it.first, it.second)
}
super.addAll(c)
}
this.refreshSelectInput()
}
open fun showOptions() {
getElementJQueryD()?.selectpicker("show")
}
open fun hideOptions() {
getElementJQueryD()?.selectpicker("hide")
}
open fun toggleOptions() {
getElementJQueryD()?.selectpicker("toggle")
}
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
}
fun refreshSelectInput() {
getElementJQueryD()?.selectpicker("refresh")
refreshState()
}
@Suppress("ComplexMethod")
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")
this.getElementJQuery()?.on("show.bs.select", { e, _ ->
this.dispatchEvent("showBsSelect", obj({ detail = e }))
})
this.getElementJQuery()?.on("shown.bs.select", { e, _ ->
this.dispatchEvent("shownBsSelect", obj({ detail = e }))
})
this.getElementJQuery()?.on("hide.bs.select", { e, _ ->
this.dispatchEvent("hideBsSelect", obj({ detail = e }))
})
this.getElementJQuery()?.on("hidden.bs.select", { e, _ ->
this.dispatchEvent("hiddenBsSelect", obj({ detail = e }))
})
this.getElementJQuery()?.on("loaded.bs.select", { e, _ ->
this.dispatchEvent("loadedBsSelect", obj({ detail = e }))
})
this.getElementJQuery()?.on("rendered.bs.select", { e, _ ->
this.dispatchEvent("renderedBsSelect", obj({ detail = e }))
})
this.getElementJQuery()?.on("refreshed.bs.select", { e, _ ->
this.dispatchEvent("refreshedBsSelect", obj({ detail = e }))
})
this.getElementJQueryD()?.on("changed.bs.select", { e, cIndex: Int ->
e["clickedIndex"] = cIndex
this.dispatchEvent("changedBsSelect", obj({ detail = e }))
})
refreshState()
}
@Suppress("UnsafeCastFromDynamic")
private fun refreshState() {
value?.let {
if (multiple) {
getElementJQueryD()?.selectpicker("val", it.split(",").toTypedArray())
} else {
getElementJQueryD()?.selectpicker("val", it)
}
} ?: getElementJQueryD()?.selectpicker("val", null)
}
}
|