diff options
| author | Robert Jaros <rjaros@finn.pl> | 2018-01-24 12:38:06 +0100 |
|---|---|---|
| committer | Robert Jaros <rjaros@finn.pl> | 2018-01-24 12:38:06 +0100 |
| commit | 981973fc4c835ea4637147e6414c7c53222094c5 (patch) | |
| tree | 792c6308636eccb6e0101fcd14e8eebd06466a13 /src/main/kotlin/pl/treksoft/kvision/form | |
| parent | 138f39076a45f0851b06777b8fec45d47863e14e (diff) | |
| download | kvision-981973fc4c835ea4637147e6414c7c53222094c5.tar.gz kvision-981973fc4c835ea4637147e6414c7c53222094c5.tar.bz2 kvision-981973fc4c835ea4637147e6414c7c53222094c5.zip | |
New options for SelectInput, Forms and FlexPanels
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
4 files changed, 48 insertions, 19 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index f533473a..ac3dfab7 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -1,6 +1,7 @@ package pl.treksoft.kvision.form import kotlin.js.Date +import kotlin.js.Json data class FieldParams<in F : FormControl>( val required: Boolean = false, @@ -58,11 +59,19 @@ open class Form<K>(private val panel: FormPanel<K>? = null, private val modelFac } } + open fun clearData() { + fields.forEach { it.value.setValue(null) } + } + open fun getData(): K { val map = fields.entries.associateBy({ it.key }, { it.value.getValue() }) return modelFactory(map.withDefault { null }) } + open fun getDataJson(): Json { + return fields.entries.associateBy({ it.key }, { it.value.getValue() }).asJson() + } + open fun validate(): Boolean { val fieldWithError = fieldsParams.mapNotNull { entry -> fields[entry.key]?.let { control -> @@ -98,3 +107,8 @@ fun Map<String, Any?>.string(key: String): String? = this[key] as? String fun Map<String, Any?>.number(key: String): Number? = this[key] as? Number fun Map<String, Any?>.bool(key: String): Boolean? = this[key] as? Boolean fun Map<String, Any?>.date(key: String): Date? = this[key] as? Date + +fun Map<String, Any?>.asJson(): Json { + val array = this.entries.map { it.component1() to it.component2() }.toTypedArray() + return kotlin.js.json(*array) +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index 31ab3ced..6c98244d 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -7,6 +7,7 @@ import pl.treksoft.kvision.html.TAG import pl.treksoft.kvision.html.Tag import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.snabbdom.StringBoolPair +import kotlin.js.Json enum class FORMTYPE(val formType: String) { INLINE("form-inline"), @@ -108,10 +109,18 @@ open class FormPanel<K>( form.setData(data) } + open fun clearData() { + form.clearData() + } + open fun getData(): K { return form.getData() } + open fun getDataJson(): Json { + return form.getDataJson() + } + open fun validate(): Boolean { return form.validate() } 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 0cff73db..9ba13f84 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,6 @@ package pl.treksoft.kvision.form.select +import pl.treksoft.jquery.JQueryXHR import pl.treksoft.kvision.core.KVManager.AJAX_REQUEST_DELAY import pl.treksoft.kvision.core.KVManager.KVNULL import pl.treksoft.kvision.snabbdom.obj @@ -18,11 +19,11 @@ enum class DataType(val type: String) { } data class AjaxOptions( - val url: String, val processData: (dynamic) -> dynamic, + val url: String, val processData: (dynamic) -> dynamic, val beforeSend: ((JQueryXHR) -> dynamic)? = null, 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 emptyRequest: Boolean = false, val requestDelay: Int = AJAX_REQUEST_DELAY, val restoreOnError: Boolean = false ) @@ -47,6 +48,7 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { this.type = httpType.type this.dataType = dataType.type this.data = processParams + this.beforeSend = beforeSend } this.preprocessData = procData this.minLength = minLength @@ -54,7 +56,7 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { this.clearOnEmpty = clearOnEmpty this.clearOnError = clearOnError this.emptyRequest = emptyRequest - this.preserveSelected = preserveSelected + this.preserveSelected = false this.requestDelay = requestDelay this.restoreOnError = restoreOnError } 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 9e35fb6b..80654f93 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -114,27 +114,31 @@ open class 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 (KVNULL == vs) { - null - } else { - vs - } - } + 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.length == 0) { + null + } else { + vs + } + } + } + override fun render(): VNode { return kvh("select", childrenVNodes()) } |
