Debian Codegraph @

istanbul-reports/lib/html/assets/sorter.js

raw symbols SCIP
1 var addSorting = (function() {
2 'use strict';
3 var cols,
4 currentSort = {
5 index: 0,
6 desc: false
7 };
8
9 // returns the summary table element
10 function getTable() {
11 return document.querySelector('.coverage-summary');
12 }
13 // returns the thead element of the summary table
14 function getTableHeader() {
15 return getTable().querySelector('thead tr');
16 }
17 // returns the tbody element of the summary table
18 function getTableBody() {
19 return getTable().querySelector('tbody');
20 }
21 // returns the th element for nth column
22 function getNthColumn(n) {
23 return getTableHeader().querySelectorAll('th')[n];
24 }
25
26 function onFilterInput() {
27 const searchValue = document.getElementById('fileSearch').value;
28 const rows = document.getElementsByTagName('tbody')[0].children;
29 for (let i = 0; i < rows.length; i++) {
30 const row = rows[i];
31 if (
32 row.textContent
33 .toLowerCase()
34 .includes(searchValue.toLowerCase())
35 ) {
36 row.style.display = '';
37 } else {
38 row.style.display = 'none';
39 }
40 }
41 }
42
43 // loads the search box
44 function addSearchBox() {
45 var template = document.getElementById('filterTemplate');
46 var templateClone = template.content.cloneNode(true);
47 templateClone.getElementById('fileSearch').oninput = onFilterInput;
48 template.parentElement.appendChild(templateClone);
49 }
50
51 // loads all columns
52 function loadColumns() {
53 var colNodes = getTableHeader().querySelectorAll('th'),
54 colNode,
55 cols = [],
56 col,
57 i;
58
59 for (i = 0; i < colNodes.length; i += 1) {
60 colNode = colNodes[i];
61 col = {
62 key: colNode.getAttribute('data-col'),
63 sortable: !colNode.getAttribute('data-nosort'),
64 type: colNode.getAttribute('data-type') || 'string'
65 };
66 cols.push(col);
67 if (col.sortable) {
68 col.defaultDescSort = col.type === 'number';
69 colNode.innerHTML =
70 colNode.innerHTML + '<span class="sorter"></span>';
71 }
72 }
73 return cols;
74 }
75 // attaches a data attribute to every tr element with an object
76 // of data values keyed by column name
77 function loadRowData(tableRow) {
78 var tableCols = tableRow.querySelectorAll('td'),
79 colNode,
80 col,
81 data = {},
82 i,
83 val;
84 for (i = 0; i < tableCols.length; i += 1) {
85 colNode = tableCols[i];
86 col = cols[i];
87 val = colNode.getAttribute('data-value');
88 if (col.type === 'number') {
89 val = Number(val);
90 }
91 data[col.key] = val;
92 }
93 return data;
94 }
95 // loads all row data
96 function loadData() {
97 var rows = getTableBody().querySelectorAll('tr'),
98 i;
99
100 for (i = 0; i < rows.length; i += 1) {
101 rows[i].data = loadRowData(rows[i]);
102 }
103 }
104 // sorts the table using the data for the ith column
105 function sortByIndex(index, desc) {
106 var key = cols[index].key,
107 sorter = function(a, b) {
108 a = a.data[key];
109 b = b.data[key];
110 return a < b ? -1 : a > b ? 1 : 0;
111 },
112 finalSorter = sorter,
113 tableBody = document.querySelector('.coverage-summary tbody'),
114 rowNodes = tableBody.querySelectorAll('tr'),
115 rows = [],
116 i;
117
118 if (desc) {
119 finalSorter = function(a, b) {
120 return -1 * sorter(a, b);
121 };
122 }
123
124 for (i = 0; i < rowNodes.length; i += 1) {
125 rows.push(rowNodes[i]);
126 tableBody.removeChild(rowNodes[i]);
127 }
128
129 rows.sort(finalSorter);
130
131 for (i = 0; i < rows.length; i += 1) {
132 tableBody.appendChild(rows[i]);
133 }
134 }
135 // removes sort indicators for current column being sorted
136 function removeSortIndicators() {
137 var col = getNthColumn(currentSort.index),
138 cls = col.className;
139
140 cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
141 col.className = cls;
142 }
143 // adds sort indicators for current column being sorted
144 function addSortIndicators() {
145 getNthColumn(currentSort.index).className += currentSort.desc
146 ? ' sorted-desc'
147 : ' sorted';
148 }
149 // adds event listeners for all sorter widgets
150 function enableUI() {
151 var i,
152 el,
153 ithSorter = function ithSorter(i) {
154 var col = cols[i];
155
156 return function() {
157 var desc = col.defaultDescSort;
158
159 if (currentSort.index === i) {
160 desc = !currentSort.desc;
161 }
162 sortByIndex(i, desc);
163 removeSortIndicators();
164 currentSort.index = i;
165 currentSort.desc = desc;
166 addSortIndicators();
167 };
168 };
169 for (i = 0; i < cols.length; i += 1) {
170 if (cols[i].sortable) {
171 // add the click event handler on the th so users
172 // dont have to click on those tiny arrows
173 el = getNthColumn(i).querySelector('.sorter').parentElement;
174 if (el.addEventListener) {
175 el.addEventListener('click', ithSorter(i));
176 } else {
177 el.attachEvent('onclick', ithSorter(i));
178 }
179 }
180 }
181 }
182 // adds sorting functionality to the UI
183 return function() {
184 if (!getTable()) {
185 return;
186 }
187 cols = loadColumns();
188 loadData();
189 addSearchBox();
190 addSortIndicators();
191 enableUI();
192 };
193 })();
194
195 window.addEventListener('load', addSorting);