From d3dd7aca39ef53ec2b28178f14a3462ccb437034 Mon Sep 17 00:00:00 2001 From: mgreminger Date: Sat, 27 Jul 2024 14:51:28 -0500 Subject: [PATCH] feat: handle user update and resetting of input/output cols for interpolation --- src/DataTableCell.svelte | 3 +++ src/cells/DataTableCell.ts | 51 +++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/DataTableCell.svelte b/src/DataTableCell.svelte index 02091258..8eee3108 100644 --- a/src/DataTableCell.svelte +++ b/src/DataTableCell.svelte @@ -318,7 +318,10 @@ } function handleInputOutputChange(defIndex: number) { + dataTableCell.setInterpolationFunctions(); + $mathCellChanged = true; + $cells[index] = $cells[index]; } function handleDeleteInterpoloationDef(defIndex: number) { diff --git a/src/cells/DataTableCell.ts b/src/cells/DataTableCell.ts index d9f4b83f..bb0efff0 100644 --- a/src/cells/DataTableCell.ts +++ b/src/cells/DataTableCell.ts @@ -112,8 +112,23 @@ export default class DataTableCell extends BaseCell { }; } + isInterpolationCol(column: number) { + const interpolationColSet = new Set(); + for(const def of this.interpolationDefinitions) { + interpolationColSet.add(def.input); + interpolationColSet.add(def.output); + } + + return interpolationColSet.has(column); + } + parseColumn(column: number) { + const isInterpolationCol = this.isInterpolationCol(column); + if (this.columnIsOutput[column]) { + if (isInterpolationCol) { + this.fixInterpolationCols(); + } return; } @@ -161,10 +176,13 @@ export default class DataTableCell extends BaseCell { this.columnErrors[column] = "Error parsing column data, check that all column number entries are either decimal numbers or integer numbers"; this.columnStatements[column] = null; } - } else { this.columnStatements[column] = null; } + + if (isInterpolationCol) { + this.setInterpolationFunctions(); + } } addRow() { @@ -267,9 +285,40 @@ export default class DataTableCell extends BaseCell { this.interpolationDefinitions = [...this.interpolationDefinitions.slice(0,index), ...this.interpolationDefinitions.slice(index+1)]; } + fixInterpolationCols() { + const dataCols = new Set(this.columnIsOutput.map((value, i) => value ? -1 : i).filter(value => value >= 0)); + if (dataCols.size < 2) { + // not enough cols to perform interpolation + this.interpolationDefinitions = []; + return; + } + + const firstValidCol = Array.from(dataCols)[0]; + + for (const def of this.interpolationDefinitions) { + if (!dataCols.has(def.input)) { + def.input = firstValidCol; + } + if (!dataCols.has(def.output)) { + def.output = firstValidCol; + } + + if (def.input === def.output) { + for (const col of dataCols) { + if (col != def.output) { + def.output = col; + break; + } + } + } + } + } + setInterpolationFunctions() { this.interpolationFunctions = []; + this.fixInterpolationCols(); + for(const def of this.interpolationDefinitions) { if (def.nameField.statement?.type !== "parameter") { console.warn('Interpolation function name parsing error');