1
1
// Initialize Cytoscape
2
-
3
- // @ts -ignore
4
- const iconMap = window . iconMap ;
2
+ // main.js
3
+ import { getTooltipPosition } from "./graphUtils.js" ;
4
+ import {
5
+ deleteAllBorders ,
6
+ drawBorderAndIconForEachExplainNode ,
7
+ } from "./borderDraw.js" ;
8
+ // // @ts -ignore
9
+ // const iconMap = window.iconMap;
5
10
// @ts -ignore
6
11
const tooltips = window . tooltips ;
7
12
// @ts -ignore
@@ -56,67 +61,95 @@ cy.on("tap", "node", function (evt) {
56
61
} ) ;
57
62
} ) ;
58
63
59
- const getCodiconClass = ( label ) => {
60
- const className = iconMap [ label ] ;
61
- return className !== undefined ? `codicon-${ className } ` : "" ;
62
- } ;
63
-
64
- cy . nodeHtmlLabel ( [
65
- {
66
- query : ".l1" ,
67
- valign : "top" ,
68
- halign : "center" ,
69
- valignBox : "top" ,
70
- halignBox : "center" ,
71
- tpl : function ( data ) {
72
- const className = getCodiconClass ( data . label ) ;
73
- return `<div><div class="icon"><i class="codicon ${ className } "></i></div>` ;
74
- } ,
75
- } ,
76
- ] ) ;
77
-
64
+ // const getCodiconClass = (label) => {
65
+ // const className = iconMap[label];
66
+ // return className !== undefined ? `codicon-${className}` : "";
67
+ // };
68
+
69
+ // cy.nodeHtmlLabel([
70
+ // {
71
+ // query: ".l1",
72
+ // valign: "top",
73
+ // halign: "center",
74
+ // valignBox: "top",
75
+ // halignBox: "center",
76
+ // tpl: function (data) {
77
+ // const className = getCodiconClass(data.label);
78
+ // return `<div><div class="icon"><i class="codicon ${className}"></i></div>`;
79
+ // },
80
+ // },
81
+ // ]);
82
+
83
+ // cy.nodes().on("mouseover", (event) => {
84
+ // const node = event.target;
85
+ // const id = node.id();
86
+ // const tooltip = tooltips[id];
87
+ // const hoverDiv = document.createElement("pre");
88
+ // hoverDiv.innerText = tooltip;
89
+ // hoverDiv.className = "hover-box";
90
+ // document.body.appendChild(hoverDiv);
91
+
92
+ // function updatePosition() {
93
+ // const { x, y } = node.renderedPosition(); // center of node
94
+ // const containerRect = cy.container().getBoundingClientRect(); // Cytoscape canvas
95
+ // const boxRect = hoverDiv.getBoundingClientRect(); // Tooltip box
96
+ // const boxWidth = boxRect.width;
97
+ // const boxHeight = boxRect.height;
98
+
99
+ // const nodeTopY = y - node.renderedOuterHeight() / 2;
100
+ // const offset = 20;
101
+
102
+ // let top = nodeTopY + containerRect.top - boxHeight - offset;
103
+ // let left = x + containerRect.left - boxWidth / 2;
104
+
105
+ // // Constrain to visible area
106
+ // const viewportWidth = window.innerWidth;
107
+ // const viewportHeight = window.innerHeight;
108
+
109
+ // // Keep inside horizontal bounds
110
+ // if (left < 4) left = 4;
111
+ // if (left + boxWidth > viewportWidth - 4) {
112
+ // left = viewportWidth - boxWidth - 4;
113
+ // }
114
+
115
+ // // If tooltip would be cut off vertically, show it *below* the node instead
116
+ // if (top < 4) {
117
+ // top = y + containerRect.top + node.renderedOuterHeight() / 2;
118
+ // }
119
+
120
+ // hoverDiv.style.left = `${left}px`;
121
+ // hoverDiv.style.top = `${top}px`;
122
+ // }
123
+
124
+ // updatePosition(); // initial position
125
+ // cy.on("pan zoom resize", updatePosition);
126
+ // node.on("position", updatePosition);
127
+
128
+ // node.once("mouseout", () => {
129
+ // hoverDiv.remove();
130
+ // cy.off("pan zoom resize", updatePosition);
131
+ // node.off("position", updatePosition);
132
+ // });
133
+ // });
134
+
135
+ // === Tooltip Hover Handler ===
78
136
cy . nodes ( ) . on ( "mouseover" , ( event ) => {
79
137
const node = event . target ;
138
+
139
+ const hoverDiv = document . createElement ( "pre" ) ;
80
140
const id = node . id ( ) ;
81
141
const tooltip = tooltips [ id ] ;
82
- const hoverDiv = document . createElement ( "pre" ) ;
83
- hoverDiv . innerText = tooltip ;
142
+ hoverDiv . innerText = tooltip
84
143
hoverDiv . className = "hover-box" ;
85
144
document . body . appendChild ( hoverDiv ) ;
86
145
87
146
function updatePosition ( ) {
88
- const { x, y } = node . renderedPosition ( ) ; // center of node
89
- const containerRect = cy . container ( ) . getBoundingClientRect ( ) ; // Cytoscape canvas
90
- const boxRect = hoverDiv . getBoundingClientRect ( ) ; // Tooltip box
91
- const boxWidth = boxRect . width ;
92
- const boxHeight = boxRect . height ;
93
-
94
- const nodeTopY = y - node . renderedOuterHeight ( ) / 2 ;
95
- const offset = 20 ;
96
-
97
- let top = nodeTopY + containerRect . top - boxHeight - offset ;
98
- let left = x + containerRect . left - boxWidth / 2 ;
99
-
100
- // Constrain to visible area
101
- const viewportWidth = window . innerWidth ;
102
- const viewportHeight = window . innerHeight ;
103
-
104
- // Keep inside horizontal bounds
105
- if ( left < 4 ) left = 4 ;
106
- if ( left + boxWidth > viewportWidth - 4 ) {
107
- left = viewportWidth - boxWidth - 4 ;
108
- }
109
-
110
- // If tooltip would be cut off vertically, show it *below* the node instead
111
- if ( top < 4 ) {
112
- top = y + containerRect . top + node . renderedOuterHeight ( ) / 2 ;
113
- }
114
-
147
+ const { left, top } = getTooltipPosition ( node , cy . container ( ) , hoverDiv ) ;
115
148
hoverDiv . style . left = `${ left } px` ;
116
149
hoverDiv . style . top = `${ top } px` ;
117
150
}
118
151
119
- updatePosition ( ) ; // initial position
152
+ updatePosition ( ) ;
120
153
cy . on ( "pan zoom resize" , updatePosition ) ;
121
154
node . on ( "position" , updatePosition ) ;
122
155
@@ -126,3 +159,12 @@ cy.nodes().on("mouseover", (event) => {
126
159
node . off ( "position" , updatePosition ) ;
127
160
} ) ;
128
161
} ) ;
162
+
163
+ // === Border sync on resize/pan/zoom ===
164
+ function redrawBorders ( ) {
165
+ deleteAllBorders ( ) ;
166
+ drawBorderAndIconForEachExplainNode ( cy ) ;
167
+ }
168
+
169
+ cy . on ( "pan zoom resize" , redrawBorders ) ;
170
+ drawBorderAndIconForEachExplainNode ( cy ) ;
0 commit comments