diff options
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/data')
3 files changed, 80 insertions, 0 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/data/DataComponent.kt b/src/main/kotlin/pl/treksoft/kvision/data/DataComponent.kt new file mode 100644 index 00000000..605e1e2e --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/data/DataComponent.kt @@ -0,0 +1,15 @@ +package pl.treksoft.kvision.data + +import kotlin.properties.ObservableProperty +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +open class DataComponent { + var container: DataUpdatable? = null + + fun <T> obs(initialValue: T): ReadWriteProperty<Any?, T> = object : ObservableProperty<T>(initialValue) { + override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) { + container?.update() + } + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt new file mode 100644 index 00000000..3b6d3aa8 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt @@ -0,0 +1,60 @@ +package pl.treksoft.kvision.data + +import com.github.snabbdom.VNode +import com.lightningkite.kotlin.observable.list.ObservableList +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.panel.VPanel + +class DataContainer<M : DataComponent, C : Widget>(val model: ObservableList<M>, + private val binding: (M, Int) -> C, + private val child: Container = VPanel()) : + Widget(setOf()), Container, DataUpdatable { + + override var visible + get() = child.visible + set(value) { + child.visible = value + } + + init { + child.parent = this + model.onUpdate += { _ -> + update() + } + update() + } + + override fun add(child: Widget): Container { + return this.child.add(child) + } + + override fun addAll(children: List<Widget>): Container { + return this.child.addAll(children) + } + + override fun remove(child: Widget): Container { + return this.child.remove(child) + } + + override fun removeAll(): Container { + return this.child.removeAll() + } + + override fun getChildren(): List<Widget> { + return this.child.getChildren() + } + + override fun renderVNode(): VNode { + return this.child.renderVNode() + } + + override fun update() { + model.forEach { it.container = this } + singleRender { + child.removeAll() + child.addAll(model.mapIndexed { index, m -> binding(m, index) }) + } + } + +} diff --git a/src/main/kotlin/pl/treksoft/kvision/data/DataUpdatable.kt b/src/main/kotlin/pl/treksoft/kvision/data/DataUpdatable.kt new file mode 100644 index 00000000..5f9b7563 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/data/DataUpdatable.kt @@ -0,0 +1,5 @@ +package pl.treksoft.kvision.data + +interface DataUpdatable { + fun update() +} |