diff options
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
3 files changed, 59 insertions, 19 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt index 8dbb5f65..d86e6899 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt @@ -1,5 +1,7 @@ package pl.treksoft.kvision.form.select +import pl.treksoft.kvision.core.KVManager.AJAX_REQUEST_DELAY +import pl.treksoft.kvision.core.KVManager.KVNULL import pl.treksoft.kvision.snabbdom.obj enum class HttpType(val type: String) { @@ -20,17 +22,38 @@ data class AjaxOptions(val url: String, val processData: (dynamic) -> dynamic, val dataType: DataType = DataType.JSON, val minLength: Int = 0, val cache: Boolean = true, val clearOnEmpty: Boolean = true, val clearOnError: Boolean = true, val emptyRequest: Boolean = false, val preserveSelected: Boolean = true, - val requestDelay: Int = 300, val restoreOnError: Boolean = false) + val requestDelay: Int = AJAX_REQUEST_DELAY, val restoreOnError: Boolean = false) -fun AjaxOptions.toJs(): dynamic { +fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { + val procData = { data: dynamic -> + val processedData = this.processData(data) + if (emptyOption) { + val ret = mutableListOf(obj { + this.value = KVNULL + this.text = "" + }) + @Suppress("UnsafeCastFromDynamic") + ret.addAll((processedData as Array<dynamic>).asList()) + ret.toTypedArray() + } else { + processedData + } + } return obj { this.ajax = obj { this.url = url this.type = httpType.type + this.dataType = dataType.type this.data = processParams } - this.preprocessData = processData + this.preprocessData = procData + this.minLength = minLength + this.cache = cache + this.clearOnEmpty = clearOnEmpty + this.clearOnError = clearOnError + this.emptyRequest = emptyRequest + this.preserveSelected = preserveSelected + this.requestDelay = requestDelay + this.restoreOnError = restoreOnError } } - -data class AjaxData(val value: String, val text: String? = null) diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt index 321e3a70..4b991605 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt @@ -7,8 +7,9 @@ import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.snabbdom.SnOn import pl.treksoft.kvision.snabbdom.StringPair +@Suppress("TooManyFunctions") open class Select(options: List<StringPair>? = null, value: String? = null, - multiple: Boolean = false, label: String? = null, + multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, rich: Boolean = false) : SimplePanel(setOf("form-group")), StringFormField { var options @@ -36,6 +37,11 @@ open class Select(options: List<StringPair>? = null, value: String? = null, set(value) { input.multiple = value } + var ajaxOptions + get() = input.ajaxOptions + set(value) { + input.ajaxOptions = value + } var maxOptions get() = input.maxOptions set(value) { @@ -93,7 +99,8 @@ open class Select(options: List<StringPair>? = null, value: String? = null, } private val idc = "kv_form_select_" + counter - val input: SelectInput = SelectInput(options, value, multiple, null, setOf("form-control")).apply { id = idc } + val input: SelectInput = SelectInput(options, value, multiple, ajaxOptions, + setOf("form-control")).apply { id = idc } val flabel: FieldLabel = FieldLabel(idc, label, rich) init { @@ -159,4 +166,8 @@ open class Select(options: List<StringPair>? = null, value: String? = null, open fun toggleOptions() { input.toggleOptions() } + + open fun deselect() { + input.deselect() + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt index 7522ef98..c95cf434 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -2,6 +2,7 @@ package pl.treksoft.kvision.form.select import com.github.snabbdom.VNode import pl.treksoft.kvision.core.CssSize +import pl.treksoft.kvision.core.KVManager.KVNULL import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.INPUTSIZE import pl.treksoft.kvision.form.StringFormField @@ -11,13 +12,12 @@ 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") } +@Suppress("TooManyFunctions") open class SelectInput(options: List<StringPair>? = null, value: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, classes: Set<String> = setOf()) : SimplePanel(classes), StringFormField { @@ -125,7 +125,7 @@ open class SelectInput(options: List<StringPair>? = null, value: String? = null, } } else { val vs = it as String - if (_aKVNULL == vs) { + if (KVNULL == vs) { null } else { vs @@ -165,15 +165,17 @@ open class SelectInput(options: List<StringPair>? = null, value: String? = null, } private fun setChildrenFromOptions() { - super.removeAll() - if (emptyOption) { - super.add(SelectOption(_aKVNULL, "")) - } - options?.let { - val c = it.map { - SelectOption(it.first, it.second) + if (ajaxOptions == null) { + super.removeAll() + if (emptyOption) { + super.add(SelectOption(KVNULL, "")) + } + options?.let { + val c = it.map { + SelectOption(it.first, it.second) + } + super.addAll(c) } - super.addAll(c) } this.refreshSelectInput() } @@ -190,6 +192,10 @@ open class SelectInput(options: List<StringPair>? = null, value: String? = null, getElementJQueryD()?.selectpicker("toggle") } + open fun deselect() { + getElementJQueryD()?.selectpicker("deselectAll") + } + override fun getSnClass(): List<StringBoolPair> { val cl = super.getSnClass().toMutableList() cl.add("selectpicker" to true) @@ -253,7 +259,7 @@ open class SelectInput(options: List<StringPair>? = null, value: String? = null, @Suppress("UnsafeCastFromDynamic") override fun afterInsert(node: VNode) { ajaxOptions?.let { - getElementJQueryD()?.selectpicker("render").ajaxSelectPicker(it.toJs()) + getElementJQueryD()?.selectpicker("render").ajaxSelectPicker(it.toJs(emptyOption)) } ?: getElementJQueryD()?.selectpicker("render") this.getElementJQuery()?.on("show.bs.select", { e, _ -> |