From 920b4ec7a51e88769940b619c1b90b230b353ccb Mon Sep 17 00:00:00 2001 From: Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> Date: Wed, 8 May 2024 04:14:00 -0400 Subject: equation parser working in dash; need to fix auto-updating --- .../collectionSchema/CollectionSchemaView.tsx | 50 ++++++++++++++++------ src/fields/SchemaHeaderField.ts | 1 + 2 files changed, 39 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx index de38d0c56..d84dd33ff 100644 --- a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx +++ b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx @@ -273,7 +273,7 @@ export class CollectionSchemaView extends CollectionSubView() { @undoBatch changeColumnKey = (index: number, newKey: string, defaultVal?: any) => { if (!this.documentKeys.includes(newKey)) { - this.addNewKey(newKey, defaultVal, false); + this.addNewKey(newKey, defaultVal); } const currKeys = this.columnKeys.slice(); // copy the column key array first, then change it. @@ -284,7 +284,7 @@ export class CollectionSchemaView extends CollectionSubView() { @undoBatch addColumn = (key: string, defaultVal?: any) => { if (!this.documentKeys.includes(key)) { - this.addNewKey(key, defaultVal, false); + this.addNewKey(key, defaultVal); } const newColWidth = this.tableWidth / (this.storedColumnWidths.length + 1); @@ -299,11 +299,11 @@ export class CollectionSchemaView extends CollectionSubView() { }; @action - addNewKey = (key: string, defaultVal: any, isEquation: boolean) => { - if (isEquation) { + addNewKey = (key: string, defaultVal: any) => { + if (this._newFieldType == ColumnType.Equation) { this.childDocs.forEach(doc => { - const eq = this.parseEquation(key); - doc[DocData][key] = this.parsedEquationResult(eq); + const eq = this.parseEquation(defaultVal, doc); + doc[DocData][key] = this.parsedEquationResult(eq, doc); this.setupAutoUpdate(eq, doc); }); } else { @@ -316,19 +316,19 @@ export class CollectionSchemaView extends CollectionSubView() { @action setupAutoUpdate = (equation: string, doc: Doc) => { return autorun(() => { - const result = this.parsedEquationResult(equation); + const result = this.parsedEquationResult(equation, doc); doc[DocData][equation] = result; }); } - parseEquation = (eq: string): string => { + parseEquation = (eq: string, doc: Doc): string => { const variablePattern = /[a-z]+/gi; - return eq.replace(variablePattern, (match) => `doc[DocData]['${match}']`); + return eq.replace(variablePattern, (match) => `doc.${match}`); } - parsedEquationResult = (eq: string): number => { - const result = new Function('return ' + eq).call(this); - return result; + parsedEquationResult = (eq: string, doc: any): number => { + const func = new Function('doc', 'return ' + eq); + return func(doc); } @undoBatch @@ -691,6 +691,19 @@ export class CollectionSchemaView extends CollectionSubView() { })} /> ); + case ColumnType.Equation: + return ( + e.stopPropagation()} + onChange={action((e: any) => { + this._newFieldDefault = e.target.value; + })} + /> + ); default: return undefined; } @@ -714,6 +727,7 @@ export class CollectionSchemaView extends CollectionSubView() { @action setKey = (key: string, defaultVal?: any) => { + console.log("called") if (this._makeNewColumn) { this.addColumn(key, defaultVal); } else { @@ -881,6 +895,18 @@ export class CollectionSchemaView extends CollectionSubView() { /> string +