diff options
author | Robert Jaros <rjaros@finn.pl> | 2017-10-19 13:51:01 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2017-10-19 13:51:01 +0200 |
commit | 6caae545b7961f9ba5f136d38730ecf026ab7fbb (patch) | |
tree | 46e987bbf1cc0c30075c32e50140f4f90834c22c /src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt | |
parent | d17f27058f41f2dddd9fd5e88149ed55f9f7bf0c (diff) | |
download | kvision-6caae545b7961f9ba5f136d38730ecf026ab7fbb.tar.gz kvision-6caae545b7961f9ba5f136d38730ecf026ab7fbb.tar.bz2 kvision-6caae545b7961f9ba5f136d38730ecf026ab7fbb.zip |
Refactoring - CheckBoxInput -> CheckInput
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt new file mode 100644 index 00000000..131793b8 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt @@ -0,0 +1,97 @@ +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 + +enum class CHECKINPUTTYPE(val type: String) { + CHECKBOX("checkbox"), + RADIO("radio") +} + +open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override var value: Boolean = false, + name: String? = null, disabled: Boolean = false, id: String? = null, + extraValue: String? = null, + classes: Set<String> = setOf()) : Widget(classes), BoolFormField { + + init { + this.id = id + } + + @Suppress("LeakingThis") + var startValue: Boolean = value + set(value) { + field = value + this.value = value + refresh() + } + var type: CHECKINPUTTYPE = type + set(value) { + field = value + refresh() + } + var name: String? = name + set(value) { + field = value + refresh() + } + override var disabled: Boolean = disabled + set(value) { + field = value + refresh() + } + var extraValue: String? = extraValue + set(value) { + field = value + refresh() + } + override var size: INPUTSIZE? = null + set(value) { + field = value + refresh() + } + + override fun render(): VNode { + return kvh("input") + } + + override fun getSnClass(): List<StringBoolPair> { + val cl = super.getSnClass().toMutableList() + size?.let { + cl.add(it.className to true) + } + return cl + } + + override fun getSnAttrs(): List<StringPair> { + val sn = super.getSnAttrs().toMutableList() + sn.add("type" to type.type) + if (startValue) { + sn.add("checked" to "true") + } + name?.let { + sn.add("name" to it) + } + if (disabled) { + sn.add("disabled" to "true") + } + extraValue?.let { + sn.add("value" to it) + } + return sn + } + + 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 + }) + } +} |