Skip to content

Commit

Permalink
add attach all node context option
Browse files Browse the repository at this point in the history
  • Loading branch information
MFA-X-AI committed Jul 15, 2024
1 parent f037be6 commit fbf4e5d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/commands/CommandIDs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const commandIDs = {
copyNode: "Xircuit-editor:copy-node",
pasteNode: "Xircuit-editor:paste-node",
attachNode: "Xircuit-editor:attach-node",
detachNode: "Xircuit-editor:detach-node",
attachAllNodes: "Xircuit-editor:attach-all-nodes",
detachAllNodes: "Xircuit-editor:detach-all-nodes",
triggerLoadingAnimation: "Xircuit-editor:trigger-loading-animation",
reloadNode: "Xircuit-editor:reload-node",
reloadAllNodes: "Xircuit-editor:reload-all-nodes",
Expand Down
41 changes: 37 additions & 4 deletions src/commands/NodeActionCommands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,43 @@ export function addNodeActionCommands(
},
label: trans.__('attach node')
});


// Add command to detach parameter node
commands.addCommand(commandIDs.detachNode, {
// Add command to attach all parameter nodes
commands.addCommand(commandIDs.attachAllNodes, {
execute: async () => {

const widget = tracker.currentWidget?.content as XircuitsPanel;
const model = widget.xircuitsApp.getDiagramEngine().getModel();
const selected_entities = model.getSelectedEntities();

const literal_nodes = [];
const selected_nodes = selected_entities.filter(entity => entity instanceof NodeModel) as CustomNodeModel[];
selected_nodes.forEach(node => {
node.getOptions().selected = false;
let inPorts = node.getInPorts();
Object.values(inPorts).forEach((port: CustomPortModel) => {
let sourceNode = port.getSourceNodes()[0] as CustomNodeModel;
if (sourceNode && sourceNode['name'].startsWith('Literal ') && !sourceNode['extras']['attached']) {
sourceNode.getOptions().extras.attached = true;
sourceNode.getOptions().selected = true;
literal_nodes.push(sourceNode);
}
})
});

literal_nodes.forEach(node => {
let parameterOutPort = node.getOutPorts()[0] as CustomPortModel;
let connectedNodes = parameterOutPort.getTargetNodes();
connectedNodes.forEach((node: CustomNodeModel) => node.getOptions().selected = true)
});

widget.xircuitsApp.getDiagramEngine().repaintCanvas();
},
label: trans.__('attach all nodes')
});

// Add command to detach all parameter nodes
commands.addCommand(commandIDs.detachAllNodes, {
execute: async () => {

const widget = tracker.currentWidget?.content as XircuitsPanel;
Expand Down Expand Up @@ -963,7 +996,7 @@ export function addNodeActionCommands(

widget.xircuitsApp.getDiagramEngine().repaintCanvas();
},
label: trans.__('detach node')
label: trans.__('detach all nodes')
});

}
32 changes: 25 additions & 7 deletions src/context-menu/CanvasContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ export class CanvasContextMenu extends React.Component<CanvasContextMenuProps> {
await this.props.app.commands.execute(commandIDs.reloadNode);
};

const handleDetachNode = async () => {
this.props.app.commands.execute(commandIDs.detachNode);
const handleAllAttachNodes = async () => {
this.props.app.commands.execute(commandIDs.attachAllNodes);
await this.props.app.commands.execute(commandIDs.reloadNode);
};

const handleDetachAllNodes = async () => {
this.props.app.commands.execute(commandIDs.detachAllNodes);
await this.props.app.commands.execute(commandIDs.reloadNode);
};

Expand All @@ -53,8 +58,11 @@ export class CanvasContextMenu extends React.Component<CanvasContextMenuProps> {
{visibility.showAttachNode && (
<div className="context-menu-option" onClick={handleAttachNode}>Attach</div>
)}
{visibility.showDetachNode && (
<div className="context-menu-option" onClick={handleDetachNode}>Detach</div>
{visibility.showAttachAllNodes && (
<div className="context-menu-option" onClick={handleAllAttachNodes}>Attach Literals</div>
)}
{visibility.showDetachAllNodes && (
<div className="context-menu-option" onClick={handleDetachAllNodes}>Detach Literals</div>
)}
{visibility.showReloadNode && (
<div className="context-menu-option" onClick={handleReloadNode}>Reload Node</div>
Expand Down Expand Up @@ -99,7 +107,15 @@ export function getMenuOptionsVisibility(models) {
return !isLiteralNode(node) && !isArgumentNode(node);
}

function canDetachNode(node) {
function canAttachAllNodes(node) {
let ports = node.getInPorts();
return ports.some((port) => {
let sourceNode = port.getSourceNodes()[0];
return sourceNode?.getOptions()?.extras?.attached === false;
});
}

function canDetachAllNodes(node) {
let ports = node.getInPorts();
return ports.some((port) => {
let sourceNode = port.getSourceNodes()[0];
Expand All @@ -120,7 +136,8 @@ export function getMenuOptionsVisibility(models) {
let showReloadNode = isNodeSelected && componentNodes.length > 0;
let showopenXircuitsWorkflow = isSingleComponentNodeSelected && models.some(model => isXircuitsWorkflow(model));
let showAttachNode = isNodeSelected && parameterNodes.length > 0;
let showDetachNode = componentNodes.some(model => canDetachNode(model));
let showAttachAllNodes = componentNodes.some(model => canAttachAllNodes(model));
let showDetachAllNodes = componentNodes.some(model => canDetachAllNodes(model));

return {
showCutCopyPaste: !models.length || isNodeSelected || isLinkSelected,
Expand All @@ -132,7 +149,8 @@ export function getMenuOptionsVisibility(models) {
showUndoRedo: !models.length,
showAddComment: !models.length,
showAttachNode: showAttachNode,
showDetachNode: showDetachNode
showAttachAllNodes: showAttachAllNodes,
showDetachAllNodes: showDetachAllNodes
};
}

Expand Down

0 comments on commit fbf4e5d

Please sign in to comment.