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 | |
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')
7 files changed, 84 insertions, 25 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()) } diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt index 86f1abc9..66f2d422 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/FlexPanel.kt @@ -1,8 +1,10 @@ package pl.treksoft.kvision.panel import pl.treksoft.kvision.core.Component +import pl.treksoft.kvision.core.StyledComponent import pl.treksoft.kvision.core.WidgetWrapper import pl.treksoft.kvision.snabbdom.StringPair +import pl.treksoft.kvision.utils.px enum class FLEXDIR(val dir: String) { ROW("row"), @@ -46,11 +48,12 @@ enum class FLEXALIGNCONTENT(val alignContent: String) { open class FlexPanel( direction: FLEXDIR? = null, wrap: FLEXWRAP? = null, justify: FLEXJUSTIFY? = null, alignItems: FLEXALIGNITEMS? = null, alignContent: FLEXALIGNCONTENT? = null, - classes: Set<String> = setOf() + spacing: Int? = null, classes: Set<String> = setOf() ) : SimplePanel(classes) { var direction = direction set(value) { field = value + refreshSpacing() refresh() } var wrap = wrap @@ -73,16 +76,43 @@ open class FlexPanel( field = value refresh() } + var spacing = spacing + set(value) { + field = value + refreshSpacing() + refresh() + } @Suppress("LongParameterList") fun add( child: Component, order: Int? = null, grow: Int? = null, shrink: Int? = null, basis: Int? = null, alignSelf: FLEXALIGNITEMS? = null, classes: Set<String> = setOf() ): FlexPanel { - addInternal(FlexWrapper(child, order, grow, shrink, basis, alignSelf, classes)) + val wrapper = FlexWrapper(child, order, grow, shrink, basis, alignSelf, classes) + addInternal(applySpacing(wrapper)) return this } + private fun refreshSpacing() { + getChildren().filterIsInstance<StyledComponent>().map { applySpacing(it) } + } + + private fun applySpacing(wrapper: StyledComponent): StyledComponent { + wrapper.marginTop = null + wrapper.marginRight = null + wrapper.marginBottom = null + wrapper.marginLeft = null + spacing?.let { + when (direction) { + FLEXDIR.COLUMN -> wrapper.marginBottom = it.px() + FLEXDIR.ROWREV -> wrapper.marginLeft = it.px() + FLEXDIR.COLUMNREV -> wrapper.marginTop = it.px() + else -> wrapper.marginRight = it.px() + } + } + return wrapper + } + override fun add(child: Component): FlexPanel { return add(child, null) } diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/HPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/HPanel.kt index 93d60f1d..173ac826 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/HPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/HPanel.kt @@ -1,9 +1,9 @@ package pl.treksoft.kvision.panel open class HPanel( - justify: FLEXJUSTIFY? = null, alignItems: FLEXALIGNITEMS? = null, + justify: FLEXJUSTIFY? = null, alignItems: FLEXALIGNITEMS? = null, spacing: Int? = null, classes: Set<String> = setOf() ) : FlexPanel( null, - null, justify, alignItems, null, classes + null, justify, alignItems, null, spacing, classes ) diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/VPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/VPanel.kt index 883d9d75..7870b67a 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/VPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/VPanel.kt @@ -1,9 +1,9 @@ package pl.treksoft.kvision.panel open class VPanel( - justify: FLEXJUSTIFY? = null, alignItems: FLEXALIGNITEMS? = null, + justify: FLEXJUSTIFY? = null, alignItems: FLEXALIGNITEMS? = null, spacing: Int? = null, classes: Set<String> = setOf() ) : FlexPanel( FLEXDIR.COLUMN, - null, justify, alignItems, null, classes + null, justify, alignItems, null, spacing, classes ) |