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, classes: Set = setOf() ) : SimplePanel(classes) { var activeIndex = -1 set(value) { field = value refresh() } override fun childrenVNodes(): Array { return if (activeIndex >= 0 && activeIndex < children.size) { arrayOf(children[activeIndex].renderVNode()) } else { arrayOf() } } 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 else if (activeIndex == -1) activeIndex = 0 return this } override fun addAll(children: List): StackPanel { super.addAll(children) if (activateLast) activeIndex = this.children.size - 1 else if (activeIndex == -1) activeIndex = 0 return this } override fun remove(child: Component): StackPanel { super.remove(child) if (activeIndex > children.size - 1) activeIndex = children.size - 1 return this } override fun removeAll(): StackPanel { super.removeAll() if (activeIndex > children.size - 1) activeIndex = children.size - 1 return this } }