diff options
| author | Robert Jaros <rjaros@finn.pl> | 2018-02-12 10:50:42 +0100 |
|---|---|---|
| committer | Robert Jaros <rjaros@finn.pl> | 2018-02-12 10:50:42 +0100 |
| commit | bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c (patch) | |
| tree | 39288bdcb59ec48cb70f1c5820fc1854949744da /src | |
| parent | 2feea5e7cf8d492663e826ebcfb0a58e61820352 (diff) | |
| download | kvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.tar.gz kvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.tar.bz2 kvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.zip | |
Type safe DSL builders
Diffstat (limited to 'src')
39 files changed, 604 insertions, 31 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Container.kt b/src/main/kotlin/pl/treksoft/kvision/core/Container.kt index d2650f7e..653f4a79 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Container.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Container.kt @@ -41,7 +41,7 @@ interface Container : Component { /** * Operator function for adding children in a DSL style. - * @param child children components + * @param children children components * @return current container */ operator fun invoke(vararg children: Component): Container { diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt index 5f664563..532a6bb7 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt @@ -516,4 +516,15 @@ open class Widget(classes: Set<String> = setOf()) : StyledComponent() { override fun dispose() { } + + companion object { + /** + * 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) { + this.add(Widget(classes).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt b/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt index 47470c83..81765846 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/WidgetWrapper.kt @@ -54,4 +54,18 @@ open class WidgetWrapper(internal var wrapped: Component?, classes: Set<String> 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 + ) { + this.add(WidgetWrapper(wrapped, classes).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt index 13b5ce00..fa545dc8 100644 --- a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt +++ b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt @@ -123,4 +123,20 @@ class DataContainer<M : DataComponent, C : Component>( onUpdateHandler = null return this } + + companion object { + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun <M : DataComponent, C : Component> Container.dataContainer( + model: ObservableList<M>, + binding: (Int) -> C, + child: Container = VPanel(), + init: (DataContainer<M, C>.() -> Unit)? = null + ) { + this.add(DataContainer(model, binding, child, init)) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt b/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt index b80c6dc5..1af71dc8 100644 --- a/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt +++ b/src/main/kotlin/pl/treksoft/kvision/dropdown/DropDown.kt @@ -23,6 +23,7 @@ package pl.treksoft.kvision.dropdown import com.github.snabbdom.VNode import pl.treksoft.kvision.core.Component +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.CssSize import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.StringPair @@ -154,10 +155,6 @@ open class DropDown( counter++ } - companion object { - internal var counter = 0 - } - override fun add(child: Component): SimplePanel { list.add(child) return this @@ -237,6 +234,23 @@ open class DropDown( open fun toggle() { this.list.getElementJQueryD()?.dropdown("toggle") } + + companion object { + internal var counter = 0 + + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.dropDown( + text: String, elements: List<StringPair>? = null, icon: String? = null, + style: BUTTONSTYLE = BUTTONSTYLE.DEFAULT, disabled: Boolean = false, + classes: Set<String> = setOf(), init: (DropDown.() -> Unit)? = null + ) { + this.add(DropDown(text, elements, icon, style, disabled, classes).apply { init?.invoke(this) }) + } + } } internal class DropDownButton( diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index 0a12f818..70c44fd7 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -22,6 +22,7 @@ package pl.treksoft.kvision.form import com.github.snabbdom.VNode +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.form.check.CheckBox import pl.treksoft.kvision.form.check.Radio @@ -214,4 +215,20 @@ open class FormPanel<K>( open fun validate(): Boolean { return form.validate() } + + companion object { + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun <K> Container.formPanel( + type: FORMTYPE? = null, classes: Set<String> = setOf(), + modelFactory: (Map<String, Any?>) -> K + ): FormPanel<K> { + val panel = FormPanel(type, classes, modelFactory) + this.add(panel) + return panel + } + } } 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 0bad8440..4c373a2d 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt @@ -22,6 +22,7 @@ package pl.treksoft.kvision.form.check import org.w3c.dom.events.MouseEvent +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.BoolFormControl @@ -153,10 +154,6 @@ open class CheckBox( counter++ } - companion object { - internal var counter = 0 - } - @Suppress("UNCHECKED_CAST") override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget { input.setEventListener(block) @@ -201,4 +198,20 @@ open class CheckBox( } return this } + + 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, label: String? = null, + rich: Boolean = false, init: (CheckBox.() -> Unit)? = null + ) { + this.add(CheckBox(value, label, rich).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt index 26a06fa7..8d083b42 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt @@ -23,6 +23,7 @@ package pl.treksoft.kvision.form.check import com.github.snabbdom.VNode import org.w3c.dom.events.MouseEvent +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.StringPair import pl.treksoft.kvision.core.Widget @@ -179,4 +180,18 @@ open class CheckInput( } return this } + + companion object { + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.checkInput( + type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, value: Boolean = false, + classes: Set<String> = setOf(), init: (CheckInput.() -> Unit)? = null + ) { + this.add(CheckInput(type, value, classes).apply { init?.invoke(this) }) + } + } } 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 a8059230..13f4c4cd 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt @@ -22,6 +22,7 @@ package pl.treksoft.kvision.form.check import org.w3c.dom.events.MouseEvent +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.BoolFormControl @@ -164,10 +165,6 @@ open class Radio( counter++ } - companion object { - internal var counter = 0 - } - @Suppress("UNCHECKED_CAST") override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget { input.setEventListener(block) @@ -221,4 +218,20 @@ open class Radio( } return this } + + 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 + ) { + this.add(Radio(value, extraValue, name, label, rich).apply { init?.invoke(this) }) + } + } } 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 7bd13d4e..80736d59 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt @@ -21,6 +21,7 @@ */ package pl.treksoft.kvision.form.check +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.StringPair import pl.treksoft.kvision.core.Widget @@ -110,10 +111,6 @@ open class RadioGroup( counter++ } - companion object { - internal var counter = 0 - } - override fun getSnClass(): List<StringBoolPair> { val cl = super.getSnClass().toMutableList() if (validatorError != null) { @@ -166,4 +163,19 @@ open class RadioGroup( this.addInternal(validationInfo) } + 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, inline: Boolean = false, + label: String? = null, rich: Boolean = false, init: (RadioGroup.() -> Unit)? = null + ) { + this.add(RadioGroup(options, value, inline, label, rich).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt index e8494d58..dc55dd04 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt @@ -22,6 +22,7 @@ package pl.treksoft.kvision.form.select import pl.treksoft.kvision.core.Component +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.StringPair import pl.treksoft.kvision.core.Widget @@ -200,10 +201,6 @@ open class Select( counter++ } - companion object { - internal var counter = 0 - } - override fun getSnClass(): List<StringBoolPair> { val cl = super.getSnClass().toMutableList() if (validatorError != null) { @@ -272,4 +269,21 @@ open class Select( open fun toggleOptions() { input.toggleOptions() } + + companion object { + internal var counter = 0 + + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.select( + options: List<StringPair>? = null, value: String? = null, + multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, + rich: Boolean = false, init: (Select.() -> Unit)? = null + ) { + this.add(Select(options, value, multiple, ajaxOptions, label, rich).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt index 7374a30d..be986f02 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -24,6 +24,7 @@ package pl.treksoft.kvision.form.select import com.github.snabbdom.VNode import pl.treksoft.kvision.KVManager.KVNULL import pl.treksoft.kvision.core.Component +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.CssSize import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.StringPair @@ -384,4 +385,19 @@ open class SelectInput( } ?: getElementJQueryD()?.selectpicker("val", null) } + + companion object { + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.selectInput( + options: List<StringPair>? = null, value: String? = null, + multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, + classes: Set<String> = setOf(), init: (SelectInput.() -> Unit)? = null + ) { + this.add(SelectInput(options, value, multiple, ajaxOptions, classes).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt b/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt index 4d7a3150..2a5d2cbe 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/spinner/Spinner.kt @@ -21,6 +21,7 @@ */ package pl.treksoft.kvision.form.spinner +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.FieldLabel @@ -191,10 +192,6 @@ open class Spinner( counter++ } - companion object { - internal var counter = 0 - } - override fun getSnClass(): List<StringBoolPair> { val cl = super.getSnClass().toMutableList() if (validatorError != null) { @@ -238,4 +235,26 @@ open class Spinner( input.spinDown() return this } + + companion object { + internal var counter = 0 + + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.spinner( + value: Number? = null, min: Int = 0, max: Int = DEFAULT_MAX, step: Double = DEFAULT_STEP, + decimals: Int = 0, buttonsType: BUTTONSTYPE = BUTTONSTYPE.VERTICAL, + forceType: FORCETYPE = FORCETYPE.NONE, label: String? = null, + rich: Boolean = false, init: (Spinner.() -> Unit)? = null + ) { + this.add(Spinner(value, min, max, step, decimals, buttonsType, forceType, label, rich).apply { + init?.invoke( + this + ) + }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt index 63ad0852..6dcb2b4a 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/spinner/SpinnerInput.kt @@ -23,6 +23,7 @@ package pl.treksoft.kvision.form.spinner import com.github.snabbdom.VNode import pl.treksoft.jquery.JQuery +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringBoolPair import pl.treksoft.kvision.core.StringPair import pl.treksoft.kvision.core.Widget @@ -336,4 +337,26 @@ open class SpinnerInput( this.forcestepdivisibility = forceType.value } } + + companion object { + internal var counter = 0 + + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.spinnerInput( + value: Number? = null, min: Int = 0, max: Int = DEFAULT_MAX, step: Double = DEFAULT_STEP, + decimals: Int = 0, buttonsType: BUTTONSTYPE = BUTTONSTYPE.VERTICAL, + forceType: FORCETYPE = FORCETYPE.NONE, classes: Set<String> = setOf(), + init: (SpinnerInput.() -> Unit)? = null + ) { + this.add(SpinnerInput(value, min, max, step, decimals, buttonsType, forceType, classes).apply { + init?.invoke( + this + ) + }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt index f3f50a63..39c9a306 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/Password.kt @@ -21,6 +21,8 @@ */ package pl.treksoft.kvision.form.text +import pl.treksoft.kvision.core.Container + /** * Form field password component. * @@ -32,4 +34,17 @@ package pl.treksoft.kvision.form.text open class Password(value: String? = null, label: String? = null, rich: Boolean = false) : Text( TEXTINPUTTYPE.PASSWORD, value, label, rich -) +) { + companion object { + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.password( + value: String? = null, label: String? = null, rich: Boolean = false, init: (Password.() -> Unit)? = null + ) { + this.add(Password(value, label, rich).apply { init?.invoke(this) }) + } + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/RichText.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/RichText.kt index 39083577..9d5c8c49 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/text/RichText.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/RichText.kt @@ -21,6 +21,8 @@ */ package pl.treksoft.kvision.form.text +import pl.treksoft.kvision.core.Container + /** * Form field rich text component. * @@ -51,4 +53,17 @@ open class RichText( this.addInternal(input) this.addInternal(validationInfo) } + + companion object { + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.richText( + value: String? = null, label: String? = null, rich: Boolean = false, init: (RichText.() -> Unit)? = null + ) { + this.add(RichText(value, label, rich).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt index d90c5769..f802cc2a 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt @@ -23,6 +23,7 @@ package pl.treksoft.kvision.form.text import com.github.snabbdom.VNode import pl.treksoft.jquery.jQuery +import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.StringPair import kotlin.browser.document @@ -108,4 +109,17 @@ open class RichTextInput(value: String? = null, classes: Set<String> = setOf()) override fun changeValue() { // disabled parent class functionality } + + companion object { + /** + * DSL builder extension function + * + * It takes the same parameters as the constructor of the built component. + */ + fun Container.richTextInput( + value: String? = null, classes: Set<String> = setOf(), init: (RichTextInput.() -> Unit)? = null + ) { + this.add(RichTextInput(value, classes).apply { init?.invoke(this) }) + } + } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt index 298a3209..2878dc66 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/Text.kt @@ -21,6 +21,8 @@ */ package pl.treksoft.kvision.form.text +import pl.treksoft.kvision.core.Container + /** * Form field text component. * @@ -60,4 +62,18 @ |
