From bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 12 Feb 2018 10:50:42 +0100 Subject: Type safe DSL builders --- .../pl/treksoft/kvision/form/spinner/Spinner.kt | 27 ++++++++++++++++++---- .../treksoft/kvision/form/spinner/SpinnerInput.kt | 23 ++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) (limited to 'src/main/kotlin/pl/treksoft/kvision/form/spinner') 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 { 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 = setOf(), + init: (SpinnerInput.() -> Unit)? = null + ) { + this.add(SpinnerInput(value, min, max, step, decimals, buttonsType, forceType, classes).apply { + init?.invoke( + this + ) + }) + } + } } -- cgit