From a990385dd3f2dbac4807b7a9498271efb3519c0d Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Fri, 25 Oct 2019 15:08:38 +0200 Subject: Now dropdown align option for Select and RemoteSelect components. --- .../treksoft/kvision/form/select/SelectRemote.kt | 8 ++++ .../kvision/form/select/SelectRemoteInput.kt | 52 +++++++++++++--------- .../pl/treksoft/kvision/form/select/Select.kt | 8 ++++ .../pl/treksoft/kvision/form/select/SelectInput.kt | 24 ++++++++++ 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt index 74cb3694..a81d7f2d 100644 --- a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt +++ b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemote.kt @@ -115,6 +115,14 @@ open class SelectRemote( set(value) { input.selectWidthType = value } + /** + * The dropdown align of the select control. + */ + var dropdownAlign + get() = input.dropdownAlign + set(value) { + input.dropdownAlign = value + } /** * Determines if an empty option is automatically generated. */ diff --git a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt index af3f1a52..f1643e3d 100644 --- a/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt +++ b/kvision-modules/kvision-bootstrap-select-remote/src/main/kotlin/pl/treksoft/kvision/form/select/SelectRemoteInput.kt @@ -68,29 +68,37 @@ open class SelectRemoteInput( q = "{{{q}}}" } val tempAjaxOptions = ajaxOptions ?: AjaxOptions() - this.ajaxOptions = tempAjaxOptions.copy(url = url, preprocessData = { - @Suppress("UnsafeCastFromDynamic") - JSON.plain.parse(RemoteOption.serializer().list, it.result as String).map { - obj { - this.value = it.value - if (it.text != null) this.text = it.text - if (it.className != null) this.`class` = it.className - if (it.disabled) this.disabled = true - if (it.divider) this.divider = true - this.data = obj { - if (it.subtext != null) this.subtext = it.subtext - if (it.icon != null) this.icon = it.icon - if (it.content != null) this.content = it.content + this.ajaxOptions = tempAjaxOptions.copy( + url = url, + preprocessData = { + @Suppress("UnsafeCastFromDynamic") + JSON.plain.parse(RemoteOption.serializer().list, it.result as String).map { + obj { + this.value = it.value + if (it.text != null) this.text = it.text + if (it.className != null) this.`class` = it.className + if (it.disabled) this.disabled = true + if (it.divider) this.divider = true + this.data = obj { + if (it.subtext != null) this.subtext = it.subtext + if (it.icon != null) this.icon = it.icon + if (it.content != null) this.content = it.content + } } - } - }.toTypedArray() - }, data = data, beforeSend = { _, b -> - @Suppress("UnsafeCastFromDynamic") - val q = decodeURIComponent(b.data.substring(2)) - val state = stateFunction?.invoke() - b.data = JSON.plain.stringify(JsonRpcRequest(0, url, listOf(q, this.value, state))) - true - }, httpType = HttpType.valueOf(method.name), cache = false, preserveSelected = true) + }.toTypedArray() + }, + data = data, + beforeSend = { _, b -> + @Suppress("UnsafeCastFromDynamic") + val q = decodeURIComponent(b.data.substring(2)) + val state = stateFunction?.invoke() + b.data = JSON.plain.stringify(JsonRpcRequest(0, url, listOf(q, this.value, state))) + true + }, + httpType = HttpType.valueOf(method.name), + cache = false, + preserveSelected = ajaxOptions?.preserveSelected ?: true + ) if (value != null) { GlobalScope.launch { val callAgent = CallAgent() diff --git a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt index ea98e369..8373dee7 100644 --- a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt +++ b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt @@ -134,6 +134,14 @@ open class Select( set(value) { input.selectWidthType = value } + /** + * The dropdown align of the select control. + */ + var dropdownAlign + get() = input.dropdownAlign + set(value) { + input.dropdownAlign = value + } /** * Determines if an empty option is automatically generated. */ diff --git a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt index b515ea3e..31ae5125 100644 --- a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -44,6 +44,15 @@ enum class SelectWidthType(internal val value: String) { FIT("fit") } +/** + * Select dropdown align. See [Bootstrap Select width](http://silviomoreto.github.io/bootstrap-select/examples/#width). + */ +enum class SelectDropdownAlign { + AUTO, + LEFT, + RIGHT +} + /** * The basic component for Select control. * @@ -113,6 +122,10 @@ open class SelectInput( * The width type of the select control. */ var selectWidthType: SelectWidthType? by refreshOnUpdate() + /** + * The dropdown align of the select control. + */ + var dropdownAlign by refreshOnUpdate(SelectDropdownAlign.LEFT) /** * Determines if an empty option is automatically generated. */ @@ -291,6 +304,15 @@ open class SelectInput( } ?: selectWidth?.let { sn.add("data-width" to it.asString()) } + when (dropdownAlign) { + SelectDropdownAlign.RIGHT -> { + sn.add("data-dropdown-align-right" to "true") + } + SelectDropdownAlign.AUTO -> { + sn.add("data-dropdown-align-right" to "auto") + } + else -> {} + } return sn } @@ -338,6 +360,8 @@ open class SelectInput( getElementJQueryD()?.selectpicker("val", it) } } ?: getElementJQueryD()?.selectpicker("val", null) + } else if (value == null) { + getElementJQueryD()?.selectpicker("val", null) } } -- cgit