diff options
author | Robert Jaros <rjaros@finn.pl> | 2020-01-19 22:49:15 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2020-01-19 22:49:15 +0100 |
commit | 5655c24a123e6b9557766186790cd4f7e6bfe011 (patch) | |
tree | 024e6b9aa79f699167bcce97d6d8f8fe50cb52c9 /kvision-modules/kvision-datacontainer/src | |
parent | ff4fbeda386505f5ebb0575a92fe0ef4398e4b18 (diff) | |
download | kvision-5655c24a123e6b9557766186790cd4f7e6bfe011.tar.gz kvision-5655c24a123e6b9557766186790cd4f7e6bfe011.tar.bz2 kvision-5655c24a123e6b9557766186790cd4f7e6bfe011.zip |
Add an "optional" receiver for factory function in DataContainer component (#128)
Diffstat (limited to 'kvision-modules/kvision-datacontainer/src')
-rw-r--r-- | kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt b/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt index 84b30afa..6acd72ad 100644 --- a/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt +++ b/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt @@ -54,7 +54,7 @@ enum class SorterType { */ class DataContainer<M, C : Component, CONT : Container>( private val model: MutableList<M>, - private val factory: (M, Int, MutableList<M>) -> C, + private val factory: Container.(M, Int, MutableList<M>) -> C, private val container: CONT, private val containerAdd: (CONT.(C, M) -> Unit)? = null, private val filter: ((M) -> Boolean)? = null, @@ -144,13 +144,15 @@ class DataContainer<M, C : Component, CONT : Container>( } else { sorted } - 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) + val children = filtered.map { p -> p.first to container.factory(p.first, p.second, model) } + if (container.getChildren().isEmpty() && children.isNotEmpty()) { + if (containerAdd != null) { + children.forEach { child -> + containerAdd.invoke(container, child.second, child.first) + } + } else { + container.addAll(children.map { it.second }) } - } else { - container.addAll(children.map { it.second }) } } onUpdateHandler?.invoke() @@ -183,7 +185,7 @@ class DataContainer<M, C : Component, CONT : Container>( */ fun <M, C : Component, CONT : Container> Container.dataContainer( model: MutableList<M>, - factory: (M, Int, MutableList<M>) -> C, + factory: Container.(M, Int, MutableList<M>) -> C, container: CONT, containerAdd: (CONT.(C, M) -> Unit)? = null, filter: ((M) -> Boolean)? = null, @@ -203,7 +205,7 @@ fun <M, C : Component, CONT : Container> Container.dataContainer( */ fun <M, C : Component> Container.dataContainer( model: MutableList<M>, - factory: (M, Int, MutableList<M>) -> C, + factory: Container.(M, Int, MutableList<M>) -> C, containerAdd: (VPanel.(C, M) -> Unit)? = null, filter: ((M) -> Boolean)? = null, sorter: ((M) -> Comparable<*>?)? = null, |