|
| 1 | +// Tachyon Profiler - Heatmap JavaScript |
| 2 | +// Interactive features for the heatmap visualization |
| 3 | + |
| 4 | +// State management |
| 5 | +let currentMenu = null; |
| 6 | + |
| 7 | +// Utility: Create element with class and content |
| 8 | +function createElement(tag, className, textContent = '') { |
| 9 | + const el = document.createElement(tag); |
| 10 | + if (className) el.className = className; |
| 11 | + if (textContent) el.textContent = textContent; |
| 12 | + return el; |
| 13 | +} |
| 14 | + |
| 15 | +// Utility: Calculate smart menu position |
| 16 | +function calculateMenuPosition(buttonRect, menuWidth, menuHeight) { |
| 17 | + const viewport = { width: window.innerWidth, height: window.innerHeight }; |
| 18 | + const scroll = { |
| 19 | + x: window.pageXOffset || document.documentElement.scrollLeft, |
| 20 | + y: window.pageYOffset || document.documentElement.scrollTop |
| 21 | + }; |
| 22 | + |
| 23 | + const left = buttonRect.right + menuWidth + 10 < viewport.width |
| 24 | + ? buttonRect.right + scroll.x + 10 |
| 25 | + : Math.max(scroll.x + 10, buttonRect.left + scroll.x - menuWidth - 10); |
| 26 | + |
| 27 | + const top = buttonRect.bottom + menuHeight + 10 < viewport.height |
| 28 | + ? buttonRect.bottom + scroll.y + 5 |
| 29 | + : Math.max(scroll.y + 10, buttonRect.top + scroll.y - menuHeight - 10); |
| 30 | + |
| 31 | + return { left, top }; |
| 32 | +} |
| 33 | + |
| 34 | +// Close and remove current menu |
| 35 | +function closeMenu() { |
| 36 | + if (currentMenu) { |
| 37 | + currentMenu.remove(); |
| 38 | + currentMenu = null; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Show navigation menu for multiple options |
| 43 | +function showNavigationMenu(button, items, title) { |
| 44 | + closeMenu(); |
| 45 | + |
| 46 | + const menu = createElement('div', 'callee-menu'); |
| 47 | + menu.appendChild(createElement('div', 'callee-menu-header', title)); |
| 48 | + |
| 49 | + items.forEach(linkData => { |
| 50 | + const item = createElement('div', 'callee-menu-item'); |
| 51 | + item.appendChild(createElement('div', 'callee-menu-func', linkData.func)); |
| 52 | + item.appendChild(createElement('div', 'callee-menu-file', linkData.file)); |
| 53 | + item.addEventListener('click', () => window.location.href = linkData.link); |
| 54 | + menu.appendChild(item); |
| 55 | + }); |
| 56 | + |
| 57 | + const pos = calculateMenuPosition(button.getBoundingClientRect(), 350, 300); |
| 58 | + menu.style.left = `${pos.left}px`; |
| 59 | + menu.style.top = `${pos.top}px`; |
| 60 | + |
| 61 | + document.body.appendChild(menu); |
| 62 | + currentMenu = menu; |
| 63 | +} |
| 64 | + |
| 65 | +// Handle navigation button clicks |
| 66 | +function handleNavigationClick(button, e) { |
| 67 | + e.stopPropagation(); |
| 68 | + |
| 69 | + const navData = button.getAttribute('data-nav'); |
| 70 | + if (navData) { |
| 71 | + window.location.href = JSON.parse(navData).link; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const navMulti = button.getAttribute('data-nav-multi'); |
| 76 | + if (navMulti) { |
| 77 | + const items = JSON.parse(navMulti); |
| 78 | + const title = button.classList.contains('caller') ? 'Choose a caller:' : 'Choose a callee:'; |
| 79 | + showNavigationMenu(button, items, title); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Initialize navigation buttons |
| 84 | +document.querySelectorAll('.nav-btn').forEach(button => { |
| 85 | + button.addEventListener('click', e => handleNavigationClick(button, e)); |
| 86 | +}); |
| 87 | + |
| 88 | +// Close menu when clicking outside |
| 89 | +document.addEventListener('click', e => { |
| 90 | + if (currentMenu && !currentMenu.contains(e.target) && !e.target.classList.contains('nav-btn')) { |
| 91 | + closeMenu(); |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +// Scroll to target line (centered using CSS scroll-margin-top) |
| 96 | +function scrollToTargetLine() { |
| 97 | + if (!window.location.hash) return; |
| 98 | + const target = document.querySelector(window.location.hash); |
| 99 | + if (target) { |
| 100 | + target.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Initialize line number permalink handlers |
| 105 | +document.querySelectorAll('.line-number').forEach(lineNum => { |
| 106 | + lineNum.style.cursor = 'pointer'; |
| 107 | + lineNum.addEventListener('click', e => { |
| 108 | + window.location.hash = `line-${e.target.textContent.trim()}`; |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +// Setup scroll-to-line behavior |
| 113 | +setTimeout(scrollToTargetLine, 100); |
| 114 | +window.addEventListener('hashchange', () => setTimeout(scrollToTargetLine, 50)); |
| 115 | + |
| 116 | +// Get sample count from line element |
| 117 | +function getSampleCount(line) { |
| 118 | + const text = line.querySelector('.line-samples')?.textContent.trim().replace(/,/g, ''); |
| 119 | + return parseInt(text) || 0; |
| 120 | +} |
| 121 | + |
| 122 | +// Classify intensity based on ratio |
| 123 | +function getIntensityClass(ratio) { |
| 124 | + if (ratio > 0.75) return 'vhot'; |
| 125 | + if (ratio > 0.5) return 'hot'; |
| 126 | + if (ratio > 0.25) return 'warm'; |
| 127 | + return 'cold'; |
| 128 | +} |
| 129 | + |
| 130 | +// Build scroll minimap showing hotspot locations |
| 131 | +function buildScrollMarker() { |
| 132 | + const existing = document.getElementById('scroll_marker'); |
| 133 | + if (existing) existing.remove(); |
| 134 | + |
| 135 | + if (document.body.scrollHeight <= window.innerHeight) return; |
| 136 | + |
| 137 | + const lines = document.querySelectorAll('.code-line'); |
| 138 | + const markerScale = window.innerHeight / document.body.scrollHeight; |
| 139 | + const lineHeight = Math.min(Math.max(3, window.innerHeight / lines.length), 10); |
| 140 | + const maxSamples = Math.max(...Array.from(lines, getSampleCount)); |
| 141 | + |
| 142 | + const scrollMarker = createElement('div', ''); |
| 143 | + scrollMarker.id = 'scroll_marker'; |
| 144 | + |
| 145 | + let prevLine = -99, lastMark, lastTop; |
| 146 | + |
| 147 | + lines.forEach((line, index) => { |
| 148 | + const samples = getSampleCount(line); |
| 149 | + if (samples === 0) return; |
| 150 | + |
| 151 | + const lineTop = Math.floor(line.offsetTop * markerScale); |
| 152 | + const lineNumber = index + 1; |
| 153 | + const intensityClass = maxSamples > 0 ? getIntensityClass(samples / maxSamples) : 'cold'; |
| 154 | + |
| 155 | + if (lineNumber === prevLine + 1 && lastMark?.classList.contains(intensityClass)) { |
| 156 | + lastMark.style.height = `${lineTop + lineHeight - lastTop}px`; |
| 157 | + } else { |
| 158 | + lastMark = createElement('div', `marker ${intensityClass}`); |
| 159 | + lastMark.style.height = `${lineHeight}px`; |
| 160 | + lastMark.style.top = `${lineTop}px`; |
| 161 | + scrollMarker.appendChild(lastMark); |
| 162 | + lastTop = lineTop; |
| 163 | + } |
| 164 | + |
| 165 | + prevLine = lineNumber; |
| 166 | + }); |
| 167 | + |
| 168 | + document.body.appendChild(scrollMarker); |
| 169 | +} |
| 170 | + |
| 171 | +// Build scroll marker on load and resize |
| 172 | +setTimeout(buildScrollMarker, 200); |
| 173 | +window.addEventListener('resize', buildScrollMarker); |
0 commit comments