aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/form
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2017-10-28 23:45:26 +0200
committerRobert Jaros <rjaros@finn.pl>2017-10-28 23:45:26 +0200
commit06f297d68887c7934e66d2c757abc8bf619df66a (patch)
treea828eec09f0bdc99b0f3fd45972b8cead37fbdec /src/main/kotlin/pl/treksoft/kvision/form
parent6b13b8909a302b0f0f2155b81b83cd5ab4d7a046 (diff)
downloadkvision-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')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt4
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt28
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt4
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt32
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Radio.kt4
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