From 48153da7cd97c57a60ca1032d19cdec0714c4c04 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 23 Jan 2019 11:00:00 +0100 Subject: Data container enhancements and fixes. --- .../pl/treksoft/kvision/data/DataContainer.kt | 53 +++++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) (limited to 'src/main/kotlin/pl/treksoft') diff --git a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt index bb3eaa83..3c808bca 100644 --- a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt +++ b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt @@ -42,22 +42,25 @@ enum class SorterType { * @constructor Creates DataContainer bound to given data model. * @param M data model type * @param C visual component type + * @param CONT container type * @param model data model of type *ObservableList* * @param factory a function which creates component C from data model at given index + * @param container internal container + * @param containerAdd function to add component C to the internal container CONT * @param filter a filtering function * @param sorter a sorting function * @param sorterType a sorting type selection function - * @param container internal container (defaults to [VPanel]) * @param init an initializer extension function */ -class DataContainer( +class DataContainer( private val model: ObservableList, private val factory: (M, Int, ObservableList) -> C, + private val container: CONT, + private val containerAdd: (CONT.(C, M) -> Unit)? = null, private val filter: ((M) -> Boolean)? = null, private val sorter: ((M) -> Comparable<*>?)? = null, private val sorterType: () -> SorterType = { SorterType.ASC }, - private val container: Container = VPanel(), - init: (DataContainer.() -> Unit)? = null + init: (DataContainer.() -> Unit)? = null ) : Widget(setOf()), Container, DataUpdatable { @@ -138,8 +141,14 @@ class DataContainer( } else { sorted } - val children = filtered.map { p -> factory(p.first, p.second, model) } - container.addAll(children) + val children = filtered.map { p -> p.first to factory(p.first, p.second, model) } + if (containerAdd != null) { + children.forEach { child -> + containerAdd.invoke(container, child.second, child.first) + } + } else { + container.addAll(children.map { it.second }) + } } onUpdateHandler?.invoke() } @@ -149,7 +158,7 @@ class DataContainer( * @param handler notification handler * @return current container */ - fun onUpdate(handler: () -> Unit): DataContainer { + fun onUpdate(handler: () -> Unit): DataContainer { onUpdateHandler = handler return this } @@ -158,7 +167,7 @@ class DataContainer( * Clears notification handler. * @return current container */ - fun clearOnUpdate(): DataContainer { + fun clearOnUpdate(): DataContainer { onUpdateHandler = null return this } @@ -169,16 +178,36 @@ class DataContainer( * * It takes the same parameters as the constructor of the built component. */ + fun Container.dataContainer( + model: ObservableList, + factory: (M, Int, ObservableList) -> C, + container: CONT, + containerAdd: (CONT.(C, M) -> Unit)? = null, + filter: ((M) -> Boolean)? = null, + sorter: ((M) -> Comparable<*>?)? = null, + sorterType: () -> SorterType = { SorterType.ASC }, + init: (DataContainer.() -> Unit)? = null + ): DataContainer { + val dataContainer = DataContainer(model, factory, container, containerAdd, filter, sorter, sorterType, init) + this.add(dataContainer) + return dataContainer + } + + /** + * DSL builder extension function with VPanel default. + * + * It takes the same parameters as the constructor of the built component. + */ fun Container.dataContainer( model: ObservableList, factory: (M, Int, ObservableList) -> C, + containerAdd: (VPanel.(C, M) -> Unit)? = null, filter: ((M) -> Boolean)? = null, sorter: ((M) -> Comparable<*>?)? = null, sorterType: () -> SorterType = { SorterType.ASC }, - container: Container = VPanel(), - init: (DataContainer.() -> Unit)? = null - ): DataContainer { - val dataContainer = DataContainer(model, factory, filter, sorter, sorterType, container, init) + init: (DataContainer.() -> Unit)? = null + ): DataContainer { + val dataContainer = DataContainer(model, factory, VPanel(), containerAdd, filter, sorter, sorterType, init) this.add(dataContainer) return dataContainer } -- cgit