diff options
6 files changed, 209 insertions, 8 deletions
diff --git a/kvision-modules/kvision-datacontainer/build.gradle b/kvision-modules/kvision-datacontainer/build.gradle index 9337698c..a10a048f 100644 --- a/kvision-modules/kvision-datacontainer/build.gradle +++ b/kvision-modules/kvision-datacontainer/build.gradle @@ -1,9 +1,5 @@ apply from: "../shared.gradle" -dependencies { - compile "pl.treksoft:kotlin-observable-js:$kotlinObservableVersion" -} - kotlinFrontend { npm { 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 6c05da4b..cb360982 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 @@ -22,11 +22,11 @@ package pl.treksoft.kvision.data import com.github.snabbdom.VNode -import com.lightningkite.kotlin.observable.list.ObservableList import pl.treksoft.kvision.core.Component import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.panel.VPanel +import pl.treksoft.kvision.utils.ObservableList /** * Sorter types. diff --git a/kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/data/DataContainerSpec.kt b/kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/data/DataContainerSpec.kt index 556d3991..36347cd3 100644 --- a/kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/data/DataContainerSpec.kt +++ b/kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/data/DataContainerSpec.kt @@ -21,12 +21,12 @@ */ package test.pl.treksoft.kvision.data -import com.lightningkite.kotlin.observable.list.observableListOf import pl.treksoft.kvision.data.BaseDataComponent import pl.treksoft.kvision.data.DataContainer import pl.treksoft.kvision.html.Label import pl.treksoft.kvision.panel.Root import pl.treksoft.kvision.panel.VPanel +import pl.treksoft.kvision.utils.observableListOf import test.pl.treksoft.kvision.DomSpec import kotlin.browser.document import kotlin.test.Test diff --git a/kvision-modules/kvision-tabulator/build.gradle b/kvision-modules/kvision-tabulator/build.gradle index b74ef2d4..6959fec5 100644 --- a/kvision-modules/kvision-tabulator/build.gradle +++ b/kvision-modules/kvision-tabulator/build.gradle @@ -1,7 +1,6 @@ apply from: "../shared.gradle" dependencies { - compile "pl.treksoft:kotlin-observable-js:$kotlinObservableVersion" compile project(":kvision-modules:kvision-redux") } diff --git a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt index bd17d7bf..f87af393 100644 --- a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt +++ b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt @@ -22,7 +22,6 @@ package pl.treksoft.kvision.tabulator import com.github.snabbdom.VNode -import com.lightningkite.kotlin.observable.list.ObservableList import org.w3c.dom.HTMLElement import pl.treksoft.kvision.KVManagerTabulator import pl.treksoft.kvision.core.Container @@ -31,6 +30,7 @@ import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.i18n.I18n import pl.treksoft.kvision.redux.ReduxStore import pl.treksoft.kvision.table.TableType +import pl.treksoft.kvision.utils.ObservableList import pl.treksoft.kvision.utils.createInstance import pl.treksoft.kvision.utils.obj import pl.treksoft.kvision.utils.syncWithList diff --git a/src/main/kotlin/pl/treksoft/kvision/utils/ObservableList.kt b/src/main/kotlin/pl/treksoft/kvision/utils/ObservableList.kt new file mode 100644 index 00000000..193cd575 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/utils/ObservableList.kt @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package pl.treksoft.kvision.utils + +/** + * Observable list interface. + */ +interface ObservableList<T> : MutableList<T> { + val onUpdate: MutableCollection<(MutableList<T>) -> Unit> +} + +/** + * Simple observable list implementation. + */ +class ObservableListWrapper<T>(val mutableList: MutableList<T> = mutableListOf()) : MutableList<T>, ObservableList<T> { + + override val onUpdate: MutableCollection<(MutableList<T>) -> Unit> = mutableListOf() + + override val size: Int + get() = mutableList.size + + override fun contains(element: T): Boolean { + return mutableList.contains(element) + } + + override fun containsAll(elements: Collection<T>): Boolean { + return mutableList.containsAll(elements) + } + + override fun get(index: Int): T { + return mutableList[index] + } + + override fun indexOf(element: T): Int { + return mutableList.indexOf(element) + } + + override fun isEmpty(): Boolean { + return mutableList.isEmpty() + } + + override fun iterator(): MutableIterator<T> = object : MutableIterator<T> { + val inner = mutableList.iterator() + override fun hasNext(): Boolean = inner.hasNext() + override fun next(): T = inner.next() + override fun remove() { + inner.remove() + onUpdate.forEach { it(this@ObservableListWrapper) } + } + } + + override fun lastIndexOf(element: T): Int { + return mutableList.lastIndexOf(element) + } + + override fun listIterator(): MutableListIterator<T> = object : MutableListIterator<T> { + val inner = mutableList.listIterator() + + override fun hasNext() = inner.hasNext() + override fun next() = inner.next() + override fun hasPrevious() = inner.hasPrevious() + override fun nextIndex() = inner.nextIndex() + override fun previous() = inner.previous() + override fun previousIndex() = inner.previousIndex() + + override fun add(element: T) { + inner.add(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun set(element: T) { + inner.set(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun remove() { + inner.remove() + onUpdate.forEach { it(this@ObservableListWrapper) } + } + } + + override fun listIterator(index: Int): MutableListIterator<T> = object : MutableListIterator<T> { + val inner = mutableList.listIterator(index) + + override fun hasNext() = inner.hasNext() + override fun next() = inner.next() + override fun hasPrevious() = inner.hasPrevious() + override fun nextIndex() = inner.nextIndex() + override fun previous() = inner.previous() + override fun previousIndex() = inner.previousIndex() + + override fun add(element: T) { + inner.add(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun set(element: T) { + inner.set(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun remove() { + inner.remove() + onUpdate.forEach { it(this@ObservableListWrapper) } + } + } + + override fun subList(fromIndex: Int, toIndex: Int): MutableList<T> { + return mutableList.subList(fromIndex, toIndex) + } + + override fun add(element: T): Boolean { + val result = mutableList.add(element) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun add(index: Int, element: T) { + mutableList.add(index, element) + onUpdate.forEach { it(this) } + } + + override fun addAll(elements: Collection<T>): Boolean { + val result = mutableList.addAll(elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun addAll(index: Int, elements: Collection<T>): Boolean { + val result = mutableList.addAll(index, elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun clear() { + mutableList.clear() + onUpdate.forEach { it(this) } + } + + override fun remove(element: T): Boolean { + val result = mutableList.remove(element) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun removeAt(index: Int): T { + val result = mutableList.removeAt(index) + onUpdate.forEach { it(this) } + return result + } + + override fun removeAll(elements: Collection<T>): Boolean { + val result = mutableList.removeAll(elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun retainAll(elements: Collection<T>): Boolean { + val result = mutableList.retainAll(elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun set(index: Int, element: T): T { + val result = mutableList.set(index, element) + onUpdate.forEach { it(this) } + return result + } +} + +/** + * Creates an instance of ObservableList<T> + */ +fun <T> observableListOf(vararg items: T) = ObservableListWrapper(items.toMutableList()) |