diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-10-14 00:05:08 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-10-14 00:05:08 +0200 |
commit | b44c032818774035b3fe1665f64254366b81e60d (patch) | |
tree | 691e29cb85b220a5b1e4f9189df757abf809f903 | |
parent | 72f03505810e2a4fff3abfb224a8975574de5302 (diff) | |
download | kvision-b44c032818774035b3fe1665f64254366b81e60d.tar.gz kvision-b44c032818774035b3fe1665f64254366b81e60d.tar.bz2 kvision-b44c032818774035b3fe1665f64254366b81e60d.zip |
Refactoring Root class constructors (#92)
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/panel/Root.kt | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt index ce86c678..c206ea02 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt @@ -38,21 +38,11 @@ import pl.treksoft.kvision.utils.snOpt * This container is bound to the specific element in the main HTML file of the project. * It is always the root of components tree and it is responsible for rendering and updating * Snabbdom virtual DOM. - * - * @constructor - * @param id ID attribute of element in the main HTML file - * @param element HTML element in the DOM tree - * @param fixed if false, the container is rendered with Bootstrap "container-fluid" class, - * otherwise it's rendered with "container" class (default is false) - * @param init an initializer extension function */ @Suppress("TooManyFunctions") -class Root( - id: String? = null, - element: HTMLElement? = null, - private val fixed: Boolean = false, - init: (Root.() -> Unit)? = null -) : SimplePanel() { +class Root : SimplePanel { + + private val fixed: Boolean private val contextMenus: MutableList<Widget> = mutableListOf() private var rootVnode: VNode = renderVNode() @@ -60,22 +50,41 @@ class Root( val isFirstRoot = roots.isEmpty() + /** + * @constructor + * @param id ID attribute of element in the main HTML file + * @param fixed if false, the container is rendered with Bootstrap "container-fluid" class, + * otherwise it's rendered with "container" class (default is false) + * @param init an initializer extension function + */ + constructor(id: String, fixed: Boolean = false, init: (Root.() -> Unit)? = null) : super() { + this.fixed = fixed + rootVnode = KVManager.patch(id, this.renderVNode()) + this.id = id + @Suppress("LeakingThis") + init?.invoke(this) + } + + /** + * @constructor + * @param element HTML element in the DOM tree + * @param fixed if false, the container is rendered with Bootstrap "container-fluid" class, + * otherwise it's rendered with "container" class (default is false) + * @param init an initializer extension function + */ + constructor(element: HTMLElement, fixed: Boolean = false, init: (Root.() -> Unit)? = null) : super() { + this.fixed = fixed + rootVnode = KVManager.patch(element, this.renderVNode()) + this.id = "kv_root_${counter++}" + @Suppress("LeakingThis") + init?.invoke(this) + } + init { - if (id != null) { - rootVnode = KVManager.patch(id, this.renderVNode()) - this.id = id - } else if (element != null) { - rootVnode = KVManager.patch(element, this.renderVNode()) - this.id = "kv_root_${counter++}" - } else { - throw IllegalArgumentException("No root element specified!") - } roots.add(this) if (isFirstRoot) { modals.forEach { it.parent = this } } - @Suppress("LeakingThis") - init?.invoke(this) } override fun render(): VNode { @@ -211,7 +220,7 @@ class Root( * @param init an initializer extension function */ fun Application.root(id: String, fixed: Boolean = false, init: Root.() -> Unit) { - Root(id, fixed = fixed, init = init) + Root(id, fixed, init) } /** @@ -222,7 +231,7 @@ class Root( * @param init an initializer extension function */ fun Application.root(element: HTMLElement, fixed: Boolean = false, init: Root.() -> Unit) { - Root(element = element, fixed = fixed, init = init) + Root(element, fixed, init) } } |