From 25ab470ea458b2652ff77e2a66a856c63553c486 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 9 Nov 2017 12:21:43 +0100 Subject: Refactoring packages --- src/main/kotlin/pl/treksoft/kvision/Showcase.kt | 12 +- .../pl/treksoft/kvision/form/AbstractText.kt | 84 ------------- .../pl/treksoft/kvision/form/AbstractTextInput.kt | 111 ----------------- .../kotlin/pl/treksoft/kvision/form/CheckBox.kt | 116 ------------------ .../kotlin/pl/treksoft/kvision/form/CheckInput.kt | 110 ----------------- .../kotlin/pl/treksoft/kvision/form/Password.kt | 4 - src/main/kotlin/pl/treksoft/kvision/form/Radio.kt | 132 -------------------- src/main/kotlin/pl/treksoft/kvision/form/Text.kt | 25 ---- .../kotlin/pl/treksoft/kvision/form/TextArea.kt | 29 ----- .../pl/treksoft/kvision/form/TextAreaInput.kt | 44 ------- .../kotlin/pl/treksoft/kvision/form/TextInput.kt | 44 ------- .../pl/treksoft/kvision/form/check/CheckBox.kt | 118 ++++++++++++++++++ .../pl/treksoft/kvision/form/check/CheckInput.kt | 112 +++++++++++++++++ .../kotlin/pl/treksoft/kvision/form/check/Radio.kt | 134 +++++++++++++++++++++ .../pl/treksoft/kvision/form/text/AbstractText.kt | 86 +++++++++++++ .../kvision/form/text/AbstractTextInput.kt | 113 +++++++++++++++++ .../pl/treksoft/kvision/form/text/Password.kt | 4 + .../kotlin/pl/treksoft/kvision/form/text/Text.kt | 25 ++++ .../pl/treksoft/kvision/form/text/TextArea.kt | 29 +++++ .../pl/treksoft/kvision/form/text/TextAreaInput.kt | 44 +++++++ .../pl/treksoft/kvision/form/text/TextInput.kt | 44 +++++++ 21 files changed, 715 insertions(+), 705 deletions(-) delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/Password.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/Radio.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/Text.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/form/TextInput.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/AbstractText.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/AbstractTextInput.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/TextArea.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/TextAreaInput.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/text/TextInput.kt (limited to 'src/main') diff --git a/src/main/kotlin/pl/treksoft/kvision/Showcase.kt b/src/main/kotlin/pl/treksoft/kvision/Showcase.kt index a3fec251..b6444720 100644 --- a/src/main/kotlin/pl/treksoft/kvision/Showcase.kt +++ b/src/main/kotlin/pl/treksoft/kvision/Showcase.kt @@ -7,13 +7,13 @@ import pl.treksoft.kvision.data.DataComponent import pl.treksoft.kvision.data.DataContainer import pl.treksoft.kvision.dropdown.DD.* import pl.treksoft.kvision.dropdown.DropDown -import pl.treksoft.kvision.form.CheckBox +import pl.treksoft.kvision.form.check.CheckBox import pl.treksoft.kvision.form.INPUTSIZE -import pl.treksoft.kvision.form.TEXTINPUTTYPE -import pl.treksoft.kvision.form.Text -import pl.treksoft.kvision.form.TextArea -import pl.treksoft.kvision.form.TextAreaInput -import pl.treksoft.kvision.form.TextInput +import pl.treksoft.kvision.form.text.TEXTINPUTTYPE +import pl.treksoft.kvision.form.text.Text +import pl.treksoft.kvision.form.text.TextArea +import pl.treksoft.kvision.form.text.TextAreaInput +import pl.treksoft.kvision.form.text.TextInput import pl.treksoft.kvision.form.select.AjaxOptions import pl.treksoft.kvision.form.select.SELECTWIDTHTYPE import pl.treksoft.kvision.form.select.Select diff --git a/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt b/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt deleted file mode 100644 index dfa4233b..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/AbstractText.kt +++ /dev/null @@ -1,84 +0,0 @@ -package pl.treksoft.kvision.form - -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) : - SimplePanel(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 setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun setEventListener(block: SnOn.() -> 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 deleted file mode 100644 index 8f0a09e0..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/AbstractTextInput.kt +++ /dev/null @@ -1,111 +0,0 @@ -package pl.treksoft.kvision.form - -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.snabbdom.StringBoolPair -import pl.treksoft.kvision.snabbdom.StringPair - -abstract class AbstractTextInput(value: String? = null, - classes: Set = setOf()) : Widget(classes + "form-control"), StringFormField { - - init { - this.setInternalEventListener { - input = { - val v = getElementJQuery()?.`val`() as String? - if (v != null && v.isNotEmpty()) { - self.value = v - } else { - self.value = null - } - } - } - } - - override var value: String? = value - set(value) { - field = value - refreshState() - } - @Suppress("LeakingThis") - var startValue: String? = value - set(value) { - field = value - this.value = value - refresh() - } - var placeholder: String? = null - set(value) { - field = value - refresh() - } - var name: String? = null - set(value) { - field = value - refresh() - } - var maxlength: Int? = null - set(value) { - field = value - refresh() - } - override var disabled: Boolean = false - 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 { - val cl = super.getSnClass().toMutableList() - size?.let { - cl.add(it.className to true) - } - return cl - } - - override fun getSnAttrs(): List { - 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 - } - - 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 deleted file mode 100644 index ecd016b2..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt +++ /dev/null @@ -1,116 +0,0 @@ -package pl.treksoft.kvision.form - -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.snabbdom.SnOn -import pl.treksoft.kvision.snabbdom.StringBoolPair - -enum class CHECKBOXSTYLE(val className: String) { - DEFAULT("checkbox-default"), - PRIMARY("checkbox-primary"), - SUCCESS("checkbox-success"), - INFO("checkbox-info"), - WARNING("checkbox-warning"), - DANGER("checkbox-danger"), -} - -open class CheckBox(value: Boolean = false, label: String? = null, - rich: Boolean = false) : SimplePanel(setOf("checkbox")), BoolFormField { - - override var value - get() = input.value - set(value) { - input.value = value - } - var startValue - get() = input.startValue - set(value) { - input.startValue = value - } - var name - get() = input.name - set(value) { - input.name = 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 - } - var style: CHECKBOXSTYLE? = null - set(value) { - field = value - refresh() - } - var circled: Boolean = false - set(value) { - field = value - refresh() - } - var inline: Boolean = false - set(value) { - field = value - refresh() - } - override var size - get() = input.size - set(value) { - input.size = value - } - - private val idc = "kv_form_checkbox_" + counter - val input: CheckInput = CheckInput(CHECKINPUTTYPE.CHECKBOX, value, setOf("styled")).apply { id = idc } - val flabel: FieldLabel = FieldLabel(idc, label, rich) - - init { - @Suppress("LeakingThis") - input.eventTarget = this - this.addInternal(input) - this.addInternal(flabel) - counter++ - } - - companion object { - var counter = 0 - } - - @Suppress("UNCHECKED_CAST") - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun removeEventListeners(): Widget { - input.removeEventListeners() - return this - } - - override fun getSnClass(): List { - val cl = super.getSnClass().toMutableList() - style?.let { - cl.add(it.className to true) - } - if (circled) { - cl.add("checkbox-circle" to true) - } - if (inline) { - cl.add("checkbox-inline" to true) - } - return cl - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt deleted file mode 100644 index 746b6e3d..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt +++ /dev/null @@ -1,110 +0,0 @@ -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, value: Boolean = false, - classes: Set = setOf()) : Widget(classes), BoolFormField { - - init { - this.setInternalEventListener { - click = { - val v = getElementJQuery()?.prop("checked") as Boolean? - self.value = (v == true) - } - change = { - val v = getElementJQuery()?.prop("checked") as Boolean? - self.value = (v == true) - } - } - } - override var value: Boolean = value - set(value) { - field = value - refreshState() - } - @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? = null - set(value) { - field = value - refresh() - } - override var disabled: Boolean = false - set(value) { - field = value - refresh() - } - var extraValue: String? = 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 { - val cl = super.getSnClass().toMutableList() - size?.let { - cl.add(it.className to true) - } - return cl - } - - override fun getSnAttrs(): List { - 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) { - refreshState() - } - - override fun afterPostpatch(node: VNode) { - refreshState() - } - - 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/Password.kt b/src/main/kotlin/pl/treksoft/kvision/form/Password.kt deleted file mode 100644 index 9b505e8b..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/Password.kt +++ /dev/null @@ -1,4 +0,0 @@ -package pl.treksoft.kvision.form - -open class Password(value: String? = null, label: String? = null, rich: Boolean = false) : Text(TEXTINPUTTYPE.PASSWORD, - value, label, rich) diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt deleted file mode 100644 index f4ca9c41..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt +++ /dev/null @@ -1,132 +0,0 @@ -package pl.treksoft.kvision.form - -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.snabbdom.SnOn -import pl.treksoft.kvision.snabbdom.StringBoolPair - -enum class RADIOSTYLE(val className: String) { - DEFAULT("radio-default"), - PRIMARY("radio-primary"), - SUCCESS("radio-success"), - INFO("radio-info"), - WARNING("radio-warning"), - DANGER("radio-danger"), -} - -open class Radio(value: Boolean = false, extraValue: String? = null, label: String? = null, - rich: Boolean = false) : SimplePanel(), BoolFormField { - - override var value - get() = input.value - set(value) { - input.value = value - } - var startValue - get() = input.startValue - set(value) { - input.startValue = value - } - var extraValue - get() = input.extraValue - set(value) { - input.extraValue = value - } - var name - get() = input.name - set(value) { - input.name = 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 - } - var style: RADIOSTYLE? = null - set(value) { - field = value - refresh() - } - var squared: Boolean = false - set(value) { - field = value - refresh() - } - var inline: Boolean = false - set(value) { - field = value - refresh() - } - override var size - get() = input.size - set(value) { - input.size = value - } - - private val idc = "kv_form_radio_" + counter - val input: CheckInput = CheckInput(CHECKINPUTTYPE.RADIO, value).apply { - this.id = idc - this.extraValue = extraValue - } - val flabel: FieldLabel = FieldLabel(idc, label, rich) - - init { - @Suppress("LeakingThis") - input.eventTarget = this - this.addInternal(input) - this.addInternal(flabel) - counter++ - } - - companion object { - var counter = 0 - } - - @Suppress("UNCHECKED_CAST") - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun setEventListener(block: SnOn.() -> Unit): Widget { - input.setEventListener(block) - return this - } - - override fun removeEventListeners(): Widget { - input.removeEventListeners() - return this - } - - override fun getSnClass(): List { - val cl = super.getSnClass().toMutableList() - if (!squared) { - cl.add("radio" to true) - style?.let { - cl.add(it.className to true) - } - if (inline) { - cl.add("radio-inline" to true) - } - } else { - cl.add("checkbox" to true) - style?.let { - cl.add(it.className.replace("radio", "checkbox") to true) - } - if (inline) { - cl.add("checkbox-inline" to true) - } - } - return cl - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Text.kt b/src/main/kotlin/pl/treksoft/kvision/form/Text.kt deleted file mode 100644 index 211cb1f6..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/Text.kt +++ /dev/null @@ -1,25 +0,0 @@ -package pl.treksoft.kvision.form - - -open class Text(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, value: String? = null, - label: String? = null, rich: Boolean = false) : AbstractText(label, rich) { - - var type - get() = input.type - set(value) { - input.type = value - } - var autocomplete - get() = input.autocomplete - set(value) { - input.autocomplete = value - } - - 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 deleted file mode 100644 index 28bbdb48..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/TextArea.kt +++ /dev/null @@ -1,29 +0,0 @@ -package pl.treksoft.kvision.form - -open class TextArea(cols: Int? = null, rows: Int? = null, value: String? = null, - label: String? = null, rich: 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 - } - - 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 deleted file mode 100644 index c30d760e..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/TextAreaInput.kt +++ /dev/null @@ -1,44 +0,0 @@ -package pl.treksoft.kvision.form - -import com.github.snabbdom.VNode -import pl.treksoft.kvision.snabbdom.StringPair - -open class TextAreaInput(cols: Int? = null, rows: Int? = null, value: String? = null, classes: Set = setOf()) : - AbstractTextInput(value, 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 startValue?.let { - kvh("textarea", arrayOf(it)) - } ?: kvh("textarea") - } - - override fun getSnAttrs(): List { - 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 deleted file mode 100644 index c900fcff..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/form/TextInput.kt +++ /dev/null @@ -1,44 +0,0 @@ -package pl.treksoft.kvision.form - -import com.github.snabbdom.VNode -import pl.treksoft.kvision.snabbdom.StringPair - -enum class TEXTINPUTTYPE(val type: String) { - TEXT("text"), - PASSWORD("password") -} - -open class TextInput(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, value: String? = null, classes: Set = setOf()) : - AbstractTextInput(value, classes) { - - var type: TEXTINPUTTYPE = type - set(value) { - field = value - refresh() - } - var autocomplete: Boolean? = null - set(value) { - field = value - refresh() - } - - override fun render(): VNode { - return kvh("input") - } - - override fun getSnAttrs(): List { - val sn = super.getSnAttrs().toMutableList() - sn.add("type" to type.type) - startValue?.let { - sn.add("value" to it) - } - autocomplete?.let { - if (it) { - sn.add("autocomplete" to "on") - } else { - sn.add("autocomplete" to "off") - } - } - return sn - } -} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt new file mode 100644 index 00000000..e4dd7fe9 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt @@ -0,0 +1,118 @@ +package pl.treksoft.kvision.form.check + +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.form.BoolFormField +import pl.treksoft.kvision.form.FieldLabel +import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.snabbdom.SnOn +import pl.treksoft.kvision.snabbdom.StringBoolPair + +enum class CHECKBOXSTYLE(val className: String) { + DEFAULT("checkbox-default"), + PRIMARY("checkbox-primary"), + SUCCESS("checkbox-success"), + INFO("checkbox-info"), + WARNING("checkbox-warning"), + DANGER("checkbox-danger"), +} + +open class CheckBox(value: Boolean = false, label: String? = null, + rich: Boolean = false) : SimplePanel(setOf("checkbox")), BoolFormField { + + override var value + get() = input.value + set(value) { + input.value = value + } + var startValue + get() = input.startValue + set(value) { + input.startValue = value + } + var name + get() = input.name + set(value) { + input.name = 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 + } + var style: CHECKBOXSTYLE? = null + set(value) { + field = value + refresh() + } + var circled: Boolean = false + set(value) { + field = value + refresh() + } + var inline: Boolean = false + set(value) { + field = value + refresh() + } + override var size + get() = input.size + set(value) { + input.size = value + } + + private val idc = "kv_form_checkbox_" + counter + val input: CheckInput = CheckInput(CHECKINPUTTYPE.CHECKBOX, value, setOf("styled")).apply { id = idc } + val flabel: FieldLabel = FieldLabel(idc, label, rich) + + init { + @Suppress("LeakingThis") + input.eventTarget = this + this.addInternal(input) + this.addInternal(flabel) + counter++ + } + + companion object { + var counter = 0 + } + + @Suppress("UNCHECKED_CAST") + override fun setEventListener(block: SnOn.() -> Unit): Widget { + input.setEventListener(block) + return this + } + + override fun setEventListener(block: SnOn.() -> Unit): Widget { + input.setEventListener(block) + return this + } + + override fun removeEventListeners(): Widget { + input.removeEventListeners() + return this + } + + override fun getSnClass(): List { + val cl = super.getSnClass().toMutableList() + style?.let { + cl.add(it.className to true) + } + if (circled) { + cl.add("checkbox-circle" to true) + } + if (inline) { + cl.add("checkbox-inline" to true) + } + return cl + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt new file mode 100644 index 00000000..a4899f45 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt @@ -0,0 +1,112 @@ +package pl.treksoft.kvision.form.check + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.form.BoolFormField +import pl.treksoft.kvision.form.INPUTSIZE +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, value: Boolean = false, + classes: Set = setOf()) : Widget(classes), BoolFormField { + + init { + this.setInternalEventListener { + click = { + val v = getElementJQuery()?.prop("checked") as Boolean? + self.value = (v == true) + } + change = { + val v = getElementJQuery()?.prop("checked") as Boolean? + self.value = (v == true) + } + } + } + override var value: Boolean = value + set(value) { + field = value + refreshState() + } + @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? = null + set(value) { + field = value + refresh() + } + override var disabled: Boolean = false + set(value) { + field = value + refresh() + } + var extraValue: String? = 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 { + val cl = super.getSnClass().toMutableList() + size?.let { + cl.add(it.className to true) + } + return cl + } + + override fun getSnAttrs(): List { + 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) { + refreshState() + } + + override fun afterPostpatch(node: VNode) { + refreshState() + } + + 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/check/Radio.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt new file mode 100644 index 00000000..284cf26c --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt @@ -0,0 +1,134 @@ +package pl.treksoft.kvision.form.check + +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.form.BoolFormField +import pl.treksoft.kvision.form.FieldLabel +import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.snabbdom.SnOn +import pl.treksoft.kvision.snabbdom.StringBoolPair + +enum class RADIOSTYLE(val className: String) { + DEFAULT("radio-default"), + PRIMARY("radio-primary"), + SUCCESS("radio-success"), + INFO("radio-info"), + WARNING("radio-warning"), + DANGER("radio-danger"), +} + +open class Radio(value: Boolean = false, extraValue: String? = null, label: String? = null, + rich: Boolean = false) : SimplePanel(), BoolFormField { + + override var value + get() = input.value + set(value) { + input.value = value + } + var startValue + get() = input.startValue + set(value) { + input.startValue = value + } + var extraValue + get() = input.extraValue + set(value) { + input.extraValue = value + } + var name + get() = input.name + set(value) { + input.name = 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 + } + var style: RADIOSTYLE? = null + set(value) { + field = value + refresh() + } + var squared: Boolean = false + set(value) { + field = value + refresh() + } + var inline: Boolean = false + set(value) { + field = value + refresh() + } + override var size + get() = input.size + set(value) { + input.size = value + } + + private val idc = "kv_form_radio_" + counter + val input: CheckInput = CheckInput(CHECKINPUTTYPE.RADIO, value).apply { + this.id = idc + this.extraValue = extraValue + } + val flabel: FieldLabel = FieldLabel(idc, label, rich) + + init { + @Suppress("LeakingThis") + input.eventTarget = this + this.addInternal(input) + this.addInternal(flabel) + counter++ + } + + companion object { + var counter = 0 + } + + @Suppress("UNCHECKED_CAST") + override fun setEventListener(block: SnOn.() -> Unit): Widget { + input.setEventListener(block) + return this + } + + override fun setEventListener(block: SnOn.() -> Unit): Widget { + input.setEventListener(block) + return this + } + + override fun removeEventListeners(): Widget { + input.removeEventListeners() + return this + } + + override fun getSnClass(): List { + val cl = super.getSnClass().toMutableList() + if (!squared) { + cl.add("radio" to true) + style?.let { + cl.add(it.className to true) + } + if (inline) { + cl.add("radio-inline" to true) + } + } else { + cl.add("checkbox" to true) + style?.let { + cl.add(it.className.replace("radio", "checkbox") to true) + } + if (inline) { + cl.add("checkbox-inline" to true) + } + } + return cl + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractText.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractText.kt new file mode 100644 index 00000000..4d0ebf51 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractText.kt @@ -0,0 +1,86 @@ +package pl.treksoft.kvision.form.text + +import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.form.FieldLabel +import pl.treksoft.kvision.form.StringFormField +import pl.treksoft.kvision.snabbdom.SnOn + +abstract class AbstractText(label: String? = null, rich: Boolean = false) : + SimplePanel(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 setEventListener(block: SnOn.() -> Unit): Widget { + input.setEventListener(block) + return this + } + + override fun setEventListener(block: SnOn.() -> 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/text/AbstractTextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractTextInput.kt new file mode 100644 index 00000000..09d9ec50 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractTextInput.kt @@ -0,0 +1,113 @@ +package pl.treksoft.kvision.form.text + +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.form.INPUTSIZE +import pl.treksoft.kvision.form.StringFormField +import pl.treksoft.kvision.snabbdom.StringBoolPair +import pl.treksoft.kvision.snabbdom.StringPair + +abstract class AbstractTextInput(value: String? = null, + classes: Set = setOf()) : Widget(classes + "form-control"), StringFormField { + + init { + this.setInternalEventListener { + input = { + val v = getElementJQuery()?.`val`() as String? + if (v != null && v.isNotEmpty()) { + self.value = v + } else { + self.value = null + } + } + } + } + + override var value: String? = value + set(value) { + field = value + refreshState() + } + @Suppress("LeakingThis") + var startValue: String? = value + set(value) { + field = value + this.value = value + refresh() + } + var placeholder: String? = null + set(value) { + field = value + refresh() + } + var name: String? = null + set(value) { + field = value + refresh() + } + var maxlength: Int? = null + set(value) { + field = value + refresh() + } + override var disabled: Boolean = false + 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 { + val cl = super.getSnClass().toMutableList() + size?.let { + cl.add(it.className to true) + } + return cl + } + + override fun getSnAttrs(): List { + 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 + } + + private fun refreshState() { + value?.let { + getElementJQuery()?.`val`(it) + } ?: getElementJQueryD()?.`val`(null) + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt new file mode 100644 index 00000000..c5b8e622 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt @@ -0,0 +1,4 @@ +package pl.treksoft.kvision.form.text + +open class Password(value: String? = null, label: String? = null, rich: Boolean = false) : Text(TEXTINPUTTYPE.PASSWORD, + value, label, rich) diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt new file mode 100644 index 00000000..d5d78616 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt @@ -0,0 +1,25 @@ +package pl.treksoft.kvision.form.text + + +open class Text(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, value: String? = null, + label: String? = null, rich: Boolean = false) : AbstractText(label, rich) { + + var type + get() = input.type + set(value) { + input.type = value + } + var autocomplete + get() = input.autocomplete + set(value) { + input.autocomplete = value + } + + 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/text/TextArea.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/TextArea.kt new file mode 100644 index 00000000..ecfe9cee --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/TextArea.kt @@ -0,0 +1,29 @@ +package pl.treksoft.kvision.form.text + +open class TextArea(cols: Int? = null, rows: Int? = null, value: String? = null, + label: String? = null, rich: 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 + } + + 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/text/TextAreaInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/TextAreaInput.kt new file mode 100644 index 00000000..e8fd9a43 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/TextAreaInput.kt @@ -0,0 +1,44 @@ +package pl.treksoft.kvision.form.text + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.snabbdom.StringPair + +open class TextAreaInput(cols: Int? = null, rows: Int? = null, value: String? = null, classes: Set = setOf()) : + AbstractTextInput(value, 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 startValue?.let { + kvh("textarea", arrayOf(it)) + } ?: kvh("textarea") + } + + override fun getSnAttrs(): List { + 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/text/TextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/TextInput.kt new file mode 100644 index 00000000..436c0fec --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/TextInput.kt @@ -0,0 +1,44 @@ +package pl.treksoft.kvision.form.text + +import com.github.snabbdom.VNode +import pl.treksoft.kvision.snabbdom.StringPair + +enum class TEXTINPUTTYPE(val type: String) { + TEXT("text"), + PASSWORD("password") +} + +open class TextInput(type: TEXTINPUTTYPE = TEXTINPUTTYPE.TEXT, value: String? = null, classes: Set = setOf()) : + AbstractTextInput(value, classes) { + + var type: TEXTINPUTTYPE = type + set(value) { + field = value + refresh() + } + var autocomplete: Boolean? = null + set(value) { + field = value + refresh() + } + + override fun render(): VNode { + return kvh("input") + } + + override fun getSnAttrs(): List { + val sn = super.getSnAttrs().toMutableList() + sn.add("type" to type.type) + startValue?.let { + sn.add("value" to it) + } + autocomplete?.let { + if (it) { + sn.add("autocomplete" to "on") + } else { + sn.add("autocomplete" to "off") + } + } + return sn + } +} -- cgit