diff options
author | Robert Jaros <rjaros@finn.pl> | 2017-10-28 23:45:26 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2017-10-28 23:45:26 +0200 |
commit | 06f297d68887c7934e66d2c757abc8bf619df66a (patch) | |
tree | a828eec09f0bdc99b0f3fd45972b8cead37fbdec /src/main/kotlin/pl/treksoft/kvision/form | |
parent | 6b13b8909a302b0f0f2155b81b83cd5ab4d7a046 (diff) | |
download | kvision-06f297d68887c7934e66d2c757abc8bf619df66a.tar.gz kvision-06f297d68887c7934e66d2c757abc8bf619df66a.tar.bz2 kvision-06f297d68887c7934e66d2c757abc8bf619df66a.zip |
Databinding components
Event handlers refactoring
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
5 files changed, 41 insertions, 31 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt b/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt index 98619dbc..dfa4233b 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt @@ -1,11 +1,11 @@ package pl.treksoft.kvision.form -import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.snabbdom.SnOn abstract class AbstractText(label: String? = null, rich: Boolean = false) : - Container(setOf("form-group")), StringFormField { + SimplePanel(setOf("form-group")), StringFormField { override var value get() = input.value diff --git a/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt index 017e7fef..82846012 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt @@ -1,16 +1,25 @@ package pl.treksoft.kvision.form -import com.github.snabbdom.VNode import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.snabbdom.StringBoolPair import pl.treksoft.kvision.snabbdom.StringPair abstract class AbstractTextInput(placeholder: String? = null, - override var value: String? = null, name: String? = null, maxlength: Int? = null, - disabled: Boolean = false, id: String? = null, - classes: Set<String> = setOf()) : Widget(classes + "form-control"), StringFormField { + override var value: String? = null, name: String? = null, maxlength: Int? = null, + disabled: Boolean = false, id: String? = null, + classes: Set<String> = setOf()) : Widget(classes + "form-control"), StringFormField { init { this.id = id + this.setInternalEventListener { + input = { + val v = getElementJQuery()?.`val`() as String? + if (v != null && v.isNotEmpty()) { + value = v + } else { + value = null + } + } + } } @Suppress("LeakingThis") @@ -90,15 +99,4 @@ abstract class AbstractTextInput(placeholder: String? = null, } return sn } - - override fun afterInsert(node: VNode) { - this.getElementJQuery()?.on("input", { _, _ -> - val v = getElementJQuery()?.`val`() as String? - if (v != null && v.isNotEmpty()) { - value = v - } else { - value = null - } - }) - } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt index 325c1982..65941495 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt @@ -1,6 +1,6 @@ package pl.treksoft.kvision.form -import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.snabbdom.SnOn import pl.treksoft.kvision.snabbdom.StringBoolPair @@ -16,7 +16,7 @@ enum class CHECKBOXSTYLE(val className: String) { open class CheckBox(value: Boolean = false, name: String? = null, style: CHECKBOXSTYLE? = null, circled: Boolean = false, inline: Boolean = false, disabled: Boolean = false, - label: String? = null, rich: Boolean = false) : Container(setOf("checkbox")), BoolFormField { + label: String? = null, rich: Boolean = false) : SimplePanel(setOf("checkbox")), BoolFormField { override var value get() = input.value diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt index 131793b8..07008ddd 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt @@ -17,6 +17,16 @@ open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override v init { this.id = id + this.setInternalEventListener { + click = { + val v = getElementJQuery()?.prop("checked") as Boolean? + value = (v == true) + } + change = { + val v = getElementJQuery()?.prop("checked") as Boolean? + value = (v == true) + } + } } @Suppress("LeakingThis") @@ -83,15 +93,17 @@ open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override v } override fun afterInsert(node: VNode) { - this.getElementJQuery()?.on("change", { _, _ -> - val v = getElementJQuery()?.prop("checked") as Boolean? - value = (v == true) - true - }) - this.getElementJQuery()?.on("click", { _, _ -> - val v = getElementJQuery()?.prop("checked") as Boolean? - value = (v == true) - true - }) + refreshCheckedState() + } + + override fun afterPostpatch(node: VNode) { + refreshCheckedState() + } + + private fun refreshCheckedState() { + val v = getElementJQuery()?.prop("checked") as Boolean? + if (this.value != v) { + getElementJQuery()?.prop("checked", this.value) + } } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt index eebbe65a..66c2d75c 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt @@ -1,6 +1,6 @@ package pl.treksoft.kvision.form -import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.snabbdom.SnOn import pl.treksoft.kvision.snabbdom.StringBoolPair @@ -16,7 +16,7 @@ enum class RADIOSTYLE(val className: String) { open class Radio(value: Boolean = false, extraValue: String? = null, name: String? = null, style: RADIOSTYLE? = null, squared: Boolean = false, inline: Boolean = false, disabled: Boolean = false, - label: String? = null, rich: Boolean = false) : Container(), BoolFormField { + label: String? = null, rich: Boolean = false) : SimplePanel(), BoolFormField { override var value get() = input.value |