1 var addSorting = (function () {
9 // returns the summary table element
10 function getTable() { return document.querySelector('.coverage-summary table'); }
11 // returns the thead element of the summary table
12 function getTableHeader() { return getTable().querySelector('thead tr'); }
13 // returns the tbody element of the summary table
14 function getTableBody() { return getTable().querySelector('tbody'); }
15 // returns the th element for nth column
16 function getNthColumn(n) { return getTableHeader().querySelectorAll('th')[n]; }
19 function loadColumns() {
20 var colNodes = getTableHeader().querySelectorAll('th'),
26 for (i = 0; i < colNodes.length; i += 1) {
27 colNode = colNodes[i];
29 key: colNode.getAttribute('data-col'),
30 sortable: !colNode.getAttribute('data-nosort'),
31 type: colNode.getAttribute('data-type') || 'string'
35 col.defaultDescSort = col.type === 'number';
36 colNode.innerHTML = colNode.innerHTML + '<span class="sorter"></span>';
41 // attaches a data attribute to every tr element with an object
42 // of data values keyed by column name
43 function loadRowData(tableRow) {
44 var tableCols = tableRow.querySelectorAll('td'),
50 for (i = 0; i < tableCols.length; i += 1) {
51 colNode = tableCols[i];
53 val = colNode.getAttribute('data-value');
54 if (col.type === 'number') {
63 var rows = getTableBody().querySelectorAll('tr'),
66 for (i = 0; i < rows.length; i += 1) {
67 rows[i].data = loadRowData(rows[i]);
70 // sorts the table using the data for the ith column
71 function sortByIndex(index, desc) {
72 var key = cols[index].key,
73 sorter = function (a, b) {
76 return a < b ? -1 : a > b ? 1 : 0;
79 tableBody = document.querySelector('.coverage-summary tbody'),
80 rowNodes = tableBody.querySelectorAll('tr'),
85 finalSorter = function (a, b) {
86 return -1 * sorter(a, b);
90 for (i = 0; i < rowNodes.length; i += 1) {
91 rows.push(rowNodes[i]);
92 tableBody.removeChild(rowNodes[i]);
95 rows.sort(finalSorter);
97 for (i = 0; i < rows.length; i += 1) {
98 tableBody.appendChild(rows[i]);
101 // removes sort indicators for current column being sorted
102 function removeSortIndicators() {
103 var col = getNthColumn(currentSort.index),
106 cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
109 // adds sort indicators for current column being sorted
110 function addSortIndicators() {
111 getNthColumn(currentSort.index).className += currentSort.desc ? ' sorted-desc' : ' sorted';
113 // adds event listeners for all sorter widgets
114 function enableUI() {
117 ithSorter = function ithSorter(i) {
121 var desc = col.defaultDescSort;
123 if (currentSort.index === i) {
124 desc = !currentSort.desc;
126 sortByIndex(i, desc);
127 removeSortIndicators();
128 currentSort.index = i;
129 currentSort.desc = desc;
133 for (i =0 ; i < cols.length; i += 1) {
134 if (cols[i].sortable) {
135 el = getNthColumn(i).querySelector('.sorter');
136 if (el.addEventListener) {
137 el.addEventListener('click', ithSorter(i));
139 el.attachEvent('onclick', ithSorter(i));
144 // adds sorting functionality to the UI
149 cols = loadColumns();
156 window.addEventListener('load', addSorting);