-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltips.js
More file actions
204 lines (162 loc) · 6.95 KB
/
Copy pathtooltips.js
File metadata and controls
204 lines (162 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Overview subgraph nodes duplicate real components — point them at the real entry
const OVERVIEW_ALIASES = {
OV_TANK: 'TK_101', OV_PUMP: 'P_201', OV_PRE: 'PRE_FIL', OV_BIO: 'BIO_RX',
OV_BUF: 'MIN_RX', OV_REDOX: 'OX_RX', OV_DEG: 'DEG_COL'
};
// Mermaid node groups get id="<diagramId>-flowchart-<NODE_ID>-<N>" (prefix varies by version) — exact match, no fuzzy text guessing
function componentForSvgId(id) {
const m = String(id).match(/flowchart-(.+?)-\d+$/);
if (!m) return null;
const cid = m[1];
if (window.sidebarComponents[cid]) return window.sidebarComponents[cid];
return OVERVIEW_ALIASES[cid] ? window.sidebarComponents[OVERVIEW_ALIASES[cid]] : null;
}
function initializeTooltips() {
const tooltip = document.getElementById('component-tooltip');
const svg = document.getElementById('mySvgId');
if (!svg || !tooltip) return;
tooltip.setAttribute('role', 'tooltip');
// Escape dismisses the tooltip
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') hideTooltip(tooltip);
});
// Clear any existing event listeners
const oldElements = svg.querySelectorAll('[data-tooltip-enabled="true"]');
oldElements.forEach(el => el.removeAttribute('data-tooltip-enabled'));
// Bind by exact mermaid node group id only
svg.querySelectorAll('g[class*="node"]').forEach(g => {
const component = componentForSvgId(g.id);
if (component) addTooltip(g, component, tooltip);
});
}
function addTooltip(element, component, tooltip) {
if (element.hasAttribute('data-tooltip-enabled')) return;
element.setAttribute('data-tooltip-enabled', 'true');
element.style.cursor = 'pointer';
element.setAttribute('tabindex', '0');
const showAt = (position) => {
tooltip.innerHTML = createTooltipContent(component);
updateTooltipPosition(position, tooltip);
tooltip.style.opacity = '1';
tooltip.classList.add('visible');
};
const mouseEnter = (e) => showAt(e);
const mouseLeave = () => hideTooltip(tooltip);
const mouseMove = (e) => {
if (tooltip.classList.contains('visible')) {
updateTooltipPosition(e, tooltip);
}
};
// Keyboard: focus shows tooltip at element center, blur hides
const focusShow = () => showAt(getElementScreenCenter(element));
const focusHide = () => hideTooltip(tooltip);
element.addEventListener('mouseenter', mouseEnter);
element.addEventListener('mouseleave', mouseLeave);
element.addEventListener('mousemove', mouseMove);
element.addEventListener('focus', focusShow);
element.addEventListener('blur', focusHide);
const click = (e) => {
e.preventDefault();
e.stopPropagation();
if (window.panZoomInstance) focusOnComponent(window.panZoomInstance, component.id);
};
element.addEventListener('click', click);
// Touch support: tap to show / tap again or tap elsewhere to dismiss
const TOUCH_MOVE_THRESHOLD = 10; // pixels; below this is treated as a tap, not a drag
let touchStartX = 0;
let touchStartY = 0;
const touchStart = (e) => {
if (e.touches.length === 1) {
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
}
};
const touchEnd = (e) => {
if (e.changedTouches.length !== 1) return;
const t = e.changedTouches[0];
if (Math.abs(t.clientX - touchStartX) > TOUCH_MOVE_THRESHOLD || Math.abs(t.clientY - touchStartY) > TOUCH_MOVE_THRESHOLD) return;
e.preventDefault(); // prevent ghost click
e.stopPropagation(); // prevent global body dismiss from firing simultaneously
if (tooltip.classList.contains('visible')) {
hideTooltip(tooltip);
} else {
showAt(t);
if (window.innerWidth <= 768) tooltip.classList.add('touch-positioned');
}
};
element.addEventListener('touchstart', touchStart, { passive: true });
element.addEventListener('touchend', touchEnd);
}
function createTooltipContent(componentData) {
let html = `<div class="tooltip-title">${componentData.name}</div>`;
if (componentData.desc) {
html += `<div class="tooltip-desc">${componentData.desc}</div>`;
}
if (componentData.status) {
html += `<div class="tooltip-status ${componentData.status}">${componentData.status}</div>`;
}
if (!componentData.details) {
return html;
}
const details = componentData.details;
if (details.operatingCost?.monthly) {
html += `<div class="tooltip-cost">⚡ ${details.operatingCost.monthly}</div>`;
}
if (details.priceRange) {
html += `<div style="color: #81c784; font-size: 11px; margin-top: 4px;">💰 ${details.priceRange}</div>`;
}
if (details.specs) {
const shortSpecs = details.specs.length > 80 ? details.specs.substring(0, 80) + '...' : details.specs;
html += `<div style="color: #e0e0e0; font-size: 10px; margin-top: 4px;">📋 ${shortSpecs}</div>`;
}
if (details.preferredBrands) {
const shortBrands = details.preferredBrands.length > 60 ? details.preferredBrands.substring(0, 60) + '...' : details.preferredBrands;
html += `<div style="color: #64b5f6; font-size: 10px; margin-top: 3px;">⭐ ${shortBrands}</div>`;
}
if (details.maintenance) {
const maintenanceItems = Object.entries(details.maintenance).slice(0, 2);
if (maintenanceItems.length > 0) {
html += `<div style="color: #ffb74d; font-size: 10px; margin-top: 3px;">🔧 Maintenance:</div>`;
maintenanceItems.forEach(([freq, task]) => {
const shortTask = task.length > 40 ? task.substring(0, 40) + '...' : task;
html += `<div style="color: #ffcc02; font-size: 9px; margin-left: 8px;">• ${freq}: ${shortTask}</div>`;
});
}
}
if (details.notes && details.notes.length <= 100) {
html += `<div style="color: #f48fb1; font-size: 10px; margin-top: 4px;">💡 ${details.notes}</div>`;
}
if (details.suppliers) {
const shortSuppliers = details.suppliers.length > 50 ? details.suppliers.substring(0, 50) + '...' : details.suppliers;
html += `<div style="color: #a5d6a7; font-size: 9px; margin-top: 3px;">🛒 ${shortSuppliers}</div>`;
}
html += `<div style="color: #90caf9; font-size: 9px; margin-top: 6px; font-style: italic; border-top: 1px solid #444; padding-top: 4px;">💬 Click sidebar for full details</div>`;
return html;
}
function hideTooltip(tooltip) {
tooltip.style.opacity = '0';
tooltip.classList.remove('visible');
tooltip.classList.remove('touch-positioned');
}
function updateTooltipPosition(event, tooltip) {
const mainContainer = document.getElementById('main-container');
const containerRect = mainContainer.getBoundingClientRect();
let x = event.clientX + 15;
let y = event.clientY - 10;
const tooltipWidth = 300; // max-width from CSS
const tooltipHeight = 200; // estimated height
if (x + tooltipWidth > window.innerWidth) {
x = event.clientX - tooltipWidth - 15;
}
if (y + tooltipHeight > window.innerHeight) {
y = event.clientY - tooltipHeight - 15;
}
if (x < containerRect.left) {
x = containerRect.left + 10;
}
if (y < containerRect.top) {
y = containerRect.top + 10;
}
tooltip.style.left = x + 'px';
tooltip.style.top = y + 'px';
}