davidardz07 commited on
Commit
542c4a7
·
1 Parent(s): 902479b

Editar desde el diagrama

Browse files
Files changed (1) hide show
  1. script.js +70 -11
script.js CHANGED
@@ -56,10 +56,24 @@ function populateLinkForm(link) {
56
  editLinkTarget.appendChild(sourceOption);
57
  });
58
 
59
- // Set current values
60
- editLinkSource.value = sankeyData.nodes[link.source].name;
61
- editLinkTarget.value = sankeyData.nodes[link.target].name;
62
- editLinkValue.value = link.value;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  }
64
 
65
  // Event Handlers for Modal
@@ -469,15 +483,27 @@ saveEditBtn.addEventListener('click', () => {
469
  const targetNode = editLinkTarget.value;
470
  const value = parseInt(editLinkValue.value);
471
 
472
- if (sourceNode && targetNode && value > 0) {
473
- sankeyData.links[currentEditItem.index] = {
474
- source: sankeyData.nodes.findIndex(n => n.name === sourceNode),
475
- target: sankeyData.nodes.findIndex(n => n.name === targetNode),
476
- value: value
477
- };
 
 
 
 
 
 
 
 
478
  }
479
  }
480
 
 
 
 
 
481
  updateLists();
482
  initDiagram();
483
  saveData();
@@ -591,11 +617,39 @@ function initDiagram() {
591
  })
592
  .attr("stroke-width", d => Math.max(1, d.dy))
593
  .style("mix-blend-mode", "multiply")
 
594
  .on("mouseover", function() {
595
  d3.select(this).attr("stroke-opacity", 0.8);
596
  })
597
  .on("mouseout", function() {
598
  d3.select(this).attr("stroke-opacity", linkAlpha);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
  });
600
 
601
  // Filter out nodes that have no connections
@@ -623,6 +677,7 @@ function initDiagram() {
623
 
624
  const nodeEdgeColor = document.getElementById('node-edge-color').value;
625
 
 
626
  node.append("rect")
627
  .attr("height", d => Math.max(d.dy, 1)) // Ensure minimum height of 1px
628
  .attr("width", d => d.dx)
@@ -630,7 +685,11 @@ function initDiagram() {
630
  .attr("stroke", nodeEdgeColor)
631
  .attr("stroke-width", 1)
632
  .attr("rx", 3) // Rounded corners
633
- .attr("ry", 3);
 
 
 
 
634
 
635
  // Add drag behavior
636
  node.call(d3.drag()
 
56
  editLinkTarget.appendChild(sourceOption);
57
  });
58
 
59
+ // Handle different link data structures
60
+ let sourceName, targetName, linkValue;
61
+
62
+ if (link.originalSource) {
63
+ // Case: clicked from diagram
64
+ sourceName = typeof link.originalSource === 'object' ? link.originalSource.name : sankeyData.nodes[link.originalSource].name;
65
+ targetName = typeof link.originalTarget === 'object' ? link.originalTarget.name : sankeyData.nodes[link.originalTarget].name;
66
+ linkValue = link.originalValue;
67
+ } else {
68
+ // Case: clicked from list
69
+ sourceName = typeof link.source === 'object' ? link.source.name : sankeyData.nodes[link.source].name;
70
+ targetName = typeof link.target === 'object' ? link.target.name : sankeyData.nodes[link.target].name;
71
+ linkValue = link.value;
72
+ }
73
+
74
+ editLinkSource.value = sourceName;
75
+ editLinkTarget.value = targetName;
76
+ editLinkValue.value = linkValue;
77
  }
78
 
79
  // Event Handlers for Modal
 
483
  const targetNode = editLinkTarget.value;
484
  const value = parseInt(editLinkValue.value);
485
 
486
+ // Validate the index exists
487
+ if (currentEditItem.index >= 0 && currentEditItem.index < sankeyData.links.length) {
488
+ // Find the indices of source and target nodes
489
+ const sourceIndex = sankeyData.nodes.findIndex(n => n.name === sourceNode);
490
+ const targetIndex = sankeyData.nodes.findIndex(n => n.name === targetNode);
491
+
492
+ if (sourceIndex !== -1 && targetIndex !== -1 && value > 0) {
493
+ // Update the link data
494
+ sankeyData.links[currentEditItem.index] = {
495
+ source: sourceIndex,
496
+ target: targetIndex,
497
+ value: value
498
+ };
499
+ }
500
  }
501
  }
502
 
503
+ // Clear diagram cache to force complete redraw
504
+ nodePositions = {};
505
+
506
+ // Update everything
507
  updateLists();
508
  initDiagram();
509
  saveData();
 
617
  })
618
  .attr("stroke-width", d => Math.max(1, d.dy))
619
  .style("mix-blend-mode", "multiply")
620
+ .attr("cursor", "pointer")
621
  .on("mouseover", function() {
622
  d3.select(this).attr("stroke-opacity", 0.8);
623
  })
624
  .on("mouseout", function() {
625
  d3.select(this).attr("stroke-opacity", linkAlpha);
626
+ })
627
+ .on("dblclick", function(event, d) {
628
+ // Find the link index by matching source name and target name
629
+ const index = sankeyData.links.findIndex(link => {
630
+ const sourceName = typeof d.source === 'object' ? d.source.name : sankeyData.nodes[d.source].name;
631
+ const targetName = typeof d.target === 'object' ? d.target.name : sankeyData.nodes[d.target].name;
632
+
633
+ const linkSourceName = typeof link.source === 'object' ?
634
+ link.source.name : sankeyData.nodes[link.source].name;
635
+ const linkTargetName = typeof link.target === 'object' ?
636
+ link.target.name : sankeyData.nodes[link.target].name;
637
+
638
+ return sourceName === linkSourceName &&
639
+ targetName === linkTargetName &&
640
+ link.value === d.value;
641
+ });
642
+
643
+ // Store the link data for the modal
644
+ const linkData = {
645
+ source: sankeyData.links[index].source,
646
+ target: sankeyData.links[index].target,
647
+ value: sankeyData.links[index].value,
648
+ sourceName: typeof d.source === 'object' ? d.source.name : sankeyData.nodes[d.source].name,
649
+ targetName: typeof d.target === 'object' ? d.target.name : sankeyData.nodes[d.target].name
650
+ };
651
+
652
+ handleEditLink(linkData, index);
653
  });
654
 
655
  // Filter out nodes that have no connections
 
677
 
678
  const nodeEdgeColor = document.getElementById('node-edge-color').value;
679
 
680
+ // Add node rectangles with click interaction
681
  node.append("rect")
682
  .attr("height", d => Math.max(d.dy, 1)) // Ensure minimum height of 1px
683
  .attr("width", d => d.dx)
 
685
  .attr("stroke", nodeEdgeColor)
686
  .attr("stroke-width", 1)
687
  .attr("rx", 3) // Rounded corners
688
+ .attr("ry", 3)
689
+ .attr("cursor", "pointer")
690
+ .on("dblclick", function(event, d) {
691
+ handleEditNode(d, sankeyData.nodes.findIndex(node => node.name === d.name));
692
+ });
693
 
694
  // Add drag behavior
695
  node.call(d3.drag()