|
1 | 1 | // @ts-nocheck |
2 | | -import { getTooltipPosition } from "./graphUtils.js"; |
| 2 | +import { |
| 3 | + getTooltipPosition, |
| 4 | + showResizeWarning, |
| 5 | + isWindowTooSmall, |
| 6 | + isEnoughAreaWithoutBorders, |
| 7 | +} from "./graphUtils.js"; |
3 | 8 | import { |
4 | 9 | deleteAllBorders, |
5 | 10 | drawBorderAndIconForEachExplainNode, |
6 | 11 | } from "./borderAndIconDraw.js"; |
7 | 12 |
|
8 | 13 | const tooltips = window.tooltips; |
9 | 14 | const vscode = window.acquireVsCodeApi(); |
| 15 | +const GRAPH_PADDING = 50; |
10 | 16 |
|
11 | | -// Initialize Cytoscape |
12 | | -const cy = cytoscape({ |
13 | | - container: document.getElementById("diagramContainer"), |
14 | | - elements: window.data, |
15 | | - style: [ |
16 | | - { |
17 | | - selector: "node", |
18 | | - style: { |
19 | | - padding: "5px", |
20 | | - height: "15px", |
21 | | - width: "label", |
22 | | - "background-color": "var(--vscode-list-activeSelectionBackground)", |
23 | | - color: "var(--vscode-list-activeSelectionForeground)", |
24 | | - label: "data(label)", |
25 | | - "text-valign": "center", |
26 | | - "font-size": "14px", |
| 17 | +if (isWindowTooSmall(GRAPH_PADDING)) { |
| 18 | + showResizeWarning(); |
| 19 | +} else { |
| 20 | + // Initialize Cytoscape |
| 21 | + const cy = cytoscape({ |
| 22 | + container: document.getElementById("diagramContainer"), |
| 23 | + elements: window.data, |
| 24 | + zoomingEnabled: false, |
| 25 | + userZoomingEnabled: false, |
| 26 | + style: [ |
| 27 | + { |
| 28 | + selector: "node", |
| 29 | + // style: { |
| 30 | + // padding: "5px", |
| 31 | + // height: "15px", |
| 32 | + // width: "label", |
| 33 | + // "background-color": "var(--vscode-list-activeSelectionBackground)", |
| 34 | + // color: "var(--vscode-list-activeSelectionForeground)", |
| 35 | + // label: "data(label)", |
| 36 | + // "text-valign": "center", |
| 37 | + // "font-size": "14px", |
| 38 | + // }, |
| 39 | + style: { |
| 40 | + padding: "5px", |
| 41 | + width: "150px", // fixed width |
| 42 | + shape: "roundrectangle", |
| 43 | + "background-color": "var(--vscode-list-activeSelectionBackground)", |
| 44 | + color: "var(--vscode-list-activeSelectionForeground)", |
| 45 | + label: "data(label)", |
| 46 | + "text-wrap": "wrap", // enable wrapping |
| 47 | + "text-max-width": "150px", // must match or be less than width |
| 48 | + "text-valign": "center", |
| 49 | + "text-halign": "center", |
| 50 | + "font-size": "14px", |
| 51 | + "line-height": "1.2", |
| 52 | + // "text-overflow-wrap": "anywhere", |
| 53 | + }, |
27 | 54 | }, |
28 | | - }, |
29 | | - { |
30 | | - selector: "edge", |
31 | | - style: { |
32 | | - width: 2, |
33 | | - "line-color": "#5c96bc", |
34 | | - "target-arrow-color": "#5c96bc", |
35 | | - "target-arrow-shape": "triangle", |
36 | | - "curve-style": "bezier", |
| 55 | + { |
| 56 | + selector: "edge", |
| 57 | + style: { |
| 58 | + width: 2, |
| 59 | + "line-color": "#5c96bc", |
| 60 | + "target-arrow-color": "#5c96bc", |
| 61 | + "target-arrow-shape": "triangle", |
| 62 | + "curve-style": "bezier", |
| 63 | + }, |
37 | 64 | }, |
| 65 | + ], |
| 66 | + |
| 67 | + layout: { |
| 68 | + name: "grid", |
| 69 | + fit: true, // whether to fit the viewport to the graph |
| 70 | + padding: GRAPH_PADDING, // padding used on fit |
| 71 | + avoidOverlap: true, // prevents node overlap, may overflow boundingBox if not enough space |
| 72 | + avoidOverlapPadding: 10, // extra spacing around nodes when avoidOverlap: true |
| 73 | + nodeDimensionsIncludeLabels: false, // Excludes the label when calculating node bounding boxes for the layout algorithm |
| 74 | + spacingFactor: undefined, // Applies a multiplicative factor (>0) to expand or compress the overall area that the nodes take up |
| 75 | + condense: false, // uses all available space on false, uses minimal space on true |
| 76 | + rows: undefined, // force num of rows in the grid |
| 77 | + cols: undefined, // force num of columns in the grid |
| 78 | + position: function (node) {}, // returns { row, col } for element |
| 79 | + sort: undefined, // a sorting function to order the nodes; e.g. function(a, b){ return a.data('weight') - b.data('weight') } |
| 80 | + animate: false, // whether to transition the node positions |
| 81 | + animationDuration: 500, // duration of animation in ms if enabled |
| 82 | + animationEasing: undefined, // easing of animation if enabled |
| 83 | + animateFilter: function (node, i) { |
| 84 | + return true; |
| 85 | + }, // a function that determines whether the node should be animated. All nodes animated by default on animate enabled. Non-animated nodes are positioned immediately when the layout starts |
| 86 | + ready: undefined, // callback on layoutready |
| 87 | + stop: undefined, // callback on layoutstop |
| 88 | + transform: function (node, position) { |
| 89 | + return position; |
| 90 | + }, // transform a given node position. Useful for changing flow direction in discrete layouts |
38 | 91 | }, |
39 | | - ], |
| 92 | + }); |
40 | 93 |
|
41 | | - layout: { |
42 | | - name: "grid", |
43 | | - padding: 50, // Padding around the graph |
44 | | - spacingFactor: 0.9, // Spacing between nodes |
45 | | - }, |
46 | | -}); |
| 94 | + if (!isEnoughAreaWithoutBorders(cy, GRAPH_PADDING)) { |
| 95 | + cy.destroy(); |
| 96 | + showResizeWarning(); |
| 97 | + } else { |
| 98 | + // When clicked, we display the details for the node in the bottom tree view |
| 99 | + cy.on("tap", "node", function (evt) { |
| 100 | + const id = evt.target.id(); |
| 101 | + vscode.postMessage({ |
| 102 | + command: "selected", |
| 103 | + nodeId: id, |
| 104 | + }); |
| 105 | + }); |
47 | 106 |
|
48 | | -// When clicked, we display the details for the node in the bottom tree view |
49 | | -cy.on("tap", "node", function (evt) { |
50 | | - const id = evt.target.id(); |
51 | | - vscode.postMessage({ |
52 | | - command: "selected", |
53 | | - nodeId: id, |
54 | | - }); |
55 | | -}); |
| 107 | + // === Tooltip Hover Handler === |
| 108 | + cy.nodes().on("mouseover", (event) => { |
| 109 | + const node = event.target; |
56 | 110 |
|
57 | | -// === Tooltip Hover Handler === |
58 | | -cy.nodes().on("mouseover", (event) => { |
59 | | - const node = event.target; |
| 111 | + const hoverDiv = document.createElement("pre"); |
| 112 | + const id = node.id(); |
| 113 | + const tooltip = tooltips[id]; |
| 114 | + hoverDiv.innerText = tooltip; |
| 115 | + hoverDiv.className = "hover-box"; |
| 116 | + document.body.appendChild(hoverDiv); |
60 | 117 |
|
61 | | - const hoverDiv = document.createElement("pre"); |
62 | | - const id = node.id(); |
63 | | - const tooltip = tooltips[id]; |
64 | | - hoverDiv.innerText = tooltip |
65 | | - hoverDiv.className = "hover-box"; |
66 | | - document.body.appendChild(hoverDiv); |
| 118 | + function updatePosition() { |
| 119 | + const { left, top } = getTooltipPosition( |
| 120 | + node, |
| 121 | + cy.container(), |
| 122 | + hoverDiv |
| 123 | + ); |
| 124 | + hoverDiv.style.left = `${left}px`; |
| 125 | + hoverDiv.style.top = `${top}px`; |
| 126 | + } |
67 | 127 |
|
68 | | - function updatePosition() { |
69 | | - const { left, top } = getTooltipPosition(node, cy.container(), hoverDiv); |
70 | | - hoverDiv.style.left = `${left}px`; |
71 | | - hoverDiv.style.top = `${top}px`; |
72 | | - } |
| 128 | + updatePosition(); |
| 129 | + cy.on("pan zoom resize", updatePosition); |
| 130 | + node.on("position", updatePosition); |
73 | 131 |
|
74 | | - updatePosition(); |
75 | | - cy.on("pan zoom resize", updatePosition); |
76 | | - node.on("position", updatePosition); |
| 132 | + node.once("mouseout", () => { |
| 133 | + hoverDiv.remove(); |
| 134 | + cy.off("pan zoom resize", updatePosition); |
| 135 | + node.off("position", updatePosition); |
| 136 | + }); |
| 137 | + }); |
77 | 138 |
|
78 | | - node.once("mouseout", () => { |
79 | | - hoverDiv.remove(); |
80 | | - cy.off("pan zoom resize", updatePosition); |
81 | | - node.off("position", updatePosition); |
82 | | - }); |
83 | | -}); |
| 139 | + function isVisibleBorders() { |
| 140 | + console.log(window.location.href); |
| 141 | + const border = document.querySelector(".border"); |
| 142 | + if (border !== null) { |
| 143 | + return border.style.borderColor !== "transparent"; |
| 144 | + } |
| 145 | + return false; |
| 146 | + } |
84 | 147 |
|
85 | | -// === Border sync on resize/pan/zoom === |
86 | | -function redrawBorders() { |
87 | | - deleteAllBorders(); |
88 | | - drawBorderAndIconForEachExplainNode(cy); |
89 | | -} |
| 148 | + function redrawBorders() { |
| 149 | + const showBorder = isVisibleBorders(); |
| 150 | + deleteAllBorders(); |
| 151 | + drawBorderAndIconForEachExplainNode(cy, GRAPH_PADDING, showBorder); |
| 152 | + } |
90 | 153 |
|
91 | | -cy.on("pan zoom resize", redrawBorders); |
92 | | -drawBorderAndIconForEachExplainNode(cy); |
| 154 | + cy.on("pan", redrawBorders); |
| 155 | + drawBorderAndIconForEachExplainNode(cy, GRAPH_PADDING, true); |
| 156 | + } |
| 157 | +} |
0 commit comments