diff options
| author | Robert Jaros <rjaros@finn.pl> | 2019-10-17 21:58:34 +0200 |
|---|---|---|
| committer | Robert Jaros <rjaros@finn.pl> | 2019-10-17 21:58:34 +0200 |
| commit | 736b80835f67c9c34657074ebcfbe0752bef1c18 (patch) | |
| tree | 82d1e18a9ec07692dfe5dd31f470b842a9950a89 /src | |
| parent | 53b325d52208bfd44ba6a524ce3dda5379aed699 (diff) | |
| download | kvision-736b80835f67c9c34657074ebcfbe0752bef1c18.tar.gz kvision-736b80835f67c9c34657074ebcfbe0752bef1c18.tar.bz2 kvision-736b80835f67c9c34657074ebcfbe0752bef1c18.zip | |
Move DSL builder functions out of the companion objects (#93)
Diffstat (limited to 'src')
56 files changed, 798 insertions, 888 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt index ff91c429..7c650e6c 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt @@ -96,26 +96,25 @@ open class Style(className: String? = null, parentStyle: Style? = null, init: (S companion object { internal var counter = 0 internal var styles = mutableListOf<Style>() - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Widget.style(className: String? = null, init: (Style.() -> Unit)? = null): Style { - val style = Style(className, null, init) - this.addCssClass(style) - return style - } - - /** - * DSL builder extension function for cascading styles. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Style.style(className: String? = null, init: (Style.() -> Unit)? = null): Style { - return Style(className, this, init) - } } +} +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Widget.style(className: String? = null, init: (Style.() -> Unit)? = null): Style { + val style = Style(className, null, init) + this.addCssClass(style) + return style +} + +/** + * DSL builder extension function for cascading styles. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Style.style(className: String? = null, init: (Style.() -> Unit)? = null): Style { + return Style(className, this, init) } diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt index 24543b1f..9b3e9226 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt @@ -810,16 +810,16 @@ open class Widget(classes: Set<String> = setOf()) : StyledComponent(), Component companion object { private var counter: Long = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.widget(classes: Set<String> = setOf(), init: (Widget.() -> Unit)? = null): Widget { - val widget = Widget(classes).apply { init?.invoke(this) } - this.add(widget) - return widget - } } } + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.widget(classes: Set<String> = setOf(), init: (Widget.() -> Unit)? = null): Widget { + val widget = Widget(classes).apply { init?.invoke(this) } + this.add(widget) + return widget +} diff --git a/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt b/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt index 739cb99e..1d5ada90 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt @@ -23,7 +23,6 @@ package pl.treksoft.kvision.core import com.github.snabbdom.VNode import pl.treksoft.kvision.panel.SimplePanel -import kotlin.Unit /** * This class allows to wrap a component into separately styled DIV element. @@ -55,21 +54,19 @@ open class WidgetWrapper(internal var wrapped: Component?, classes: Set<String> wrapped?.clearParent() wrapped = null } +} - companion object { - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.widgetWrapper( - wrapped: Component?, - classes: Set<String> = setOf(), - init: (WidgetWrapper.() -> Unit)? = null - ): WidgetWrapper { - val widgetWrapper = WidgetWrapper(wrapped, classes).apply { init?.invoke(this) } - this.add(widgetWrapper) - return widgetWrapper - } - } +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.widgetWrapper( + wrapped: Component?, + classes: Set<String> = setOf(), + init: (WidgetWrapper.() -> Unit)? = null +): WidgetWrapper { + val widgetWrapper = WidgetWrapper(wrapped, classes).apply { init?.invoke(this) } + this.add(widgetWrapper) + return widgetWrapper } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FieldLabel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FieldLabel.kt index 4684b1be..8413eb3c 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FieldLabel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FieldLabel.kt @@ -46,20 +46,18 @@ open class FieldLabel( override fun getSnAttrs(): List<StringPair> { return super.getSnAttrs() + ("for" to forId) } +} - companion object { - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.fieldLabel( - forId: String, content: String? = null, rich: Boolean = false, - classes: Set<String> = setOf("control-label"), init: (FieldLabel.() -> Unit)? = null - ): FieldLabel { - val fieldLabel = FieldLabel(forId, content, rich, classes).apply { init?.invoke(this) } - this.add(fieldLabel) - return fieldLabel - } - } +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.fieldLabel( + forId: String, content: String? = null, rich: Boolean = false, + classes: Set<String> = setOf("control-label"), init: (FieldLabel.() -> Unit)? = null +): FieldLabel { + val fieldLabel = FieldLabel(forId, content, rich, classes).apply { init?.invoke(this) } + this.add(fieldLabel) + return fieldLabel } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index e8fa0a42..894ea4bc 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -28,6 +28,7 @@ import kotlinx.serialization.serializer import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.StringPair +import pl.treksoft.kvision.form.FormPanel.Companion.create import pl.treksoft.kvision.html.Div import pl.treksoft.kvision.panel.SimplePanel import pl.treksoft.kvision.types.KFile @@ -390,21 +391,6 @@ open class FormPanel<K : Any>( } companion object { - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - inline fun <reified K : Any> Container.formPanel( - method: FormMethod? = null, action: String? = null, enctype: FormEnctype? = null, - type: FormType? = null, condensed: Boolean = false, classes: Set<String> = setOf(), - noinline init: (FormPanel<K>.() -> Unit)? = null - ): FormPanel<K> { - val formPanel = create<K>(method, action, enctype, type, condensed, classes) - init?.invoke(formPanel) - this.add(formPanel) - return formPanel - } @UseExperimental(ImplicitReflectionSerializer::class) inline fun <reified K : Any> create( @@ -419,3 +405,19 @@ open class FormPanel<K : Any>( } } + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +inline fun <reified K : Any> Container.formPanel( + method: FormMethod? = null, action: String? = null, enctype: FormEnctype? = null, + type: FormType? = null, condensed: Boolean = false, classes: Set<String> = setOf(), + noinline init: (FormPanel<K>.() -> Unit)? = null +): FormPanel<K> { + val formPanel = create<K>(method, action, enctype, type, condensed, classes) + init?.invoke(formPanel) + this.add(formPanel) + return formPanel +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt index cb962850..ac25b30d 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt @@ -191,19 +191,19 @@ open class CheckBox( companion object { internal var counter = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.checkBox( - value: Boolean = false, name: String? = null, label: String? = null, - rich: Boolean = false, init: (CheckBox.() -> Unit)? = null - ): CheckBox { - val checkBox = CheckBox(value, name, label, rich).apply { init?.invoke(this) } - this.add(checkBox) - return checkBox - } } } + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.checkBox( + value: Boolean = false, name: String? = null, label: String? = null, + rich: Boolean = false, init: (CheckBox.() -> Unit)? = null +): CheckBox { + val checkBox = CheckBox(value, name, label, rich).apply { init?.invoke(this) } + this.add(checkBox) + return checkBox +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBoxInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBoxInput.kt index ea58268d..ea8bf7db 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBoxInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBoxInput.kt @@ -33,21 +33,18 @@ import pl.treksoft.kvision.core.Container open class CheckBoxInput( value: Boolean = false, classes: Set<String> = setOf() -) : CheckInput(CheckInputType.CHECKBOX, value, classes) { +) : CheckInput(CheckInputType.CHECKBOX, value, classes) - companion object { - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.checkBoxInput( - value: Boolean = false, - classes: Set<String> = setOf(), init: (CheckInput.() -> Unit)? = null - ): CheckBoxInput { - val checkBoxInput = CheckBoxInput(value, classes).apply { init?.invoke(this) } - this.add(checkBoxInput) - return checkBoxInput - } - } +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.checkBoxInput( + value: Boolean = false, + classes: Set<String> = setOf(), init: (CheckInput.() -> Unit)? = null +): CheckBoxInput { + val checkBoxInput = CheckBoxInput(value, classes).apply { init?.invoke(this) } + this.add(checkBoxInput) + return checkBoxInput } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt index e9551196..bb73da60 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt @@ -206,19 +206,19 @@ open class Radio( companion object { internal var counter = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.radio( - value: Boolean = false, extraValue: String? = null, name: String? = null, label: String? = null, - rich: Boolean = false, init: (Radio.() -> Unit)? = null - ): Radio { - val radio = Radio(value, extraValue, name, label, rich).apply { init?.invoke(this) } - this.add(radio) - return radio - } } } + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.radio( + value: Boolean = false, extraValue: String? = null, name: String? = null, label: String? = null, + rich: Boolean = false, init: (Radio.() -> Unit)? = null +): Radio { + val radio = Radio(value, extraValue, name, label, rich).apply { init?.invoke(this) } + this.add(radio) + return radio +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt index 14b426a3..bee57db4 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt @@ -227,19 +227,19 @@ open class RadioGroup( companion object { internal var counter = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.radioGroup( - options: List<StringPair>? = null, value: String? = null, name: String? = null, inline: Boolean = false, - label: String? = null, rich: Boolean = false, init: (RadioGroup.() -> Unit)? = null - ): RadioGroup { - val radioGroup = RadioGroup(options, value, name, inline, label, rich).apply { init?.invoke(this) } - this.add(radioGroup) - return radioGroup - } } } + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.radioGroup( + options: List<StringPair>? = null, value: String? = null, name: String? = null, inline: Boolean = false, + label: String? = null, rich: Boolean = false, init: (RadioGroup.() -> Unit)? = null +): RadioGroup { + val radioGroup = RadioGroup(options, value, name, inline, label, rich).apply { init?.invoke(this) } + this.add(radioGroup) + return radioGroup +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroupInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroupInput.kt index d4301709..5a47c3ce 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroupInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroupInput.kt @@ -174,19 +174,19 @@ open class RadioGroupInput( companion object { internal var counter = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.radioGroupInput( - options: List<StringPair>? = null, value: String? = null, name: String? = null, inline: Boolean = false, - init: (RadioGroupInput.() -> Unit)? = null - ): RadioGroupInput { - val radioGroupInput = RadioGroupInput(options, value, name, inline).apply { init?.invoke(this) } - this.add(radioGroupInput) - return radioGroupInput - } } } + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.radioGroupInput( + options: List<StringPair>? = null, value: String? = null, name: String? = null, inline: Boolean = false, + init: (RadioGroupInput.() -> Unit)? = null +): RadioGroupInput { + val radioGroupInput = RadioGroupInput(options, value, name, inline).apply { init?.invoke(this) } + this.add(radioGroupInput) + return radioGroupInput +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioInput.kt index 9c6a1670..72bb4cd0 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioInput.kt @@ -33,21 +33,18 @@ import pl.treksoft.kvision.core.Container open class RadioInput( value: Boolean = false, classes: Set<String> = setOf() -) : CheckInput(CheckInputType.RADIO, value, classes) { +) : CheckInput(CheckInputType.RADIO, value, classes) - companion object { - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.radioInput( - value: Boolean = false, - classes: Set<String> = setOf(), init: (CheckInput.() -> Unit)? = null - ): RadioInput { - val checkBoxInput = RadioInput(value, classes).apply { init?.invoke(this) } - this.add(checkBoxInput) - return checkBoxInput - } - } +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.radioInput( + value: Boolean = false, + classes: Set<String> = setOf(), init: (CheckInput.() -> Unit)? = null +): RadioInput { + val checkBoxInput = RadioInput(value, classes).apply { init?.invoke(this) } + this.add(checkBoxInput) + return checkBoxInput } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SimpleSelect.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SimpleSelect.kt index bef14bfa..1bcef061 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SimpleSelect.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/SimpleSelect.kt @@ -186,24 +186,24 @@ open class SimpleSelect( companion object { internal var counter = 0 - - /** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ - fun Container.simpleSelect( - options: List<StringPair>? = null, - value: String? = null, - emptyOption: Boolean = false, - name: String? = null, - label: String? = null, - rich: Boolean = false, - init: (SimpleSelect.() -> Unit)? = null - ): SimpleSelect { - val simpleSelect = SimpleSelect(options, value, emptyOption, name, label, rich).apply { init?.invoke(this) } - this.add(simpleSelect) - return simpleSelect - } } } + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the b |
