Skip to content

Commit 024bc7c

Browse files
committed
Refactor node selection logic in TwoDComponent to improve clarity and maintainability. Simplified event handling for node selection, ensuring proper deselection and selection of nodes in Cytoscape. Enhanced debug logging for better traceability of selected node IDs.
1 parent 1c6a15e commit 024bc7c

1 file changed

Lines changed: 20 additions & 42 deletions

File tree

src/app/visualizationComponents/TwoDComponent/twoD-plugin.component.ts

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,51 +1139,29 @@ export class TwoDComponent extends BaseComponentDirective implements OnInit, Mic
11391139

11401140
// });
11411141

1142-
// TODO move this to a subscribed event than use document jquery
11431142
$(document).on("node-selected", function () {
1144-
const mtSelectedNodes = that.commonService.getVisibleNodes().filter(n => n.selected);
1145-
const mtSelectedNodeIds = mtSelectedNodes.map(n => n._id || n.id);
1146-
1147-
// Deselect all nodes first in cytoscape
1148-
that.cy.elements().unselect();
1149-
1150-
// Select the nodes that are marked as selected in the common service
1151-
if (mtSelectedNodeIds.length > 0) {
1152-
const selector = mtSelectedNodeIds.map(id => `#${id}`).join(', ');
1153-
that.cy.nodes(selector).select();
1154-
}
1155-
1143+
if (!that.cy) return;
1144+
1145+
const mtSelectedNodes = that.commonService.getVisibleNodes().filter(n => n.selected);
1146+
const mtSelectedNodeIds = mtSelectedNodes.map(n => n._id || n.id);
1147+
1148+
// Clear cytoscape selection
1149+
that.cy.elements().unselect();
1150+
1151+
// Apply multi-selection
11561152
if (mtSelectedNodeIds.length > 0) {
1157-
that.selectedNodeId = mtSelectedNodeIds[mtSelectedNodeIds.length - 1];
1158-
} else {
1159-
that.selectedNodeId = undefined;
1160-
}
1161-
1162-
if (that.debugMode) {
1163-
console.log('node-selected in 2d: ', that.selectedNodeId);
1164-
console.log('node-selected in data: ', that.data.nodes.find(node => node.id == that.selectedNodeId));
1153+
const selector = mtSelectedNodeIds.map(id => `#${id}`).join(', ');
1154+
that.cy.nodes(selector).select();
1155+
that.selectedNodeId = mtSelectedNodeIds[mtSelectedNodeIds.length - 1]; // keep last-selected for UI logic only
1156+
} else {
1157+
that.selectedNodeId = undefined;
11651158
}
1166-
1167-
// Deselect all nodes first
1168-
that.cy.elements().unselect();
1169-
1170-
// Select the newly selected node
1171-
if (that.selectedNodeId) {
1172-
const node = that.cy.getElementById(that.selectedNodeId);
1173-
if (node) {
1174-
node.select();
1175-
}
1176-
}
1177-
1178-
console.log('node-selected in 2d: ', that.selectedNodeId);
1179-
1180-
// that.debouncedRerender();
1181-
if (that.debugMode) {
1182-
console.log('node-selected in 2d: ', that.selectedNodeId);
1183-
console.log('node-selected in data: ', that.data.nodes.find(node => node.id == that.selectedNodeId));
1184-
}
1185-
});
1186-
1159+
1160+
if (that.debugMode) {
1161+
console.log('node-selected in 2d ids: ', mtSelectedNodeIds);
1162+
}
1163+
});
1164+
11871165

11881166
if (this.commonService.session.files.length > 1) $('#link-color-variable').val('origin').change();
11891167
if (this.widgets['background-color']) $('#cy').css('background-color', this.widgets['background-color']);

0 commit comments

Comments
 (0)