From c5f2ad36b49b75dcaa70271e88f101e99fef5f2a Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 7 Mar 2018 13:37:25 +0100 Subject: Add mapping function for DataContainer. --- .../pl/treksoft/kvision/data/DataContainer.kt | 44 ++++++++++++---------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt index 99c8abf4..b8d13c77 100644 --- a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt +++ b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt @@ -35,30 +35,32 @@ import pl.treksoft.kvision.panel.VPanel * @param M data model type * @param C visual component type * @param model data model of type *ObservableList* - * @param binding a function which creates component C from data model at given index + * @param factory a function which creates component C from data model at given index * @param filter a filtering function - * @param child internal container (defaults to [VPanel]) + * @param mapping a mapping function + * @param container internal container (defaults to [VPanel]) * @param init an initializer extension function */ class DataContainer( private val model: ObservableList, - private val binding: (Int, M) -> C, + private val factory: (Int, M) -> C, private val filter: ((Int, M) -> Boolean)? = null, - private val child: Container = VPanel(), + private val mapping: ((List) -> List)? = null, + private val container: Container = VPanel(), init: (DataContainer.() -> Unit)? = null ) : Widget(setOf()), Container, DataUpdatable { override var visible - get() = child.visible + get() = container.visible set(value) { - child.visible = value + container.visible = value } internal var onUpdateHandler: (() -> Unit)? = null init { - child.parent = this + container.parent = this model.onUpdate += { _ -> update() } @@ -68,31 +70,31 @@ class DataContainer( } override fun add(child: Component): Container { - this.child.add(child) + this.container.add(child) return this } override fun addAll(children: List): Container { - this.child.addAll(children) + this.container.addAll(children) return this } override fun remove(child: Component): Container { - this.child.remove(child) + this.container.remove(child) return this } override fun removeAll(): Container { - this.child.removeAll() + this.container.removeAll() return this } override fun getChildren(): List { - return this.child.getChildren() + return this.container.getChildren() } override fun renderVNode(): VNode { - return this.child.renderVNode() + return this.container.renderVNode() } /** @@ -103,14 +105,15 @@ class DataContainer( if (it is DataComponent) it.container = this } singleRender { - child.removeAll() - val indexed = model.mapIndexed { index, m -> index to m } + container.removeAll() + val mapped = mapping?.invoke(model) ?: model + val indexed = mapped.mapIndexed { index, m -> index to m } val children = if (filter != null) { indexed.filter { p -> filter.invoke(p.first, p.second) } } else { indexed - }.map { p -> binding(p.first, p.second) } - child.addAll(children) + }.map { p -> factory(p.first, p.second) } + container.addAll(children) } onUpdateHandler?.invoke() } @@ -142,12 +145,13 @@ class DataContainer( */ fun Container.dataContainer( model: ObservableList, - binding: (Int, M) -> C, + factory: (Int, M) -> C, filter: ((Int, M) -> Boolean)? = null, - child: Container = VPanel(), + mapping: ((List) -> List)? = null, + container: Container = VPanel(), init: (DataContainer.() -> Unit)? = null ): DataContainer { - val dataContainer = DataContainer(model, binding, filter, child, init) + val dataContainer = DataContainer(model, factory, filter, mapping, container, init) this.add(dataContainer) return dataContainer } -- cgit