From 38d8447b7024112f54a4d2a4404840cae5e4d849 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sun, 24 Mar 2019 00:47:11 +0100 Subject: Some fixes for CSS style objects. --- src/main/kotlin/pl/treksoft/kvision/core/Style.kt | 14 +++----- src/main/kotlin/pl/treksoft/kvision/panel/Root.kt | 39 +++++++++++++--------- .../test/pl/treksoft/kvision/core/StyleSpec.kt | 13 ++++---- 3 files changed, 35 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt index c2858fa0..62154f77 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt @@ -40,7 +40,7 @@ import pl.treksoft.kvision.panel.Root open class Style(className: String? = null, parentStyle: Style? = null, init: (Style.() -> Unit)? = null) : StyledComponent() { - override var parent: Container? = null + override var parent: Container? = Root.getFirstRoot() private val newClassName: String = if (parentStyle == null) { className ?: "kv_styleclass_${counter++}" @@ -54,15 +54,7 @@ open class Style(className: String? = null, parentStyle: Style? = null, init: (S var className: String by refreshOnUpdate(newClassName) init { - val root = Root.getLastRoot() - @Suppress("LeakingThis") - parent = root - if (root != null) { - @Suppress("LeakingThis") - root.addStyle(this) - } else { - println("At least one Root object is required to create a style object!") - } + styles.add(this) @Suppress("LeakingThis") init?.invoke(this) } @@ -141,10 +133,12 @@ open class Style(className: String? = null, parentStyle: Style? = null, init: (S } override fun dispose() { + styles.remove(this) } companion object { internal var counter = 0 + internal var styles = mutableListOf
", element?.innerHTML, "Should render correct style element" ) + Style.styles.clear() } } @Test fun renderCustomClass() { run { - val root = Root("test", true) { + Root("test", true) { widget { style("customclass") { margin = 2.px @@ -68,20 +69,20 @@ class StyleSpec : DomSpec { } } } - root.reRender() val element = document.getElementById("test") assertEqualsHtml( "
", element?.innerHTML, "Should render correct style element with custom class name" ) + Style.styles.clear() } } @Test fun renderSubclass() { run { - val root = Root("test", true) { + Root("test", true) { widget { style("customclass") { margin = 2.px @@ -93,7 +94,6 @@ class StyleSpec : DomSpec { } } } - root.reRender() val element = document.getElementById("test") assertEqualsHtml( "