aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/form
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2017-11-03 15:16:42 +0100
committerRobert Jaros <rjaros@finn.pl>2017-11-03 15:16:42 +0100
commitb9a849b4ebf397b04b4b3c405de325b740b958aa (patch)
tree0be1b01f68064e0f62f295573acaf3579b3d7d82 /src/main/kotlin/pl/treksoft/kvision/form
parentd4d9ea0afaf76778f3bb588e501749867053ca5f (diff)
downloadkvision-b9a849b4ebf397b04b4b3c405de325b740b958aa.tar.gz
kvision-b9a849b4ebf397b04b4b3c405de325b740b958aa.tar.bz2
kvision-b9a849b4ebf397b04b4b3c405de325b740b958aa.zip
Select input components
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt203
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt60
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/SelectOption.kt75
3 files changed, 338 insertions, 0 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt
new file mode 100644
index 00000000..61c0240c
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/form/SelectInput.kt
@@ -0,0 +1,203 @@
+package pl.treksoft.kvision.form
+
+import com.github.snabbdom.VNode
+import pl.treksoft.kvision.core.CssSize
+import pl.treksoft.kvision.html.BUTTONSTYLE
+import pl.treksoft.kvision.panel.SimplePanel
+import pl.treksoft.kvision.snabbdom.StringBoolPair
+import pl.treksoft.kvision.snabbdom.StringPair
+
+private val _aKVNULL = "#kvnull"
+
+enum class SELECTWIDTHTYPE(val value: String) {
+ AUTO("auto"),
+ FIT("fit")
+}
+
+class SelectInput(options: List<StringPair>? = null, override var value: String? = null,
+ multiple: Boolean = false, classes: Set<String> = setOf()) : SimplePanel(classes), StringFormField {
+
+ private var options = options
+ set(value) {
+ field = value
+ setChildrenFromOptions()
+ }
+
+ @Suppress("LeakingThis")
+ var startValue: String? = value
+ set(value) {
+ field = value
+ this.value = value
+ refresh()
+ }
+ var name: String? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ var multiple: Boolean = multiple
+ set(value) {
+ field = value
+ refresh()
+ }
+ var maxOptions: Int? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ var liveSearch: Boolean = false
+ set(value) {
+ field = value
+ refresh()
+ }
+ var placeholder: String? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ var style: BUTTONSTYLE? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ var selectWidth: CssSize? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ var selectWidthType: SELECTWIDTHTYPE? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ var emptyOption: Boolean = false
+ set(value) {
+ field = value
+ setChildrenFromOptions()
+ }
+ override var disabled: Boolean = false
+ set(value) {
+ field = value
+ refresh()
+ }
+ var autofocus: Boolean? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+ override var size: INPUTSIZE? = null
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ init {
+ setChildrenFromOptions()
+ this.setInternalEventListener<SelectInput> {
+ change = {
+ val v = getElementJQuery()?.`val`()
+ self.value = v?.let {
+ if (self.multiple) {
+ @Suppress("UNCHECKED_CAST")
+ val arr = it as? Array<String>
+ if (arr != null && arr.isNotEmpty()) {
+ arr.joinToString()
+ } else {
+ null
+ }
+ } else {
+ val vs = it as String
+ if (_aKVNULL == vs) {
+ null
+ } else {
+ vs
+ }
+ }
+ }
+ }
+ }
+ }
+
+ override fun render(): VNode {
+ return kvh("select", childrenVNodes())
+ }
+
+ private fun setChildrenFromOptions() {
+ this.removeAll()
+ if (emptyOption) {
+ this.add(SelectOption(_aKVNULL, ""))
+ }
+ options?.let {
+ val c = it.map {
+ SelectOption(it.first, it.second)
+ }
+ this.addAll(c)
+ }
+ }
+
+ override fun getSnClass(): List<StringBoolPair> {
+ val cl = super.getSnClass().toMutableList()
+ cl.add("selectpicker" to true)
+ size?.let {
+ cl.add(it.className to true)
+ }
+ return cl
+ }
+
+ override fun getSnAttrs(): List<StringPair> {
+ val sn = super.getSnAttrs().toMutableList()
+ name?.let {
+ sn.add("name" to it)
+ }
+ if (multiple) {
+ sn.add("multiple" to "true")
+ }
+ maxOptions?.let {
+ sn.add("data-max-options" to "" + it)
+ }
+ if (liveSearch) {
+ sn.add("data-live-search" to "true")
+ }
+ placeholder?.let {
+ sn.add("title" to it)
+ }
+ autofocus?.let {
+ if (it) {
+ sn.add("autofocus" to "autofocus")
+ }
+ }
+ if (disabled) {
+ sn.add("disabled" to "true")
+ }
+ val btnStyle = style?.className ?: "btn-default"
+ when (size) {
+ INPUTSIZE.LARGE -> {
+ sn.add("data-style" to "$btnStyle btn-lg")
+ }
+ INPUTSIZE.SMALL -> {
+ sn.add("data-style" to "$btnStyle btn-sm")
+ }
+ else -> {
+ sn.add("data-style" to btnStyle)
+ }
+ }
+ selectWidthType?.let {
+ sn.add("data-width" to it.value)
+ } ?: selectWidth?.let {
+ sn.add("data-width" to it.first.toString() + it.second.unit)
+ }
+ return sn
+ }
+
+ @Suppress("UnsafeCastFromDynamic")
+ override fun afterInsert(node: VNode) {
+ getElementJQueryD()?.selectpicker("render")
+ value?.let {
+ if (multiple) {
+ getElementJQueryD()?.selectpicker("val", it.split(",").toTypedArray())
+ } else {
+ getElementJQueryD()?.selectpicker("val", it)
+ }
+ }
+ }
+}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt
new file mode 100644
index 00000000..22483777
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt
@@ -0,0 +1,60 @@
+package pl.treksoft.kvision.form
+
+import com.github.snabbdom.VNode
+import pl.treksoft.kvision.panel.SimplePanel
+import pl.treksoft.kvision.snabbdom.StringPair
+
+class SelectOptGroup(label: String, options: List<StringPair>? = null, maxOptions: Int? = null,
+ disabled: Boolean = false, classes: Set<String> = setOf()) : SimplePanel(classes) {
+
+ var label: String = label
+ set(value) {
+ field = value
+ refresh()
+ }
+ private var options = options
+ set(value) {
+ field = value
+ setChildrenFromOptions()
+ }
+ var maxOptions: Int? = maxOptions
+ set(value) {
+ field = value
+ refresh()
+ }
+ var disabled: Boolean = disabled
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ init {
+ setChildrenFromOptions()
+ }
+
+ override fun render(): VNode {
+ return kvh("optgroup", childrenVNodes())
+ }
+
+ private fun setChildrenFromOptions() {
+ this.removeAll()
+ options?.let {
+ val c = it.map {
+ SelectOption(it.first, it.second)
+ }
+ this.addAll(c)
+ }
+ }
+
+ override fun getSnAttrs(): List<StringPair> {
+ val sn = super.getSnAttrs().toMutableList()
+ sn.add("label" to label)
+ maxOptions?.let {
+ sn.add("data-max-options" to "" + it)
+ }
+ if (disabled) {
+ sn.add("disabled" to "true")
+ }
+ return sn
+ }
+} \ No newline at end of file
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/SelectOption.kt b/src/main/kotlin/pl/treksoft/kvision/form/SelectOption.kt
new file mode 100644
index 00000000..48412e17
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/form/SelectOption.kt
@@ -0,0 +1,75 @@
+package pl.treksoft.kvision.form
+
+import com.github.snabbdom.VNode
+import pl.treksoft.kvision.core.Widget
+import pl.treksoft.kvision.snabbdom.StringPair
+
+open class SelectOption(value: String? = null, label: String? = null, subtext: String? = null, icon: String? = null,
+ divider: Boolean = false, disabled: Boolean = false,
+ classes: Set<String> = setOf()) : Widget(classes) {
+
+ var value: String? = value
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ var label: String? = label
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ var subtext: String? = subtext
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ var icon: String? = icon
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ var divider: Boolean = divider
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ var disabled: Boolean = disabled
+ set(value) {
+ field = value
+ refresh()
+ }
+
+ override fun render(): VNode {
+ return kvh("option", arrayOf(label ?: value))
+ }
+
+ override fun getSnAttrs(): List<StringPair> {
+ val sn = super.getSnAttrs().toMutableList()
+ value?.let {
+ sn.add("value" to it)
+ }
+ if (!divider) {
+ subtext?.let {
+ sn.add("data-subtext" to it)
+ }
+ icon?.let {
+ if (it.startsWith("fa-")) {
+ sn.add("data-icon" to "fa $it")
+ } else {
+ sn.add("data-icon" to "glyphicon-$it")
+ }
+ }
+ if (disabled) {
+ sn.add("disabled" to "true")
+ }
+ } else {
+ sn.add("data-divider" to "true")
+ }
+ return sn
+ }
+}