diff options
author | Robert Jaros <rjaros@finn.pl> | 2017-11-06 00:20:25 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2017-11-06 00:20:25 +0100 |
commit | 1c742ec958a043519e58dcf53bb3b3c92bf3e94a (patch) | |
tree | 0d4804a58b0a59f28029c8f65897caf70e4ade35 /src/main/kotlin/pl/treksoft/kvision/form | |
parent | 2b0f687b17bdf0144262a20556446cfbf50a4f34 (diff) | |
download | kvision-1c742ec958a043519e58dcf53bb3b3c92bf3e94a.tar.gz kvision-1c742ec958a043519e58dcf53bb3b3c92bf3e94a.tar.bz2 kvision-1c742ec958a043519e58dcf53bb3b3c92bf3e94a.zip |
Ajax support for SelectInput
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt | 36 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt (renamed from src/main/kotlin/pl/treksoft/kvision/form/Select.kt) | 6 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt (renamed from src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt) | 34 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt (renamed from src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt) | 2 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt (renamed from src/main/kotlin/pl/treksoft/kvision/form/SelectOption.kt) | 2 |
5 files changed, 65 insertions, 15 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 new file mode 100644 index 00000000..8dbb5f65 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt @@ -0,0 +1,36 @@ +package pl.treksoft.kvision.form.select + +import pl.treksoft.kvision.snabbdom.obj + +enum class HttpType(val type: String) { + GET("GET"), + POST("POST") +} + +enum class DataType(val type: String) { + JSON("json"), + JSONP("jsonp"), + XML("xml"), + TEXT("text"), + SCRIPT("script") +} + +data class AjaxOptions(val url: String, val processData: (dynamic) -> dynamic, + val processParams: dynamic = null, val httpType: HttpType = HttpType.GET, + 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) + +fun AjaxOptions.toJs(): dynamic { + return obj { + this.ajax = obj { + this.url = url + this.type = httpType.type + this.data = processParams + } + this.preprocessData = processData + } +} + +data class AjaxData(val value: String, val text: String? = null) diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Select.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt index 4ed04cc8..321e3a70 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Select.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt @@ -1,6 +1,8 @@ -package pl.treksoft.kvision.form +package pl.treksoft.kvision.form.select import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.form.FieldLabel +import pl.treksoft.kvision.form.StringFormField import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.snabbdom.SnOn import pl.treksoft.kvision.snabbdom.StringPair @@ -91,7 +93,7 @@ open class Select(options: List<StringPair>? = null, value: String? = null, } private val idc = "kv_form_select_" + counter - val input: SelectInput = SelectInput(options, value, multiple, setOf("form-control")).apply { id = idc } + val input: SelectInput = SelectInput(options, value, multiple, null, setOf("form-control")).apply { id = idc } val flabel: FieldLabel = FieldLabel(idc, label, rich) init { diff --git a/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt index 7cacd422..7522ef98 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -1,8 +1,10 @@ -package pl.treksoft.kvision.form +package pl.treksoft.kvision.form.select import com.github.snabbdom.VNode import pl.treksoft.kvision.core.CssSize import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.form.INPUTSIZE +import pl.treksoft.kvision.form.StringFormField import pl.treksoft.kvision.html.BUTTONSTYLE import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.snabbdom.StringBoolPair @@ -17,7 +19,8 @@ enum class SELECTWIDTHTYPE(val value: String) { } open class SelectInput(options: List<StringPair>? = null, value: String? = null, - multiple: Boolean = false, classes: Set<String> = setOf()) : SimplePanel(classes), StringFormField { + multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, + classes: Set<String> = setOf()) : SimplePanel(classes), StringFormField { internal var options = options set(value) { @@ -49,6 +52,12 @@ open class SelectInput(options: List<StringPair>? = null, value: String? = null, field = value refresh() } + var ajaxOptions: AjaxOptions? = ajaxOptions + set(value) { + field = value + if (value != null) liveSearch = true + refresh() + } var maxOptions: Int? = null set(value) { field = value @@ -243,31 +252,34 @@ open class SelectInput(options: List<StringPair>? = null, value: String? = null, @Suppress("UnsafeCastFromDynamic") override fun afterInsert(node: VNode) { - getElementJQueryD()?.selectpicker("render") + ajaxOptions?.let { + getElementJQueryD()?.selectpicker("render").ajaxSelectPicker(it.toJs()) + } ?: getElementJQueryD()?.selectpicker("render") + this.getElementJQuery()?.on("show.bs.select", { e, _ -> - this.dispatchEvent("showBsSelect", obj({ detail = e })) + this.dispatchEvent("showBsSelect", obj { detail = e }) }) this.getElementJQuery()?.on("shown.bs.select", { e, _ -> - this.dispatchEvent("shownBsSelect", obj({ detail = e })) + this.dispatchEvent("shownBsSelect", obj { detail = e }) }) this.getElementJQuery()?.on("hide.bs.select", { e, _ -> - this.dispatchEvent("hideBsSelect", obj({ detail = e })) + this.dispatchEvent("hideBsSelect", obj { detail = e }) }) this.getElementJQuery()?.on("hidden.bs.select", { e, _ -> - this.dispatchEvent("hiddenBsSelect", obj({ detail = e })) + this.dispatchEvent("hiddenBsSelect", obj { detail = e }) }) this.getElementJQuery()?.on("loaded.bs.select", { e, _ -> - this.dispatchEvent("loadedBsSelect", obj({ detail = e })) + this.dispatchEvent("loadedBsSelect", obj { detail = e }) }) this.getElementJQuery()?.on("rendered.bs.select", { e, _ -> - this.dispatchEvent("renderedBsSelect", obj({ detail = e })) + this.dispatchEvent("renderedBsSelect", obj { detail = e }) }) this.getElementJQuery()?.on("refreshed.bs.select", { e, _ -> - this.dispatchEvent("refreshedBsSelect", obj({ detail = 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 })) + this.dispatchEvent("changedBsSelect", obj { detail = e }) }) refreshState() } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt index 513077ef..38a578fe 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt @@ -1,4 +1,4 @@ -package pl.treksoft.kvision.form +package pl.treksoft.kvision.form.select import com.github.snabbdom.VNode import pl.treksoft.kvision.panel.SimplePanel diff --git a/src/main/kotlin/pl/treksoft/kvision/form/SelectOption.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt index f3be295c..3ff44c61 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/SelectOption.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt @@ -1,4 +1,4 @@ -package pl.treksoft.kvision.form +package pl.treksoft.kvision.form.select import com.github.snabbdom.VNode import pl.treksoft.kvision.core.Widget |