From d5ad6278fbc4184fc3d3a648e2419652a3c29e25 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 13 May 2019 15:24:50 +0200 Subject: Auto update of mutable data model after edit actions. --- .../pl/treksoft/kvision/tabulator/Options.kt | 31 +++++++++++++--------- .../pl/treksoft/kvision/tabulator/Tabulator.kt | 22 ++++++++++----- 2 files changed, 34 insertions(+), 19 deletions(-) (limited to 'kvision-modules/kvision-tabulator/src/main/kotlin') diff --git a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt index dd3bff5f..08f2603b 100644 --- a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt +++ b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Options.kt @@ -448,22 +448,18 @@ fun ColumnDefinition.toJs( this.sorter = sorter.sorter } if (sorterParams != null) this.sorterParams = sorterParams - if (tmpFormatterFunction != null) { - this.formatter = tmpFormatterFunction - } else if (formatterFunction != null) { - this.formatter = formatterFunction - } else if (formatter != null) { - this.formatter = formatter.formatter + when { + tmpFormatterFunction != null -> this.formatter = tmpFormatterFunction + formatterFunction != null -> this.formatter = formatterFunction + formatter != null -> this.formatter = formatter.formatter } if (formatterParams != null) this.formatterParams = formatterParams if (variableHeight != null) this.variableHeight = variableHeight if (editable != null) this.editable = editable - if (tmpEditorFunction != null) { - this.editor = tmpEditorFunction - } else if (editorFunction != null) { - this.editor = editorFunction - } else if (editor != null) { - this.editor = editor.editor + when { + tmpEditorFunction != null -> this.editor = tmpEditorFunction + editorFunction != null -> this.editor = editorFunction + editor != null -> this.editor = editor.editor } if (editorParams != null) this.editorParams = editorParams if (validator != null) this.validator = validator.validator @@ -690,6 +686,13 @@ fun TabulatorOptions.toJs( i18nTranslator: (String) -> (String), dataSerializer: KSerializer? = null ): Tabulator.Options { + val tmpCellEditCancelled = this.columns?.find { it.editorComponentFunction != null }?.let { + { cell: Tabulator.CellComponent -> + cellEditCancelled?.invoke(cell) + cell.getTable().redraw(true) + } + } ?: cellEditCancelled + return obj { if (height != null) this.height = height if (virtualDom != null) this.virtualDom = virtualDom @@ -817,7 +820,9 @@ fun TabulatorOptions.toJs( if (cellMouseMove != null) this.cellMouseMove = cellMouseMove if (cellEditing != null) this.cellEditing = cellEditing if (cellEdited != null) this.cellEdited = cellEdited - if (cellEditCancelled != null) this.cellEditCancelled = cellEditCancelled + if (tmpCellEditCancelled != null) { + this.cellEditCancelled = tmpCellEditCancelled + } if (columnMoved != null) this.columnMoved = columnMoved if (columnResized != null) this.columnResized = columnResized if (columnVisibilityChanged != null) this.columnVisibilityChanged = columnVisibilityChanged 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 6c7480fc..72a2809a 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 @@ -38,6 +38,7 @@ import pl.treksoft.kvision.table.TableType import pl.treksoft.kvision.utils.JSON import pl.treksoft.kvision.utils.createInstance import pl.treksoft.kvision.utils.obj +import pl.treksoft.kvision.utils.syncWithList import redux.RAction import pl.treksoft.kvision.tabulator.js.Tabulator as JsTabulator @@ -47,13 +48,16 @@ import pl.treksoft.kvision.tabulator.js.Tabulator as JsTabulator * @constructor * @param T serializable type * @param data a list of serializable objects + * @param dataUpdateOnEdit determines if the data model is automatically updated after tabulator edit action * @param options tabulator options + * @param types a set of table types * @param classes a set of CSS class names * @param dataSerializer a serializer for class T */ @Suppress("LargeClass", "TooManyFunctions") open class Tabulator( protected val data: List? = null, + protected val dataUpdateOnEdit: Boolean = true, val options: TabulatorOptions = TabulatorOptions(), types: Set = setOf(), classes: Set = setOf(), @@ -191,6 +195,9 @@ open class Tabulator( val d = nativeToData(data, dataSerializer) @Suppress("UnsafeCastFromDynamic") this.dispatchEvent("tabulatorDataEdited", obj { detail = d }) + if (dataUpdateOnEdit && this.data is MutableList) { + this.data.syncWithList(d) + } } } counter++ @@ -561,12 +568,13 @@ open class Tabulator( */ inline fun Container.tabulator( data: List? = null, + dataUpdateOnEdit: Boolean = true, options: TabulatorOptions = TabulatorOptions(), types: Set = setOf(), classes: Set = setOf(), noinline init: (Tabulator.() -> Unit)? = null ): Tabulator { - val tabulator = create(data, options, types, classes) + val tabulator = create(data, dataUpdateOnEdit, options, types, classes) init?.invoke(tabulator) this.add(tabulator) return tabulator @@ -614,7 +622,7 @@ open class Tabulator( classes: Set = setOf(), init: (Tabulator.() -> Unit)? = null ): Tabulator { - val tabulator = Tabulator(options = options, types = types, classes = classes) + val tabulator = Tabulator(dataUpdateOnEdit = false, options = options, types = types, classes = classes) init?.invoke(tabulator) this.add(tabulator) return tabulator @@ -625,12 +633,14 @@ open class Tabulator( */ @UseExperimental(ImplicitReflectionSerializer::class) inline fun create( - data: List? = null, options: TabulatorOptions = TabulatorOptions(), + data: List? = null, + dataUpdateOnEdit: Boolean = true, + options: TabulatorOptions = TabulatorOptions(), types: Set = setOf(), classes: Set = setOf(), noinline init: (Tabulator.() -> Unit)? = null ): Tabulator { - val tabulator = Tabulator(data, options, types, classes, T::class.serializer()) + val tabulator = Tabulator(data, dataUpdateOnEdit, options, types, classes, T::class.serializer()) init?.invoke(tabulator) return tabulator } @@ -648,7 +658,7 @@ open class Tabulator( noinline init: (Tabulator.() -> Unit)? = null ): Tabulator { val data = dataFactory(store.getState()) - val tabulator = Tabulator(data, options, types, classes, T::class.serializer()) + val tabulator = Tabulator(data, false, options, types, classes, T::class.serializer()) init?.invoke(tabulator) store.subscribe { s -> tabulator.replaceData(dataFactory(s)) @@ -668,7 +678,7 @@ open class Tabulator( noinline init: (Tabulator.() -> Unit)? = null ): Tabulator { val data = store.getState() - val tabulator = Tabulator(data, options, types, classes, T::class.serializer()) + val tabulator = Tabulator(data, false, options, types, classes, T::class.serializer()) init?.invoke(tabulator) store.subscribe { s -> tabulator.replaceData(s) -- cgit