diff options
| author | Robert Jaros <rjaros@finn.pl> | 2018-10-08 13:40:04 +0200 |
|---|---|---|
| committer | Robert Jaros <rjaros@finn.pl> | 2018-10-08 13:40:04 +0200 |
| commit | 0d3e7c87bf74948f83ce006a1d340b0f3f5e68b0 (patch) | |
| tree | c86d5d0251da1fa3b208f53d1ca7d634a6cbcc8e /src/main/kotlin/pl/treksoft/kvision/form | |
| parent | 91ae1d02de111f15608e84193c10ed17bea9fe41 (diff) | |
| download | kvision-0d3e7c87bf74948f83ce006a1d340b0f3f5e68b0.tar.gz kvision-0d3e7c87bf74948f83ce006a1d340b0f3f5e68b0.tar.bz2 kvision-0d3e7c87bf74948f83ce006a1d340b0f3f5e68b0.zip | |
Refactoring to modules
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
16 files changed, 1 insertions, 2999 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index 323d0e1e..b45eed60 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -26,11 +26,9 @@ import kotlinx.serialization.Mapper import kotlinx.serialization.decode import kotlinx.serialization.json.JSON import kotlinx.serialization.serializer -import pl.treksoft.kvision.form.upload.Upload import pl.treksoft.kvision.i18n.I18n.trans import pl.treksoft.kvision.types.KDate import pl.treksoft.kvision.types.KFile -import pl.treksoft.kvision.utils.getContent import kotlin.js.Json import kotlin.reflect.KProperty1 @@ -255,22 +253,6 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali } /** - * Returns file with the content read. - * @param key key identifier of the control - * @param kFile object identifying the file - * @return KFile object - */ - @Suppress("EXPERIMENTAL_FEATURE_WARNING") - suspend fun getContent( - key: KProperty1<K, List<KFile>?>, - kFile: KFile - ): KFile { - val control = getControl(key) as Upload - val content = control.getNativeFile(kFile)?.getContent() - return kFile.copy(content = content) - } - - /** * Returns current data model as JSON. * @return data model as JSON */ diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index a013c076..3811ed82 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -375,20 +375,6 @@ open class FormPanel<K : Any>( } /** - * Returns an object with the content of the file. - * @param key key identifier of the control - * @param kFile object identifying the file - * @return KFile object - */ - @Suppress("EXPERIMENTAL_FEATURE_WARNING") - suspend fun getContent( - key: KProperty1<K, List<KFile>?>, - kFile: KFile - ): KFile { - return form.getContent(key, kFile) - } - - /** * Returns current data model as JSON. * @return data model as JSON */ diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt index b4ed127a..fdc1e3af 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt @@ -28,7 +28,6 @@ import pl.treksoft.kvision.form.FieldLabel import pl.treksoft.kvision.form.HelpBlock import pl.treksoft.kvision.form.InputSize import pl.treksoft.kvision.form.StringFormControl -import pl.treksoft.kvision.form.select.Select import pl.treksoft.kvision.panel.SimplePanel /** @@ -99,7 +98,7 @@ open class RadioGroup( setSizeToChildren(value) } - private val idc = "kv_form_radiogroup_" + Select.counter + private val idc = "kv_form_radiogroup_$counter" final override val input = CheckInput() final override val flabel: FieldLabel = FieldLabel(idc, label, rich) final override val validationInfo: HelpBlock = HelpBlock().apply { visible = false } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt deleted file mode 100644 index d802a111..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import pl.treksoft.jquery.JQueryXHR -import pl.treksoft.kvision.KVManager.AJAX_REQUEST_DELAY -import pl.treksoft.kvision.KVManager.KVNULL -import pl.treksoft.kvision.i18n.I18n -import pl.treksoft.kvision.utils.obj - -/** - * HTTP protocol type for the AJAX call. - */ -enum class HttpType(internal val type: String) { - GET("GET"), - POST("POST") -} - -/** - * Data type for the AJAX call. - */ -enum class DataType(internal val type: String) { - JSON("json"), - JSONP("jsonp"), - XML("xml"), - TEXT("text"), - SCRIPT("script") -} - -/** - * Data class for AJAX options. - * - * @constructor - * @param url the url address - * @param preprocessData - * [AjaxBootstrapSelect preprocessOption](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionspreprocessdata) - * option - * @param beforeSend - * [JQuery ajax.beforeSend](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) option - * @param data - * [JQuery ajax.data](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) option - * @param httpType - * [JQuery ajax.type](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) option - * @param minLength - * [AjaxBootstrapSelect minLength](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsminlength) option - * @param cache - * [AjaxBootstrapSelect cache](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionscache) option - * @param clearOnEmpty - * [AjaxBootstrapSelect clearOnEmpty](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsclearonempty) option - * @param clearOnError - * [AjaxBootstrapSelect clearOnError](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsclearonerror) option - * @param emptyRequest - * [AjaxBootstrapSelect emptyRequest](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsemptyrequest) option - * @param requestDelay - * [AjaxBootstrapSelect requestDelay](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsrequestdelay) option - * @param restoreOnError - * [AjaxBootstrapSelect restoreOnError](https://github.com/truckingsim/Ajax-Bootstrap-Select#optionsrestoreonerror) - * option - */ -data class AjaxOptions( - val url: String, val preprocessData: (dynamic) -> dynamic, val beforeSend: ((JQueryXHR) -> dynamic)? = null, - val data: 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 requestDelay: Int = AJAX_REQUEST_DELAY, val restoreOnError: Boolean = false -) - -/** - * Convert AjaxOptions to JavaScript JSON object. - * @param emptyOption add an empty position as the first select option - * @return JSON object - */ -fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { - val procData = { data: dynamic -> - val processedData = this.preprocessData(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 - } - } - val language = I18n.language - return obj { - this.ajax = obj { - this.url = url - this.type = httpType.type - this.dataType = dataType.type - this.data = data - this.beforeSend = beforeSend - } - this.preprocessData = procData - this.minLength = minLength - this.cache = cache - this.clearOnEmpty = clearOnEmpty - this.clearOnError = clearOnError - this.emptyRequest = emptyRequest - this.preserveSelected = false - this.requestDelay = requestDelay - this.restoreOnError = restoreOnError - this.langCode = language - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt deleted file mode 100644 index f19081e1..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import pl.treksoft.kvision.core.Component -import pl.treksoft.kvision.core.Container -import pl.treksoft.kvision.core.StringBoolPair -import pl.treksoft.kvision.core.StringPair -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.form.FieldLabel -import pl.treksoft.kvision.form.HelpBlock -import pl.treksoft.kvision.form.StringFormControl -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.utils.SnOn - -/** - * The form field component for Select control. - * - * The select control can be populated directly from *options* parameter or manually by adding - * [SelectOption] or [SelectOptGroup] components to the container. - * - * @constructor - * @param options an optional list of options (label to value pairs) for the select control - * @param value selected value - * @param name the name attribute of the generated HTML input element - * @param multiple allows multiple value selection (multiple values are comma delimited) - * @param ajaxOptions additional options for remote (AJAX) data source - * @param label label text bound to the input element - * @param rich determines if [label] can contain HTML code - */ -@Suppress("TooManyFunctions") -open class Select( - options: List<StringPair>? = null, value: String? = null, name: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, - rich: Boolean = false -) : SimplePanel(setOf("form-group")), StringFormControl { - - /** - * A list of options (label to value pairs) for the select control. - */ - var options - get() = input.options - set(value) { - input.options = value - } - /** - * A value of the selected option. - */ - override var value - get() = input.value - set(value) { - input.value = value - } - /** - * Determines if multiple value selection is allowed. - */ - var multiple - get() = input.multiple - set(value) { - input.multiple = value - } - /** - * Additional options for remote (AJAX) data source. - */ - var ajaxOptions - get() = input.ajaxOptions - set(value) { - input.ajaxOptions = value - } - /** - * Maximal number of selected options. - */ - var maxOptions - get() = input.maxOptions - set(value) { - input.maxOptions = value - } - /** - * Determines if live search is available. - */ - var liveSearch - get() = input.liveSearch - set(value) { - input.liveSearch = value - } - /** - * The placeholder for the select control. - */ - var placeholder - get() = input.placeholder - set(value) { - input.placeholder = value - } - /** - * The style of the select control. - */ - var style - get() = input.style - set(value) { - input.style = value - } - /** - * The width of the select control. - */ - var selectWidth - get() = input.selectWidth - set(value) { - input.selectWidth = value - } - /** - * The width type of the select control. - */ - var selectWidthType - get() = input.selectWidthType - set(value) { - input.selectWidthType = value - } - /** - * Determines if an empty option is automatically generated. - */ - var emptyOption - get() = input.emptyOption - set(value) { - input.emptyOption = value - } - /** - * Determines if the select is automatically focused. - */ - var autofocus - get() = input.autofocus - set(value) { - input.autofocus = value - } - /** - * The label text bound to the select element. - */ - var label - get() = flabel.content - set(value) { - flabel.content = value - } - /** - * Determines if [label] can contain HTML code. - */ - var rich - get() = flabel.rich - set(value) { - flabel.rich = value - } - - private val idc = "kv_form_select_$counter" - final override val input: SelectInput = SelectInput( - options, value, multiple, ajaxOptions, - setOf("form-control") - ).apply { - this.id = idc - this.name = name - } - final override val flabel: FieldLabel = FieldLabel(idc, label, rich) - final override val validationInfo: HelpBlock = HelpBlock().apply { visible = false } - - init { - @Suppress("LeakingThis") - input.eventTarget = this - this.addInternal(flabel) - this.addInternal(input) - this.addInternal(validationInfo) - counter++ - } - - override fun getSnClass(): List<StringBoolPair> { - val cl = super.getSnClass().toMutableList() - if (validatorError != null) { - cl.add("has-error" to true) - } - return cl - } - - @Suppress("UNCHECKED_CAST") - override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun setEventListener(block: SnOn<Widget>.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun removeEventListeners(): Widget { - input.removeEventListeners() - return this - } - - override fun add(child: Component): SimplePanel { - input.add(child) - return this - } - - override fun addAll(children: List<Component>): SimplePanel { - input.addAll(children) - return this - } - - override fun remove(child: Component): SimplePanel { - input.remove(child) - return this - } - - override fun removeAll(): SimplePanel { - input.removeAll() - return this - } - - override fun getChildren(): List<Component> { - return input.getChildren() - } - - /** - * Opens dropdown with options. - */ - open fun showOptions() { - input.showOptions() - } - - /** - * Hides dropdown with options. - */ - open fun hideOptions() { - input.hideOptions() - } - - /** - * Toggles visibility of dropdown with options. - */ - open fun toggleOptions() { - input.toggleOptions() - } - - override fun focus() { - input.focus() - } - - override fun blur() { - input.blur() - } - - companion object { - internal var counter = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.select( - options: List<StringPair>? = null, value: String? = null, name: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, - rich: Boolean = false, init: (Select.() -> Unit)? = null - ): Select { - val select = Select(options, value, name, multiple, ajaxOptions, label, rich).apply { init?.invoke(this) } - this.add(select) - return select - } - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt deleted file mode 100644 index 6d6f0fc5..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ /dev/null @@ -1,363 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.form.select - -import com.github.snabbdom.VNode -import pl.treksoft.kvision.KVManager.KVNULL -import pl.treksoft.kvision.core.Component -import pl.treksoft.kvision.core.Container -import pl.treksoft.kvision.core.CssSize -import pl.treksoft.kvision.core.StringBoolPair -import pl.treksoft.kvision.core.StringPair -import pl.treksoft.kvision.form.FormInput -import pl.treksoft.kvision.form.InputSize -import pl.treksoft.kvision.html.ButtonStyle -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.utils.asString -import pl.treksoft.kvision.utils.obj - -/** - * Select width types. See [Bootstrap Select width](http://silviomoreto.github.io/bootstrap-select/examples/#width). - */ -enum class SelectWidthType(internal val value: String) { - AUTO("auto"), - FIT("fit") -} - -/** - * The basic component for Select control. - * - * The select control can be populated directly from *options* parameter or manually by adding - * [SelectOption] or [SelectOptGroup] components to the container. - * - * @constructor - * @param options an optional list of options (label to value pairs) for the select control - * @param value selected value - * @param multiple allows multiple value selection (multiple values are comma delimited) - * @param ajaxOptions additional options for remote (AJAX) data source - * @param classes a set of CSS class names - */ -@Suppress("TooManyFunctions") -open class SelectInput( - options: List<StringPair>? = null, value: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, - classes: Set<String> = setOf() -) : SimplePanel(classes), FormInput { - - /** - * A list of options (label to value pairs) for the select control. - */ - internal var options by refreshOnUpdate(options, { setChildrenFromOptions() }) - /** - * A value of the selected option. - */ - var value by refreshOnUpdate(value, { refreshState() }) - /** - * The name attribute of the generated HTML select element. - */ - override var name: String? by refreshOnUpdate() - /** - * Determines if multiple value selection is allowed. - */ - var multiple by refreshOnUpdate(multiple) - /** - * Additional options for remote (AJAX) data source. - */ - var ajaxOptions by refreshOnUpdate(ajaxOptions, { - if (it != null) { - liveSearch = true - } - refresh() - }) - /** - * Maximal number of selected options. - */ - var maxOptions: Int? by refreshOnUpdate() - /** - * Determines if live search is available. - */ - var liveSearch by refreshOnUpdate(false) - /** - * The placeholder for the select control. - */ - var placeholder: String? by refreshOnUpdate() - /** - * The style of the select control. - */ - var style: ButtonStyle? by refreshOnUpdate() - /** - * The width of the select control. - */ - var selectWidth: CssSize? by refreshOnUpdate() - /** - * The width type of the select control. - */ - var selectWidthType: SelectWidthType? by refreshOnUpdate() - /** - * Determines if an empty option is automatically generated. - */ - var emptyOption by refreshOnUpdate(false, { setChildrenFromOptions() }) - /** - * Determines if the field is disabled. - */ - override var disabled by refreshOnUpdate(false) - /** - * Determines if the select is automatically focused. - */ - var autofocus: Boolean? by refreshOnUpdate() - /** - * The size of the input. - */ - override var size: InputSize? by refreshOnUpdate() - - init { - setChildrenFromOptions() - this.setInternalEventListener<SelectInput> { - change = { - val v = getElementJQuery()?.`val`() - self.value = v?.let { - calculateValue(it) - } - } - } - } - - private fun calculateValue(v: Any): String? { - return if (this.multiple) { - @Suppress("UNCHECKED_CAST") - val arr = v as? Array<String> - if (arr != null && arr.isNotEmpty()) { - arr.joinToString() - } else { - null - } - } else { - val vs = v as String - if (KVNULL == vs || vs.isEmpty()) { - null - } else { - vs - } - } - } - - override fun render(): VNode { - return render("select", childrenVNodes()) - } - - override fun add(child: Component): SimplePanel { - super.add(child) - refreshSelectInput() - return this - } - - override fun addAll(children: List<Component>): SimplePanel { - super.addAll(children) - refreshSelectInput() - return this - } - - override fun remove(child: Component): SimplePanel { - super.remove(child) - refreshSelectInput() - return this - } - - override fun removeAll(): SimplePanel { - super.removeAll() - refreshSelectInput() - return this - } - - private fun setChildrenFromOptions() { - 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) - } - } - this.refreshSelectInput() - } - - /** - * Opens dropdown with options. - */ - open fun showOptions() { - getElementJQueryD()?.selectpicker("show") - } - - /** - * Hides dropdown with options. - */ - open fun hideOptions() { - getElementJQueryD()?.selectpicker("hide") - } - - /** - * Toggles visibility of dropdown with options. - */ - 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 - } - - private 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 "multiple") - } - maxOptions?.let { - sn.add("data-max-options" to "" + it) - } - if (liveSearch) { - sn.add("data-live-search" to "true") - } - placeholder?.let { - sn.add("title" to translate(it)) - } - autofocus?.let { - if (it) { - sn.add("autofocus" to "autofocus") - } - } - if (disabled) { - sn.add("disabled" to "disabled") - } - 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.asString()) - } - return sn - } - - @Suppress("UnsafeCastFromDynamic") - override fun afterInsert(node: VNode) { - ajaxOptions?.let { - getElementJQueryD()?.selectpicker("render").ajaxSelectPicker(it.toJs(emptyOption)) - } ?: 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) - } - - /** - * Makes the input element focused. - */ - open fun focus() { - getElementJQuery()?.focus() - } - - /** - * Makes the input element blur. - */ - open fun blur() { - getElementJQuery()?.blur() - } - - companion object { - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.selectInput( - options: List<StringPair>? = null, value: String? = null, - multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, - classes: Set<String> = setOf(), init: (SelectInput.() -> Unit)? = null - ): SelectInput { - val selectInput = SelectInput(options, value, multiple, ajaxOptions, classes).apply { init?.invoke(this) } - this.add(selectInput) - return selectInput - } - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOptGroup.kt deleted file mode 100644< |
