aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/form
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2017-10-19 15:22:42 +0200
committerRobert Jaros <rjaros@finn.pl>2017-10-19 15:22:42 +0200
commitd591af011d040d8ce2684f044b594d28ab358ef9 (patch)
tree89ec92f3d72d535dc6507b68b70ae326797b28eb /src/main/kotlin/pl/treksoft/kvision/form
parent6caae545b7961f9ba5f136d38730ecf026ab7fbb (diff)
downloadkvision-d591af011d040d8ce2684f044b594d28ab358ef9.tar.gz
kvision-d591af011d040d8ce2684f044b594d28ab358ef9.tar.bz2
kvision-d591af011d040d8ce2684f044b594d28ab358ef9.zip
Form controls: TextArea
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt84
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt105
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt5
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Radio.kt5
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Text.kt61
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt28
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt47
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/TextInput.kt95
8 files changed, 282 insertions, 148 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt b/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt
new file mode 100644
index 00000000..98619dbc
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt
@@ -0,0 +1,84 @@
+package pl.treksoft.kvision.form
+
+import pl.treksoft.kvision.core.Container
+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 {
+
+ override var value
+ get() = input.value
+ set(value) {
+ input.value = value
+ }
+ var startValue
+ get() = input.startValue
+ set(value) {
+ input.startValue = value
+ }
+ var placeholder
+ get() = input.placeholder
+ set(value) {
+ input.placeholder = value
+ }
+ var name
+ get() = input.name
+ set(value) {
+ input.name = value
+ }
+ var maxlength
+ get() = input.maxlength
+ set(value) {
+ input.maxlength = value
+ }
+ override var disabled
+ get() = input.disabled
+ set(value) {
+ input.disabled = value
+ }
+ var label
+ get() = flabel.text
+ set(value) {
+ flabel.text = value
+ }
+ var rich
+ get() = flabel.rich
+ set(value) {
+ flabel.rich = value
+ }
+ override var size
+ get() = input.size
+ set(value) {
+ input.size = value
+ }
+
+ protected val idc = "kv_form_text_" + counter
+ internal abstract val input: AbstractTextInput
+ internal val flabel: FieldLabel = FieldLabel(idc, label, rich)
+
+ init {
+ this.addInternal(flabel)
+ counter++
+ }
+
+ companion object {
+ var counter = 0
+ }
+
+ @Suppress("UNCHECKED_CAST")
+ override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget {
+ input.setEventListener(block)
+ return this
+ }
+
+ override fun setEventListener(block: SnOn<Widget>.() -> Unit): Widget {
+ input.setEventListener(block)
+ return this
+ }
+
+ override fun removeEventListeners(): Widget {
+ input.removeEventListeners()
+ return this
+ }
+}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt
new file mode 100644
index 00000000..4423d2d4
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt
@@ -0,0 +1,105 @@
+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 {
+ init {
+ this.id = id
+ }
+
+ @Suppress("LeakingThis")
+ var startValue: String? = value
+ set(value) {
+ field = value
+ this.value = value
+ refresh()
+ }
+ var placeholder: String? = placeholder
+ set(value) {
+ field = value
+ refresh()
+ }
+ var name: String? = name
+ set(value) {
+ field = value
+ refresh()
+ }
+ var maxlength: Int? = maxlength
+ set(value) {
+ field = value
+ refresh()
+ }
+ override var disabled: Boolean = disabled
+ set(value) {
+ field = value
+ refresh()
+ }
+ var autofocus: Boolean? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ var readonly: Boolean? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ override var size: INPUTSIZE? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ override fun getSnClass(): List<StringBoolPair> {
+ val cl = super.getSnClass().toMutableList()
+ size?.let {
+ cl.add(it.className to true)
+ }
+ return cl
+ }
+
+ @Suppress("ComplexMethod")
+ override fun getSnAttrs(): List<StringPair> {
+ val sn = super.getSnAttrs().toMutableList()
+ placeholder?.let {
+ sn.add("placeholder" to it)
+ }
+ name?.let {
+ sn.add("name" to it)
+ }
+ autofocus?.let {
+ if (it) {
+ sn.add("autofocus" to "autofocus")
+ }
+ }
+ maxlength?.let {
+ sn.add("maxlength" to ("" + it))
+ }
+ readonly?.let {
+ if (it) {
+ sn.add("readonly" to "readonly")
+ }
+ }
+ if (disabled) {
+ sn.add("disabled" to "true")
+ }
+ 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 87963922..325c1982 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt
@@ -94,6 +94,11 @@ open class CheckBox(value: Boolean = false, name: String? = null, style: CHECKBO
return this
}
+ override fun removeEventListeners(): Widget {
+ input.removeEventListeners()
+ return this
+ }
+
override fun getSnClass(): List<StringBoolPair> {
val cl = super.getSnClass().toMutableList()
style?.let {
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt
index f8db5162..eebbe65a 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt
@@ -99,6 +99,11 @@ open class Radio(value: Boolean = false, extraValue: String? = null, name: Strin
return this
}
+ override fun removeEventListeners(): Widget {
+ input.removeEventListeners()
+ return this
+ }
+
override fun getSnClass(): List<StringBoolPair> {
val cl = super.getSnClass().toMutableList()
if (!squared) {
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Text.kt b/src/main/kotlin/pl/treksoft/kvision/form/Text.kt
index 0980099f..ce23974d 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/Text.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/Text.kt
@@ -1,75 +1,24 @@
package pl.treksoft.kvision.form
-import pl.treksoft.kvision.core.Container
-
open class Text(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, placeholder: String? = null, value: String? = null,
name: String? = null, maxlength: Int? = null, label: String? = null, rich: Boolean = false,
- disabled: Boolean = false) : Container(setOf("form-group")), StringFormField {
+ disabled: Boolean = false) : AbstractText(label, rich) {
- override var value
- get() = input.value
- set(value) {
- input.value = value
- }
- var startValue
- get() = input.startValue
- set(value) {
- input.startValue = value
- }
var type
get() = input.type
set(value) {
input.type = value
}
- var placeholder
- get() = input.placeholder
- set(value) {
- input.placeholder = value
- }
- var name
- get() = input.name
- set(value) {
- input.name = value
- }
- var maxlength
- get() = input.maxlength
- set(value) {
- input.maxlength = value
- }
- override var disabled
- get() = input.disabled
- set(value) {
- input.disabled = value
- }
- var label
- get() = flabel.text
- set(value) {
- flabel.text = value
- }
- var rich
- get() = flabel.rich
+ var autocomplete
+ get() = input.autocomplete
set(value) {
- flabel.rich = value
- }
- override var size
- get() = input.size
- set(value) {
- input.size = value
+ input.autocomplete = value
}
- private val idc = "kv_form_text_" + counter
- internal val input: TextInput = TextInput(type, placeholder, value, name, maxlength, disabled, idc)
- internal val flabel: FieldLabel = FieldLabel(idc, label, rich)
+ override final val input: TextInput = TextInput(type, placeholder, value, name, maxlength, disabled, idc)
init {
- this.addInternal(flabel)
this.addInternal(input)
- counter++
}
-
- companion object {
- var counter = 0
- }
-
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt b/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt
new file mode 100644
index 00000000..2efa99c9
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt
@@ -0,0 +1,28 @@
+package pl.treksoft.kvision.form
+
+open class TextArea(cols: Int? = null, rows: Int? = null, placeholder: String? = null, value: String? = null,
+ name: String? = null, maxlength: Int? = null, label: String? = null, rich: Boolean = false,
+ disabled: Boolean = false) : AbstractText(label, rich) {
+
+ var cols
+ get() = input.cols
+ set(value) {
+ input.cols = value
+ }
+ var rows
+ get() = input.rows
+ set(value) {
+ input.rows = value
+ }
+ var wrapHard
+ get() = input.wrapHard
+ set(value) {
+ input.wrapHard = value
+ }
+
+ override final val input: TextAreaInput = TextAreaInput(cols, rows, placeholder, value, name, maxlength, disabled, idc)
+
+ init {
+ 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
new file mode 100644
index 00000000..35961285
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt
@@ -0,0 +1,47 @@
+package pl.treksoft.kvision.form
+
+import com.github.snabbdom.VNode
+import pl.treksoft.kvision.snabbdom.StringPair
+
+class TextAreaInput(cols: Int? = null, rows: Int? = null, placeholder: String? = null,
+ value: String? = null, name: String? = null, maxlength: Int? = null,
+ disabled: Boolean = false, id: String? = null, classes: Set<String> = setOf()) :
+ AbstractTextInput(placeholder, value, name, maxlength, disabled, id, classes) {
+
+ var cols: Int? = cols
+ set(value) {
+ field = value
+ refresh()
+ }
+ var rows: Int? = rows
+ set(value) {
+ field = value
+ refresh()
+ }
+ var wrapHard: Boolean = false
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ override fun render(): VNode {
+ return value?.let {
+ kvh("textarea", arrayOf(it))
+ } ?: kvh("textarea")
+ }
+
+ @Suppress("ComplexMethod")
+ override fun getSnAttrs(): List<StringPair> {
+ val sn = super.getSnAttrs().toMutableList()
+ cols?.let {
+ sn.add("cols" to ("" + it))
+ }
+ rows?.let {
+ sn.add("rows" to ("" + it))
+ }
+ if (wrapHard) {
+ sn.add("wrap" to "hard")
+ }
+ return sn
+ }
+}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/TextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/TextInput.kt
index 08a39e87..6cf0f239 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/TextInput.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/TextInput.kt
@@ -1,8 +1,6 @@
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 TEXTINPUTTYPE(val type: String) {
@@ -11,92 +9,32 @@ enum class TEXTINPUTTYPE(val type: String) {
}
class TextInput(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, 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 {
- init {
- this.id = id
- }
+ value: String? = null, name: String? = null, maxlength: Int? = null,
+ disabled: Boolean = false, id: String? = null, classes: Set<String> = setOf()) :
+ AbstractTextInput(placeholder, value, name, maxlength, disabled, id, classes) {
- @Suppress("LeakingThis")
- var startValue: String? = value
- set(value) {
- field = value
- this.value = value
- refresh()
- }
var type: TEXTINPUTTYPE = type
set(value) {
field = value
refresh()
}
- var placeholder: String? = placeholder
- set(value) {
- field = value
- refresh()
- }
- var name: String? = name
- set(value) {
- field = value
- refresh()
- }
- var maxlength: Int? = maxlength
- set(value) {
- field = value
- refresh()
- }
- override var disabled: Boolean = disabled
- set(value) {
- field = value
- refresh()
- }
-
var autocomplete: Boolean? = null
set(value) {
field = value
refresh()
}
- var autofocus: Boolean? = null
- set(value) {
- field = value
- refresh()
- }
- var readonly: Boolean? = null
- 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
- }
-
@Suppress("ComplexMethod")
override fun getSnAttrs(): List<StringPair> {
val sn = super.getSnAttrs().toMutableList()
sn.add("type" to type.type)
- placeholder?.let {
- sn.add("placeholder" to it)
- }
startValue?.let {
sn.add("value" to it)
}
- name?.let {
- sn.add("name" to it)
- }
autocomplete?.let {
if (it) {
sn.add("autocomplete" to "on")
@@ -104,33 +42,6 @@ class TextInput(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, placeholder: String? =
sn.add("autocomplete" to "off")
}
}
- autofocus?.let {
- if (it) {
- sn.add("autofocus" to "autofocus")
- }
- }
- maxlength?.let {
- sn.add("maxlength" to ("" + it))
- }
- readonly?.let {
- if (it) {
- sn.add("readonly" to "readonly")
- }
- }
- if (disabled) {
- sn.add("disabled" to "true")
- }
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
- }
- })
- }
}