From 27b0827de44b4d3f6480c2c57495863743ddd2f8 Mon Sep 17 00:00:00 2001 From: Kenneth Bruskiewicz Date: Thu, 7 Mar 2024 15:39:12 -0800 Subject: [PATCH] WIP: multiedit for a column (NOTE: loses information if the column change equals another existing value, as then the next batch operation will work on an expanded set. check the semantics with users.) --- lib/Toolbar.js | 2 +- web/index.js | 81 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/lib/Toolbar.js b/lib/Toolbar.js index 130ecab2..d036e27b 100644 --- a/lib/Toolbar.js +++ b/lib/Toolbar.js @@ -128,7 +128,7 @@ class Toolbar { dh.invalid_cells = {}; dh.runBehindLoadingScreen(dh.openFile.bind(dh), [file]); dh.current_selection = [null, null, null, null]; - }) + }); $('#file_name_display').text(file.name); } // Allow consecutive uploads of the same file diff --git a/web/index.js b/web/index.js index cf9a163d..9490651f 100644 --- a/web/index.js +++ b/web/index.js @@ -415,19 +415,15 @@ class AppContext { async setupDataHarmonizers({ template_path, schema_name, template_name, schema, exportFormats, schemaClass, columnCoordinates }) { - // TODO refactor this.appConfig = new AppConfig(template_path); this.initializeTemplate(this.appConfig.template_path); this.clearInterface(); this.clearContext(); - const _template = this.template; const [_template_name, _schema_name] = this.appConfig.template_path.split('/'); const _export_formats = exportFormats || (await this.getExportFormats(_template_name)); - // const classes = (await this.getClasses()).reduce((acc, item) => Object.assign(acc, item), {}); - // attributes are the classes which feature 1-M relationshisps // to process these classes into DataHarmonizer tables, the following must be performed: // - Navigation: one tab per class = one data harmonizer per class @@ -482,7 +478,6 @@ class AppContext { // */ - // const schema = _template.current.schema; // (await this.getSchema()) const schema_tree = buildSchemaTree(schema); this.setSchemaTree(schema_tree); data_harmonizers = makeDataHarmonizersFromSchemaTree( @@ -761,25 +756,64 @@ function transformMultivaluedColumn(data_harmonizer, shared_field, changes, sour if (hot.propToCol(shared_field.name) === -1) { console.error(`Invalid column name: ${column_name}`); } else { - if (old_value !== new_value) hot.setDataAtCell(changes[0][0], changes[0][1], new_value); - - // TODO - // Perform batch operation to replace old_value with new_value where the condition matches - // const matchCondition = row => row[column_name] === old_value; - // hot.batch(() => { - // hot.getSourceData().forEach((row, rowIndex) => { - // if (matchCondition(row)) { - // // Set new value for a property of matched rows - // hot.setDataAtRowProp(rowIndex, column_name, new_value); - // } - // }); - // }); - + if (old_value !== new_value) { + // WIP + // Perform batch operation to replace old_value with new_value where the condition matches + // const matchCondition = row => row[column_name] === old_value; + // hot.batch(() => { + // hot.getSourceData().forEach((row, rowIndex) => { + // if (matchCondition(row)) { + // // Set new value for a property of matched rows + // hot.setDataAtRowProp(rowIndex, column_name, new_value); + // } + // }); + // }); + hot.batch(() => { + hot.getData().forEach((row, rowIndex) => { + console.log(row, rowIndex) + if (row[changes[0][1]] === old_value) { + // Set new value for the cell that matches the condition + // hot.setDataAtCell(rowIndex, columnIndex, new_value); + hot.setDataAtCell(rowIndex, changes[0][1], new_value); + } + }); + }); + } + }; + // Rerender table after setting data to reflect changes hot.render(); } -}; + +// function transformMultivaluedColumn(data_harmonizer, columnName, old_value, new_value) { +// const hot = data_harmonizer.hot; +// const columnIndex = hot.propToCol(columnName); + +// // Verify if columnName is a valid property +// if (columnIndex === -1) { +// console.error(`Invalid column name: ${columnName}`); +// return; +// } + +// if (old_value !== new_value) { +// // Perform batch operation to replace old_value with new_value where the condition matches +// hot.batch(() => { +// hot.getData().forEach((row, rowIndex) => { +// console.log(row, rowIndex) +// if (row[columnIndex] === old_value) { +// // Set new value for the cell that matches the condition +// // hot.setDataAtCell(rowIndex, columnIndex, new_value); +// hot.setDataAtCell(changes[0][0], changes[0][1], new_value); +// } +// }); +// }); +// } + +// // Rerender table after setting data to reflect changes +// hot.render(); +// }; + /** * Makes a column non-editable in a Handsontable instance based on a property key. @@ -801,8 +835,7 @@ function makeColumnReadOnly(dataHarmonizer, shared_key_name) { // Check if the columnIndex is valid if (columnIndex >= 0 && columnIndex < hot.countCols()) { const currentColumns = hot.getSettings().columns ? hot.getSettings().columns.slice() : []; - console.log(currentColumns); - + // Create a new column setting or update the existing one currentColumns[columnIndex] = { ...currentColumns[columnIndex], @@ -867,7 +900,7 @@ function makeSharedKeyHandler(data_harmonizer, schema_tree_node) { const updateSchemaNodeChildrenCallback = (changes, source, old_value, new_value) => { schema_tree_node.children.forEach(cls_key => { transformMultivaluedColumn(data_harmonizers[cls_key], shared_key_spec, changes, source, old_value, new_value); - // TODO + // TODO does this need to recur to get more than ~2 depths of recursion in hierarchy? // visitSchemaTree(schema_tree, (schema_tree_node) => { // schema_tree_node.children.forEach(cls_key => { // visitSchemaTree(schema_tree, () => transformMultivaluedColumn(data_harmonizers[cls_key], shared_key_name, changes, source, old_value, new_value), cls_key) @@ -899,7 +932,7 @@ function attachPropagationEventHandlersToDataHarmonizers(data_harmonizers, schem // - If has children with shared_keys, add handler // - visit children -> lock field from being edited by user (DH methods can modify it) if (schema_tree_node.children.length > 0) { - console.log('attachPropagationEventHandlersToDataHarmonizers', schema_tree_node) + if (!schema_tree_node.tree_root) { makeSharedKeyHandler(data_harmonizers[schema_tree_node.name], schema_tree_node); schema_tree_node.children.forEach(child => {