Skip to content

Commit 4841992

Browse files
one-way propagation of data flow WIP (no multivalued data modification yet
1 parent 755bc28 commit 4841992

File tree

1 file changed

+73
-69
lines changed

1 file changed

+73
-69
lines changed

web/index.js

+73-69
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class AppContext {
9292

9393
}
9494

95-
// TODO
95+
9696
async propagateChangeToChildren(node_label, func = id => id) {
9797
const dependency_tree = (await this.getDependencyTree());
9898
let current_node_label = node_label;
@@ -368,14 +368,17 @@ const main = async function () {
368368
`);
369369

370370
let dhs = [];
371+
let data_harmonizers = {};
371372

372373
const context = new AppContext(new AppConfig(await getTemplatePath()));
373374

374375

375376
context.initializeTemplate(context.appConfig.template_path)
376377
.then(async (context) => {
377378
const _template = context.template;
378-
const _export_formats = (await context.getExportFormats('grdi'));
379+
const _schema = _template.current.schema;
380+
const [_template_name, _schema_name] = context.appConfig.template_path.split('/');
381+
const _export_formats = (await context.getExportFormats(_template_name));
379382

380383
// // internationalize
381384
// // TODO: connect to locale of browser!
@@ -570,7 +573,6 @@ const main = async function () {
570573
};
571574

572575
const shared_keys_per_class = findSharedKeys(schema);
573-
574576
const pre_schema_tree = classes.reduce((acc, class_key) => {
575577
acc[class_key] = {
576578
name: class_key,
@@ -590,7 +592,6 @@ const main = async function () {
590592
* @param {Function} callback - The function to perform on each class node.
591593
*/
592594
function visitSchemaTree(schema_tree, callback=tap, next='Container') {
593-
console.log('visitSchemaTree', schema_tree)
594595
if (!schema_tree[next]) return; // Base case: If the class key is not found
595596
callback(schema_tree[next]); // Perform the callback on the current node
596597

@@ -639,11 +640,12 @@ const main = async function () {
639640

640641
let data_harmonizers = {};
641642
if (!!schema_tree) {
642-
Object.entries(schema_tree).forEach((obj, index) => {
643+
Object.entries(schema_tree).filter(([cls_key,]) => cls_key !== 'Container').forEach((obj, index) => {
643644
if (obj.length > 0) {
644645
const [cls_key, spec] = obj;
645646
const dhId = `data-harmonizer-grid-${index}`;
646647
let dhSubroot = createDataHarmonizerContainer(dhId, index === 0);
648+
647649
dhRoot.appendChild(dhSubroot); // Appending to the parent container
648650

649651
const dhTab = createDataHarmonizerTab(dhId, spec.name, index === 0);
@@ -656,9 +658,8 @@ const main = async function () {
656658
loadingScreenRoot: document.body,
657659
field_filters: findSlotNamesForClass(schema, cls_key) // TODO: Find slot names for filtering
658660
});
659-
// initialize the data harmonizer
660-
// TODO
661-
data_harmonizers[spec.name].useSchema(schema, _export_formats, 'GRDI_Sample');
661+
662+
data_harmonizers[spec.name].useSchema(_schema, _export_formats, _schema_name);
662663

663664
}
664665
});
@@ -674,28 +675,32 @@ const main = async function () {
674675
* @param {*} old_value - The original value to replace.
675676
* @param {*} new_value - The new value to replace the original with.
676677
*/
677-
function transformMultivaluedColumn(data_harmonizer, column_name, old_value, new_value) {
678+
function transformMultivaluedColumn(data_harmonizer, shared_field, changes, source, old_value, new_value) {
678679
const hot = data_harmonizer.hot;
680+
681+
console.log('transform multivalued column execute', shared_field, changes, source, old_value, new_value);
682+
console.log(hot.propToCol(shared_field.name))
679683
// Verify if column_name is a valid property
680-
if (hot.propToCol(column_name) === -1) {
684+
if (hot.propToCol(shared_field.name) === -1) {
681685
console.error(`Invalid column name: ${column_name}`);
682-
return;
686+
} else {
687+
if (old_value !== new_value) hot.setDataAtCell(changes[0][0], changes[0][1], new_value);
688+
689+
// TODO
690+
// Perform batch operation to replace old_value with new_value where the condition matches
691+
// const matchCondition = row => row[column_name] === old_value;
692+
// hot.batch(() => {
693+
// hot.getSourceData().forEach((row, rowIndex) => {
694+
// if (matchCondition(row)) {
695+
// // Set new value for a property of matched rows
696+
// hot.setDataAtRowProp(rowIndex, column_name, new_value);
697+
// }
698+
// });
699+
// });
700+
701+
// Rerender table after setting data to reflect changes
702+
hot.render();
683703
}
684-
685-
const matchCondition = row => row[column_name] === old_value;
686-
687-
// Perform batch operation to replace old_value with new_value where the condition matches
688-
hot.batch(() => {
689-
hot.getSourceData().forEach((row, rowIndex) => {
690-
if (matchCondition(row)) {
691-
// Set new value for a property of matched rows
692-
hot.setDataAtRowProp(rowIndex, column_name, new_value);
693-
}
694-
});
695-
});
696-
697-
// Rerender table after setting data to reflect changes
698-
hot.render();
699704

700705
};
701706

@@ -738,33 +743,31 @@ const main = async function () {
738743
*/
739744
function attachColumnEditHandler(data_harmonizer, shared_key_name, callback) {
740745
const hot = data_harmonizer.hot;
741-
console.log('attaching column edit handler to ', hot);
742-
743746
// Get the index of the column based on the shared_key_name
744747
const columnIndex = hot.propToCol(shared_key_name);
745-
748+
746749
// Check if the column index was found properly
747750
if (columnIndex === -1) {
748751
console.error(`Column with the name '${shared_key_name}' not found.`);
749-
return;
750-
}
751-
752-
// Listen for changes using the afterChange hook of Handsontable
753-
hot.addHook('afterChange', (changes, source) => {
754-
// changes is a 2D array containing information about each change
755-
// Each change is of the form [row, prop, oldVal, newVal]
756-
if (changes && source !== 'loadData') { // Ignore initial load changes
757-
changes.forEach(([row, prop, oldVal, newVal]) => {
758-
if (hot.propToCol(prop) === columnIndex) {
759-
console.log(`Column '${shared_key_name}' changed at row ${row}`);
760-
// You can put here the code that should be executed when
761-
// the specific column has been edited.
762-
// This could be a function call, an event dispatch, etc.
763-
callback(changes, source, oldVal, newVal);
764-
}
752+
} else {
753+
// Listen for changes using the afterChange hook of Handsontable
754+
hot.addHook('afterChange', (changes, source) => {
755+
console.log('afterchange hook execute', changes, source)
756+
// changes is a 2D array containing information about each change
757+
// Each change is of the form [row, prop, oldVal, newVal]
758+
if (changes && source !== 'loadData') { // Ignore initial load changes
759+
changes.forEach(([row, prop, oldVal, newVal]) => {
760+
console.log(prop, hot.propToCol(prop), columnIndex, shared_key_name, oldVal, newVal);
761+
console.log(`Column '${shared_key_name}' changed at row ${row}`);
762+
// You can put here the code that should be executed when
763+
// the specific column has been edited.
764+
// This could be a function call, an event dispatch, etc.
765+
callback(changes, source, oldVal, newVal);
766+
});
767+
}
765768
});
766-
}
767-
});
769+
}
770+
768771
}
769772

770773
/**
@@ -773,21 +776,24 @@ const main = async function () {
773776
* @param {Object} schema_tree_node - The schema tree node containing the shared keys and child references.
774777
*/
775778
function makeSharedKeyHandler(data_harmonizer, schema_tree_node) {
776-
console.log('makeSharedKeyHandler', data_harmonizer);
777779
if (schema_tree_node.shared_keys.length > 0) {
778780
schema_tree_node.shared_keys.forEach((shared_key_name) => {
779-
780781
const updateSchemaNodeChildrenCallback = (changes, source, old_value, new_value) => {
781-
schema_tree_node.children.forEach(cls_key => {
782-
transformMultivaluedColumn(data_harmonizer, shared_key_name, old_value, new_value);
783-
visitSchemaTree(schema_tree, (schema_tree_node) => {
784-
schema_tree_node.children.forEach(cls_key => {
785-
visitSchemaTree(schema_tree, () => transformMultivaluedColumn(data_harmonizer, shared_key_name, old_value, new_value) , next=cls_key)
786-
})
787-
}, next=cls_key)
788-
})
782+
if (schema_tree_node.children.length > 0) {
783+
schema_tree_node.children.forEach(cls_key => {
784+
transformMultivaluedColumn(data_harmonizers[cls_key], shared_key_name, changes, source, old_value, new_value);
785+
// TODO
786+
// visitSchemaTree(schema_tree, (schema_tree_node) => {
787+
// schema_tree_node.children.forEach(cls_key => {
788+
// visitSchemaTree(schema_tree, () => transformMultivaluedColumn(data_harmonizers[cls_key], shared_key_name, changes, source, old_value, new_value), cls_key)
789+
// })
790+
// }, cls_key);
791+
});
792+
793+
} else {
794+
console.log('no more iteration for', schema_tree_node.name);
795+
}
789796
};
790-
791797
attachColumnEditHandler(data_harmonizer, shared_key_name, updateSchemaNodeChildrenCallback);
792798

793799
});
@@ -804,8 +810,6 @@ const main = async function () {
804810
* @returns {Object} The same object with event handlers attached.
805811
*/
806812
function attachPropagationEventHandlersToDataHarmonizers(data_harmonizers, schema_tree) {
807-
console.log('data_harmonizers', data_harmonizers);
808-
console.log('schema_tree', schema_tree)
809813

810814
visitSchemaTree(schema_tree, (schema_tree_node) => {
811815
// Propagation:
@@ -824,14 +828,14 @@ const main = async function () {
824828
function initializeDataHarmonizers(data_harmonizers) {
825829
console.log('data_harmonizers', data_harmonizers);
826830
Object.entries(data_harmonizers).forEach(([cls_key,], index) => {
827-
new Toolbar(dhToolbarRoot, data_harmonizers[cls_key], menu, {
828-
context: context,
829-
templatePath: context.appConfig.template_path, // TODO: a default should be loaded before Toolbar is constructed! then take out all loading in "toolbar" to an outside context
830-
releasesURL: 'https: // gi thub.com/cidgoh/pathogen-genomics-package/releases',
831-
getLanguages: context.getLocaleData.bind(context),
832-
getSchema: async (schema) => Template.create(schema).then(result => result.current.schema),
833-
getExportFormats: context.getExportFormats.bind(context),
834-
});
831+
// new Toolbar(dhToolbarRoot, data_harmonizers[cls_key], menu, {
832+
// context: context,
833+
// templatePath: context.appConfig.template_path, // TODO: a default should be loaded before Toolbar is constructed! then take out all loading in "toolbar" to an outside context
834+
// releasesURL: 'https: // gi thub.com/cidgoh/pathogen-genomics-package/releases',
835+
// getLanguages: context.getLocaleData.bind(context),
836+
// getSchema: async (schema) => Template.create(schema).then(result => result.current.schema),
837+
// getExportFormats: context.getExportFormats.bind(context),
838+
// });
835839
});
836840
console.log('before attachPropagationEventHandlersToDataHarmonizers');
837841
attachPropagationEventHandlersToDataHarmonizers(data_harmonizers, schema_tree);
@@ -842,7 +846,7 @@ const main = async function () {
842846
const schema_tree = buildSchemaTree((await context.getSchema()));
843847
console.log('schema_tree', schema_tree);
844848
console.log('before makeDataHarmonizersFromSchemaTree');
845-
let data_harmonizers = makeDataHarmonizersFromSchemaTree(
849+
data_harmonizers = makeDataHarmonizersFromSchemaTree(
846850
(await context.getSchema()),
847851
schema_tree);
848852
// HACK

0 commit comments

Comments
 (0)