diff options
author | Robert Jaros <rjaros@finn.pl> | 2018-01-28 13:58:21 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2018-01-28 13:58:21 +0100 |
commit | 7a9f04ad13d8b9c961c6c1e3c3642fd10c1678bc (patch) | |
tree | 02667ba6c322f689d9043ff6e5b2305077f687f3 /src | |
parent | 3fae0d10e3066183828c3114653ec89fde24c79e (diff) | |
download | kvision-7a9f04ad13d8b9c961c6c1e3c3642fd10c1678bc.tar.gz kvision-7a9f04ad13d8b9c961c6c1e3c3642fd10c1678bc.tar.bz2 kvision-7a9f04ad13d8b9c961c6c1e3c3642fd10c1678bc.zip |
Routing integration for Tab and Stack panels
Diffstat (limited to 'src')
3 files changed, 25 insertions, 6 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt index 1d4b1b68..e69cc416 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt @@ -2,6 +2,7 @@ package pl.treksoft.kvision.panel import com.github.snabbdom.VNode import pl.treksoft.kvision.core.Component +import pl.treksoft.kvision.routing.routing open class StackPanel( private val activateLast: Boolean = true, @@ -21,6 +22,13 @@ open class StackPanel( } } + open fun add(panel: Component, route: String): StackPanel { + add(panel) + val currentIndex = children.size - 1 + routing.on(route, { _ -> activeIndex = currentIndex }).resolve() + return this + } + override fun add(child: Component): StackPanel { super.add(child) if (activateLast) activeIndex = children.size - 1 diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt index 72063be0..411c4a57 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/TabPanel.kt @@ -5,15 +5,20 @@ import pl.treksoft.kvision.core.ResString import pl.treksoft.kvision.html.Link import pl.treksoft.kvision.html.TAG import pl.treksoft.kvision.html.Tag +import pl.treksoft.kvision.routing.routing open class TabPanel : SimplePanel(setOf()) { var activeIndex get() = content.activeIndex set(value) { - content.activeIndex = value - nav.children.forEach { it.removeCssClass("active") } - if (content.activeIndex >= 0 && content.activeIndex <= nav.children.size) { - nav.children[content.activeIndex].addCssClass("active") + if (content.activeIndex != value) { + content.activeIndex = value + nav.children.forEach { + it.removeCssClass("active") + } + if (content.activeIndex >= 0 && content.activeIndex <= nav.children.size) { + nav.children[content.activeIndex].addCssClass("active") + } } } @@ -27,7 +32,7 @@ open class TabPanel : SimplePanel(setOf()) { open fun addTab( title: String, panel: Component, icon: String? = null, - image: ResString? = null + image: ResString? = null, route: String? = null ): TabPanel { val tag = Tag(TAG.LI) tag.role = "presentation" @@ -37,6 +42,9 @@ open class TabPanel : SimplePanel(setOf()) { click = { e -> activeIndex = index e.preventDefault() + if (route != null) { + routing.navigate(route) + } } } nav.add(tag) @@ -45,6 +53,9 @@ open class TabPanel : SimplePanel(setOf()) { activeIndex = 0 } content.add(panel) + if (route != null) { + routing.on(route, { _ -> activeIndex = index }).resolve() + } return this } diff --git a/src/test/kotlin/test/pl/treksoft/kvision/panel/TabPanelSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/panel/TabPanelSpec.kt index eea5710e..d8c0f905 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/panel/TabPanelSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/panel/TabPanelSpec.kt @@ -63,7 +63,7 @@ class TabPanelSpec : DomSpec { tabs.removeTab(1) val element = document.getElementById("test") assertEquals( - "<div><ul class=\"nav nav-tabs\"><li role=\"presentation\" class=\"active\"><a href=\"#\">ABC</a></li></ul><div><span>abc</span></div></div>", + "<div><ul class=\"nav nav-tabs\"><li role=\"presentation\" class=\"\"><a href=\"#\">ABC</a></li></ul><div><span>abc</span></div></div>", element?.innerHTML, "Should remove tab" ) |