From dfbd27b66a8ad7f3db527480037f21ba2d7b7292 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 20 Jan 2020 13:13:20 +0100 Subject: Better support for applications with multiple roots. --- src/main/kotlin/pl/treksoft/kvision/panel/Root.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/main/kotlin/pl/treksoft/kvision') diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt index 8847a3c6..b1bb36a8 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt @@ -31,6 +31,7 @@ import pl.treksoft.kvision.core.Style import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.utils.snClasses import pl.treksoft.kvision.utils.snOpt +import kotlin.browser.document /** * Root container. @@ -44,7 +45,7 @@ class Root : SimplePanel { private val fixed: Boolean private val contextMenus: MutableList = mutableListOf() - private var rootVnode: VNode = renderVNode() + private var rootVnode: VNode? = null internal var renderDisabled = false @@ -59,7 +60,9 @@ class Root : SimplePanel { */ constructor(id: String, fixed: Boolean = false, init: (Root.() -> Unit)? = null) : super() { this.fixed = fixed - rootVnode = KVManager.patch(id, this.renderVNode()) + if (document.getElementById(id) != null) { + rootVnode = KVManager.patch(id, this.renderVNode()) + } this.id = id @Suppress("LeakingThis") init?.invoke(this) @@ -140,15 +143,17 @@ class Root : SimplePanel { } internal fun reRender(): Root { - if (!renderDisabled) { - rootVnode = KVManager.patch(rootVnode, renderVNode()) + if (!renderDisabled && rootVnode != null) { + rootVnode = KVManager.patch(rootVnode!!, renderVNode()) } return this } internal fun restart() { - rootVnode = KVManager.patch(rootVnode, h("div")) - rootVnode = KVManager.patch(rootVnode, renderVNode()) + if (rootVnode != null) { + rootVnode = KVManager.patch(rootVnode!!, h("div")) + rootVnode = KVManager.patch(rootVnode!!, renderVNode()) + } } override fun getRoot(): Root? { -- cgit