aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/form
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2017-11-04 12:47:52 +0100
committerRobert Jaros <rjaros@finn.pl>2017-11-04 12:47:52 +0100
commit1a41c5d06bb51907b752afa9c13c08dd1e82455a (patch)
tree40dd4c1f4c75ab38f9d4a9de062e221e14da239c /src/main/kotlin/pl/treksoft/kvision/form
parent9a1be3609acc08349387013430f395b9426c23a0 (diff)
downloadkvision-1a41c5d06bb51907b752afa9c13c08dd1e82455a.tar.gz
kvision-1a41c5d06bb51907b752afa9c13c08dd1e82455a.tar.bz2
kvision-1a41c5d06bb51907b752afa9c13c08dd1e82455a.zip
Refactoring events handling
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt20
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt2
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt20
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Radio.kt2
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Select.kt2
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt80
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Text.kt2
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt2
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt2
9 files changed, 114 insertions, 18 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt
index 84ad5661..8f0a09e0 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt
@@ -4,21 +4,27 @@ import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.snabbdom.StringBoolPair
import pl.treksoft.kvision.snabbdom.StringPair
-abstract class AbstractTextInput(override var value: String? = null,
+abstract class AbstractTextInput(value: String? = null,
classes: Set<String> = setOf()) : Widget(classes + "form-control"), StringFormField {
+
init {
- this.setInternalEventListener {
+ this.setInternalEventListener<AbstractTextInput> {
input = {
val v = getElementJQuery()?.`val`() as String?
if (v != null && v.isNotEmpty()) {
- value = v
+ self.value = v
} else {
- value = null
+ self.value = null
}
}
}
}
+ override var value: String? = value
+ set(value) {
+ field = value
+ refreshState()
+ }
@Suppress("LeakingThis")
var startValue: String? = value
set(value) {
@@ -96,4 +102,10 @@ abstract class AbstractTextInput(override var value: String? = null,
}
return sn
}
+
+ private fun refreshState() {
+ value?.let {
+ getElementJQuery()?.`val`(it)
+ } ?: getElementJQueryD()?.`val`(null)
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt
index f4598d4d..ecd016b2 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt
@@ -73,6 +73,8 @@ open class CheckBox(value: Boolean = false, label: String? = null,
val flabel: FieldLabel = FieldLabel(idc, label, rich)
init {
+ @Suppress("LeakingThis")
+ input.eventTarget = this
this.addInternal(input)
this.addInternal(flabel)
counter++
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt
index 36e59e87..746b6e3d 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt
@@ -10,22 +10,26 @@ enum class CHECKINPUTTYPE(val type: String) {
RADIO("radio")
}
-open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override var value: Boolean = false,
+open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, value: Boolean = false,
classes: Set<String> = setOf()) : Widget(classes), BoolFormField {
init {
- this.setInternalEventListener {
+ this.setInternalEventListener<CheckInput> {
click = {
val v = getElementJQuery()?.prop("checked") as Boolean?
- value = (v == true)
+ self.value = (v == true)
}
change = {
val v = getElementJQuery()?.prop("checked") as Boolean?
- value = (v == true)
+ self.value = (v == true)
}
}
}
-
+ override var value: Boolean = value
+ set(value) {
+ field = value
+ refreshState()
+ }
@Suppress("LeakingThis")
var startValue: Boolean = value
set(value) {
@@ -90,14 +94,14 @@ open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override v
}
override fun afterInsert(node: VNode) {
- refreshCheckedState()
+ refreshState()
}
override fun afterPostpatch(node: VNode) {
- refreshCheckedState()
+ refreshState()
}
- private fun refreshCheckedState() {
+ private fun refreshState() {
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 3ee6edff..f4ca9c41 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt
@@ -81,6 +81,8 @@ open class Radio(value: Boolean = false, extraValue: String? = null, label: Stri
val flabel: FieldLabel = FieldLabel(idc, label, rich)
init {
+ @Suppress("LeakingThis")
+ input.eventTarget = this
this.addInternal(input)
this.addInternal(flabel)
counter++
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Select.kt b/src/main/kotlin/pl/treksoft/kvision/form/Select.kt
index a8759208..e099dd43 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/Select.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/Select.kt
@@ -96,6 +96,8 @@ open class Select(options: List<StringPair>? = null, value: String? = null,
init {
this.addInternal(flabel)
+ @Suppress("LeakingThis")
+ input.eventTarget = this
this.addInternal(input)
counter++
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt
index bd67868d..68c3b57d 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt
@@ -2,10 +2,12 @@ package pl.treksoft.kvision.form
import com.github.snabbdom.VNode
import pl.treksoft.kvision.core.CssSize
+import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.html.BUTTONSTYLE
import pl.treksoft.kvision.panel.SimplePanel
import pl.treksoft.kvision.snabbdom.StringBoolPair
import pl.treksoft.kvision.snabbdom.StringPair
+import pl.treksoft.kvision.snabbdom.obj
private val _aKVNULL = "#kvnull"
@@ -14,7 +16,7 @@ enum class SELECTWIDTHTYPE(val value: String) {
FIT("fit")
}
-class SelectInput(options: List<StringPair>? = null, override var value: String? = null,
+class SelectInput(options: List<StringPair>? = null, value: String? = null,
multiple: Boolean = false, classes: Set<String> = setOf()) : SimplePanel(classes), StringFormField {
internal var options = options
@@ -24,6 +26,13 @@ class SelectInput(options: List<StringPair>? = null, override var value: String?
}
@Suppress("LeakingThis")
+ override var value: String? = value
+ set(value) {
+ field = value
+ refreshState()
+ }
+
+ @Suppress("LeakingThis")
var startValue: String? = value
set(value) {
field = value
@@ -122,17 +131,42 @@ class SelectInput(options: List<StringPair>? = null, override var value: String?
return kvh("select", childrenVNodes())
}
+ override fun add(child: Widget): SimplePanel {
+ super.add(child)
+ refreshSelectInput()
+ return this
+ }
+
+ override fun addAll(children: List<Widget>): SimplePanel {
+ super.addAll(children)
+ refreshSelectInput()
+ return this
+ }
+
+ override fun remove(child: Widget): SimplePanel {
+ super.remove(child)
+ refreshSelectInput()
+ return this
+ }
+
+ override fun removeAll(): SimplePanel {
+ super.removeAll()
+ refreshSelectInput()
+ return this
+ }
+
private fun setChildrenFromOptions() {
- this.removeAll()
+ super.removeAll()
if (emptyOption) {
- this.add(SelectOption(_aKVNULL, ""))
+ super.add(SelectOption(_aKVNULL, ""))
}
options?.let {
val c = it.map {
SelectOption(it.first, it.second)
}
- this.addAll(c)
+ super.addAll(c)
}
+ this.refreshSelectInput()
}
override fun getSnClass(): List<StringBoolPair> {
@@ -144,6 +178,11 @@ class SelectInput(options: List<StringPair>? = null, override var value: String?
return cl
}
+ fun refreshSelectInput() {
+ getElementJQueryD()?.selectpicker("refresh")
+ refreshState()
+ }
+
@Suppress("ComplexMethod")
override fun getSnAttrs(): List<StringPair> {
val sn = super.getSnAttrs().toMutableList()
@@ -193,12 +232,43 @@ class SelectInput(options: List<StringPair>? = null, override var value: String?
@Suppress("UnsafeCastFromDynamic")
override fun afterInsert(node: VNode) {
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)
}
+
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Text.kt b/src/main/kotlin/pl/treksoft/kvision/form/Text.kt
index 01816195..211cb1f6 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/Text.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/Text.kt
@@ -18,6 +18,8 @@ open class Text(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, value: String? = null,
final override val input: TextInput = TextInput(type, value).apply { id = idc }
init {
+ @Suppress("LeakingThis")
+ input.eventTarget = this
this.addInternal(input)
}
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt b/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt
index 9b65a306..28bbdb48 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt
@@ -22,6 +22,8 @@ open class TextArea(cols: Int? = null, rows: Int? = null, value: String? = null,
final override val input: TextAreaInput = TextAreaInput(cols, rows, value).apply { id = idc }
init {
+ @Suppress("LeakingThis")
+ input.eventTarget = this
this.addInternal(input)
}
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt
index 5b8dd5b5..9f27bd16 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt
@@ -23,7 +23,7 @@ class TextAreaInput(cols: Int? = null, rows: Int? = null, value: String? = null,
}
override fun render(): VNode {
- return value?.let {
+ return startValue?.let {
kvh("textarea", arrayOf(it))
} ?: kvh("textarea")
}