| 1 |
/* |
| 2 |
This file is inspired by coverage.py's js code. |
| 3 |
*/ |
| 4 |
|
| 5 |
gcovr = {}; |
| 6 |
|
| 7 |
gcovr.fileLoaded = function () { |
| 8 |
gcovr.addOnClickHandler(".button_toggle_coveredLine", gcovr.toggleLines); |
| 9 |
gcovr.addOnClickHandler(".button_toggle_uncoveredLine", gcovr.toggleLines); |
| 10 |
gcovr.addOnClickHandler(".button_toggle_partialCoveredLine", gcovr.toggleLines); |
| 11 |
gcovr.addOnClickHandler(".button_toggle_excludedLine", gcovr.toggleLines); |
| 12 |
|
| 13 |
gcovr.addOnClickHandler("div.sortable", gcovr.sortGridTable); |
| 14 |
|
| 15 |
gcovr.source_table_body = document.querySelector(".source-table-container > table > tbody") |
| 16 |
gcovr.initScrollMarkers(); |
| 17 |
window.addEventListener("resize", () => { |
| 18 |
gcovr.initScrollMarkers(); |
| 19 |
}); |
| 20 |
}; |
| 21 |
|
| 22 |
gcovr.addOnClickHandler = function (selector, handler) { |
| 23 |
document.querySelectorAll(selector).forEach(elt => { |
| 24 |
elt.style.cursor = "pointer"; |
| 25 |
elt.addEventListener("click", handler) |
| 26 |
}); |
| 27 |
}; |
| 28 |
|
| 29 |
|
| 30 |
gcovr.toggleLines = function (event) { |
| 31 |
const btn = event.target.closest("button"); |
| 32 |
const category = btn.value |
| 33 |
const show = !btn.classList.contains("show_" + category); |
| 34 |
gcovr.setLineVisibility(btn, category, show); |
| 35 |
gcovr.buildScrollMarkers(); |
| 36 |
}; |
| 37 |
|
| 38 |
gcovr.setLineVisibility = function (btn, category, should_show) { |
| 39 |
const cls = "show_" + category; |
| 40 |
if (should_show) { |
| 41 |
btn.closest("main").querySelectorAll("td." + category).forEach(e => e.classList.add(cls)); |
| 42 |
btn.classList.add(cls); |
| 43 |
} |
| 44 |
else { |
| 45 |
btn.closest("main").querySelectorAll("td." + category).forEach(e => e.classList.remove(cls)); |
| 46 |
btn.classList.remove(cls); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
gcovr.initScrollMarkers = function () { |
| 51 |
if (gcovr.source_table_body === null) { |
| 52 |
const temp_scroll_marker = document.getElementById("scroll_marker") |
| 53 |
if (temp_scroll_marker) { |
| 54 |
temp_scroll_marker.remove(); |
| 55 |
} |
| 56 |
} |
| 57 |
else { |
| 58 |
gcovr.lines_len = gcovr.source_table_body.querySelectorAll("tr").length - 1; // exclude header |
| 59 |
gcovr.buildScrollMarkers(); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
gcovr.buildScrollMarkers = function () { |
| 64 |
const temp_scroll_marker = document.getElementById("scroll_marker") |
| 65 |
if (temp_scroll_marker) temp_scroll_marker.remove(); |
| 66 |
// Don't build markers if the window has no scroll bar. |
| 67 |
if (document.body.scrollHeight <= window.innerHeight) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
const marker_scale = window.innerHeight / document.body.scrollHeight; |
| 72 |
const line_height = Math.max(3, window.innerHeight / gcovr.lines_len); |
| 73 |
const offset_table_start = gcovr.source_table_body.querySelector("td").getBoundingClientRect().top; |
| 74 |
|
| 75 |
let previous_line = -99, last_mark, last_top; |
| 76 |
|
| 77 |
const scroll_marker = document.createElement("div"); |
| 78 |
scroll_marker.id = "scroll_marker"; |
| 79 |
gcovr.source_table_body.querySelectorAll( |
| 80 |
"tr:has(td.show_coveredLine), tr:has(td.show_uncoveredLine), tr:has(td.show_excludedLine), tr:has(td.show_partialCoveredLine)" |
| 81 |
).forEach(element => { |
| 82 |
const line_top = Math.floor((offset_table_start + element.offsetTop) * marker_scale); |
| 83 |
const line_number = parseInt(element.querySelector("td > a").textContent); |
| 84 |
|
| 85 |
if (line_number === previous_line + 1) { |
| 86 |
// If this solid missed block just make previous mark higher. |
| 87 |
last_mark.style.height = `${line_top + line_height - last_top}px`; |
| 88 |
} |
| 89 |
else { |
| 90 |
// Add colored line in scroll_marker block. |
| 91 |
last_mark = document.createElement("div"); |
| 92 |
last_mark.id = `m${line_number}`; |
| 93 |
last_mark.classList.add("marker"); |
| 94 |
last_mark.style.height = `${line_height}px`; |
| 95 |
last_mark.style.top = `${line_top}px`; |
| 96 |
scroll_marker.append(last_mark); |
| 97 |
last_top = line_top; |
| 98 |
} |
| 99 |
|
| 100 |
previous_line = line_number; |
| 101 |
}); |
| 102 |
|
| 103 |
// Append last to prevent layout calculation |
| 104 |
document.body.append(scroll_marker); |
| 105 |
}; |
| 106 |
|
| 107 |
gcovr.sortGridTable = function (event) { |
| 108 |
const rowHeaderColumn = event.target.closest('div.sortable'); |
| 109 |
const table = rowHeaderColumn.closest('.Box'); |
| 110 |
const rows = Array.from(table.querySelectorAll('.Box-row')); |
| 111 |
|
| 112 |
const columnIndex = Array.from(rowHeaderColumn.parentNode.children).indexOf(rowHeaderColumn); |
| 113 |
const isAscending = rowHeaderColumn.classList.contains('sorted-ascending'); |
| 114 |
|
| 115 |
rows.sort((a, b) => { |
| 116 |
const cellA = |
| 117 |
a.children[columnIndex].hasAttribute('data-sort') |
| 118 |
? a.children[columnIndex].getAttribute('data-sort') |
| 119 |
: a.children[columnIndex].textContent.trim().toLowerCase(); |
| 120 |
const cellB = |
| 121 |
b.children[columnIndex].hasAttribute('data-sort') |
| 122 |
? b.children[columnIndex].getAttribute('data-sort') |
| 123 |
: b.children[columnIndex].textContent.trim().toLowerCase(); |
| 124 |
|
| 125 |
let comparison = 0; |
| 126 |
if (!isNaN(parseFloat(cellA)) && !isNaN(parseFloat(cellB))) { |
| 127 |
comparison = parseFloat(cellA) - parseFloat(cellB); |
| 128 |
} else { |
| 129 |
comparison = cellA.localeCompare(cellB); |
| 130 |
} |
| 131 |
|
| 132 |
if (comparison == 0) { |
| 133 |
// Fallback to first column (ascending) for stable sorting |
| 134 |
const firstCellA = a.children[0].textContent.trim().toLowerCase(); |
| 135 |
const firstCellB = b.children[0].textContent.trim().toLowerCase(); |
| 136 |
return firstCellA.localeCompare(firstCellB); |
| 137 |
} |
| 138 |
|
| 139 |
return isAscending ? -comparison : comparison; |
| 140 |
}); |
| 141 |
|
| 142 |
|
| 143 |
// Remove existing rows |
| 144 |
rows.forEach(row => table.removeChild(row)); |
| 145 |
|
| 146 |
// Append sorted rows |
| 147 |
rows.forEach(row => table.appendChild(row)); |
| 148 |
|
| 149 |
// Update header classes |
| 150 |
Array.from(rowHeaderColumn.parentNode.children).forEach(th => { |
| 151 |
th.classList.remove('sorted-ascending', 'sorted-descending'); |
| 152 |
}); |
| 153 |
rowHeaderColumn.classList.add(isAscending ? 'sorted-descending' : 'sorted-ascending'); |
| 154 |
} |
| 155 |
|
| 156 |
document.addEventListener("DOMContentLoaded", () => { |
| 157 |
gcovr.fileLoaded(); |
| 158 |
}); |