diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-03-24 00:47:11 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-03-24 00:47:11 +0100 |
commit | 38d8447b7024112f54a4d2a4404840cae5e4d849 (patch) | |
tree | 5715fd6ebc195390d3e41f1f288175f6baa2d74f /src/main | |
parent | c56ddc135c7caa9a9b62ced9425544d599e9eb69 (diff) | |
download | kvision-38d8447b7024112f54a4d2a4404840cae5e4d849.tar.gz kvision-38d8447b7024112f54a4d2a4404840cae5e4d849.tar.bz2 kvision-38d8447b7024112f54a4d2a4404840cae5e4d849.zip |
Some fixes for CSS style objects.
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/core/Style.kt | 14 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/panel/Root.kt | 39 |
2 files changed, 28 insertions, 25 deletions
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<Style>() /** * DSL builder extension function. diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt index 1a90f7fc..16d3a0f8 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt @@ -46,17 +46,21 @@ import pl.treksoft.kvision.utils.snOpt */ @Suppress("TooManyFunctions") class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Unit)? = null) : SimplePanel() { - private val styles: MutableList<Style> = mutableListOf() private val modals: MutableList<Modal> = mutableListOf() private val contextMenus: MutableList<ContextMenu> = mutableListOf() private var rootVnode: VNode = renderVNode() internal var renderDisabled = false + private val isFirstRoot = roots.isEmpty() + init { rootVnode = KVManager.patch(id, this.renderVNode()) this.id = id roots.add(this) + if (isFirstRoot) { + Style.styles.forEach { it.parent = this } + } @Suppress("LeakingThis") init?.invoke(this) } @@ -71,12 +75,6 @@ class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Uni } } - internal fun addStyle(style: Style) { - styles.add(style) - style.parent = this - refresh() - } - internal fun addModal(modal: Modal) { modals.add(modal) modal.parent = this @@ -96,10 +94,14 @@ class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Uni } private fun stylesVNodes(): Array<VNode> { - val visibleStyles = styles.filter { it.visible } - return if (visibleStyles.isNotEmpty()) { - val stylesDesc = visibleStyles.joinToString("\n") { it.generateStyle() } - arrayOf(h("style", arrayOf(stylesDesc))) + return if (isFirstRoot) { + val visibleStyles = Style.styles.filter { it.visible } + if (visibleStyles.isNotEmpty()) { + val stylesDesc = visibleStyles.joinToString("\n") { it.generateStyle() } + arrayOf(h("style", arrayOf(stylesDesc))) + } else { + arrayOf() + } } else { arrayOf() } @@ -135,18 +137,25 @@ class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Uni } override fun dispose() { - styles.forEach { it.dispose() } - modals.forEach { it.dispose() } - contextMenus.forEach { it.dispose() } super.dispose() roots.remove(this) + if (isFirstRoot) { + Style.styles.clear() + } } companion object { internal val roots: MutableList<Root> = mutableListOf() + internal fun getFirstRoot(): Root? { + return if (roots.isNotEmpty()) + roots[0] + else + null + } + internal fun getLastRoot(): Root? { - return if (roots.size > 0) + return if (roots.isNotEmpty()) roots[roots.size - 1] else null |