| 1 | const React = require('react'); |
| 2 | |
| 3 | function getSortDetails(sortKey, activeSort) { |
| 4 | let newSort = { sortKey, order: 'desc' }; |
| 5 | let sortClass = ''; |
| 6 | if (activeSort && activeSort.sortKey === sortKey) { |
| 7 | sortClass = 'sorted'; |
| 8 | if (activeSort.order === 'desc') { |
| 9 | sortClass += '-desc'; |
| 10 | newSort.order = 'asc'; |
| 11 | } else { |
| 12 | if (sortKey !== 'file') { |
| 13 | newSort = { sortKey: 'file', order: 'desc' }; |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | return { |
| 19 | newSort, |
| 20 | sortClass |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | function SummaryTableHeaderCell({ name, onSort, sortKey, activeSort }) { |
| 25 | const { newSort, sortClass } = getSortDetails(sortKey, activeSort); |
| 26 | return ( |
| 27 | <th |
| 28 | className={'sortable headercell ' + sortClass} |
| 29 | onClick={() => onSort(newSort)} |
| 30 | > |
| 31 | {name} |
| 32 | <span className="sorter" /> |
| 33 | </th> |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | function FileHeaderCell({ onSort, activeSort }) { |
| 38 | const { newSort, sortClass } = getSortDetails('file', activeSort); |
| 39 | |
| 40 | return ( |
| 41 | <th |
| 42 | className={'sortable file ' + sortClass} |
| 43 | onClick={() => onSort(newSort)} |
| 44 | > |
| 45 | File |
| 46 | <span className="sorter" /> |
| 47 | </th> |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | function SubHeadings({ sortKeyPrefix, onSort, activeSort }) { |
| 52 | return ( |
| 53 | <> |
| 54 | <SummaryTableHeaderCell |
| 55 | name="%" |
| 56 | onSort={onSort} |
| 57 | sortKey={sortKeyPrefix + '.pct'} |
| 58 | activeSort={activeSort} |
| 59 | /> |
| 60 | <th className="headercell"></th> |
| 61 | <SummaryTableHeaderCell |
| 62 | name="Covered" |
| 63 | onSort={onSort} |
| 64 | sortKey={sortKeyPrefix + '.covered'} |
| 65 | activeSort={activeSort} |
| 66 | /> |
| 67 | <SummaryTableHeaderCell |
| 68 | name="Missed" |
| 69 | onSort={onSort} |
| 70 | sortKey={sortKeyPrefix + '.missed'} |
| 71 | activeSort={activeSort} |
| 72 | /> |
| 73 | <SummaryTableHeaderCell |
| 74 | name="Total" |
| 75 | onSort={onSort} |
| 76 | sortKey={sortKeyPrefix + '.total'} |
| 77 | activeSort={activeSort} |
| 78 | /> |
| 79 | </> |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | module.exports = function SummaryTableHeader({ |
| 84 | onSort, |
| 85 | activeSort, |
| 86 | metricsToShow |
| 87 | }) { |
| 88 | return ( |
| 89 | <thead> |
| 90 | <tr className="topheading"> |
| 91 | <th></th> |
| 92 | {metricsToShow.statements && <th colSpan={4}>Statements</th>} |
| 93 | {metricsToShow.branches && <th colSpan={4}>Branches</th>} |
| 94 | {metricsToShow.functions && <th colSpan={4}>Functions</th>} |
| 95 | {metricsToShow.lines && <th colSpan={4}>Lines</th>} |
| 96 | </tr> |
| 97 | <tr className="subheading"> |
| 98 | <FileHeaderCell onSort={onSort} activeSort={activeSort} /> |
| 99 | {metricsToShow.statements && ( |
| 100 | <SubHeadings |
| 101 | sortKeyPrefix="statements" |
| 102 | onSort={onSort} |
| 103 | activeSort={activeSort} |
| 104 | /> |
| 105 | )} |
| 106 | {metricsToShow.branches && ( |
| 107 | <SubHeadings |
| 108 | sortKeyPrefix="branches" |
| 109 | onSort={onSort} |
| 110 | activeSort={activeSort} |
| 111 | /> |
| 112 | )} |
| 113 | {metricsToShow.functions && ( |
| 114 | <SubHeadings |
| 115 | sortKeyPrefix="functions" |
| 116 | onSort={onSort} |
| 117 | activeSort={activeSort} |
| 118 | /> |
| 119 | )} |
| 120 | {metricsToShow.lines && ( |
| 121 | <SubHeadings |
| 122 | sortKeyPrefix="lines" |
| 123 | onSort={onSort} |
| 124 | activeSort={activeSort} |
| 125 | /> |
| 126 | )} |
| 127 | </tr> |
| 128 | </thead> |
| 129 | ); |
| 130 | }; |