|
1 | | - |
2 | 1 | var jumpToCode = (function init() { |
3 | | - // Classes of code we would like to highlight in the file view |
4 | | - var missingCoverageClasses = ['.cbranch-no', '.cstat-no', '.fstat-no']; |
| 2 | + // Classes of code we would like to highlight in the file view |
| 3 | + var missingCoverageClasses = ['.cbranch-no', '.cstat-no', '.fstat-no']; |
5 | 4 |
|
6 | | - // Elements to highlight in the file listing view |
7 | | - var fileListingElements = ['td.pct.low']; |
| 5 | + // Elements to highlight in the file listing view |
| 6 | + var fileListingElements = ['td.pct.low']; |
8 | 7 |
|
9 | | - // We don't want to select elements that are direct descendants of another match |
10 | | - var notSelector = ':not(' + missingCoverageClasses.join('):not(') + ') > '; // becomes `:not(a):not(b) > ` |
| 8 | + // We don't want to select elements that are direct descendants of another match |
| 9 | + var notSelector = ':not(' + missingCoverageClasses.join('):not(') + ') > '; // becomes `:not(a):not(b) > ` |
11 | 10 |
|
12 | | - // Selecter that finds elements on the page to which we can jump |
13 | | - var selector = |
14 | | - fileListingElements.join(', ') + |
15 | | - ', ' + |
16 | | - notSelector + |
17 | | - missingCoverageClasses.join(', ' + notSelector); // becomes `:not(a):not(b) > a, :not(a):not(b) > b` |
| 11 | + // Selecter that finds elements on the page to which we can jump |
| 12 | + var selector = |
| 13 | + fileListingElements.join(', ') + |
| 14 | + ', ' + |
| 15 | + notSelector + |
| 16 | + missingCoverageClasses.join(', ' + notSelector); // becomes `:not(a):not(b) > a, :not(a):not(b) > b` |
18 | 17 |
|
19 | | - // The NodeList of matching elements |
20 | | - var missingCoverageElements = document.querySelectorAll(selector); |
| 18 | + // The NodeList of matching elements |
| 19 | + var missingCoverageElements = document.querySelectorAll(selector); |
21 | 20 |
|
22 | | - var currentIndex; |
| 21 | + var currentIndex; |
23 | 22 |
|
24 | | - function toggleClass(index) { |
25 | | - missingCoverageElements |
26 | | - .item(currentIndex) |
27 | | - .classList.remove('highlighted'); |
28 | | - missingCoverageElements.item(index).classList.add('highlighted'); |
29 | | - } |
30 | | - |
31 | | - function makeCurrent(index) { |
32 | | - toggleClass(index); |
33 | | - currentIndex = index; |
34 | | - missingCoverageElements.item(index).scrollIntoView({ |
35 | | - behavior: 'smooth', |
36 | | - block: 'center', |
37 | | - inline: 'center' |
38 | | - }); |
39 | | - } |
| 23 | + function toggleClass(index) { |
| 24 | + missingCoverageElements.item(currentIndex).classList.remove('highlighted'); |
| 25 | + missingCoverageElements.item(index).classList.add('highlighted'); |
| 26 | + } |
40 | 27 |
|
41 | | - function goToPrevious() { |
42 | | - var nextIndex = 0; |
43 | | - if (typeof currentIndex !== 'number' || currentIndex === 0) { |
44 | | - nextIndex = missingCoverageElements.length - 1; |
45 | | - } else if (missingCoverageElements.length > 1) { |
46 | | - nextIndex = currentIndex - 1; |
47 | | - } |
| 28 | + function makeCurrent(index) { |
| 29 | + toggleClass(index); |
| 30 | + currentIndex = index; |
| 31 | + missingCoverageElements.item(index).scrollIntoView({ |
| 32 | + behavior: 'smooth', |
| 33 | + block: 'center', |
| 34 | + inline: 'center', |
| 35 | + }); |
| 36 | + } |
48 | 37 |
|
49 | | - makeCurrent(nextIndex); |
| 38 | + function goToPrevious() { |
| 39 | + var nextIndex = 0; |
| 40 | + if (typeof currentIndex !== 'number' || currentIndex === 0) { |
| 41 | + nextIndex = missingCoverageElements.length - 1; |
| 42 | + } else if (missingCoverageElements.length > 1) { |
| 43 | + nextIndex = currentIndex - 1; |
50 | 44 | } |
51 | 45 |
|
52 | | - function goToNext() { |
53 | | - var nextIndex = 0; |
| 46 | + makeCurrent(nextIndex); |
| 47 | + } |
54 | 48 |
|
55 | | - if ( |
56 | | - typeof currentIndex === 'number' && |
57 | | - currentIndex < missingCoverageElements.length - 1 |
58 | | - ) { |
59 | | - nextIndex = currentIndex + 1; |
60 | | - } |
| 49 | + function goToNext() { |
| 50 | + var nextIndex = 0; |
61 | 51 |
|
62 | | - makeCurrent(nextIndex); |
| 52 | + if ( |
| 53 | + typeof currentIndex === 'number' && |
| 54 | + currentIndex < missingCoverageElements.length - 1 |
| 55 | + ) { |
| 56 | + nextIndex = currentIndex + 1; |
63 | 57 | } |
64 | 58 |
|
65 | | - return function jump(event) { |
66 | | - if ( |
67 | | - document.getElementById('fileSearch') === document.activeElement && |
68 | | - document.activeElement != null |
69 | | - ) { |
70 | | - // if we're currently focused on the search input, we don't want to navigate |
71 | | - return; |
72 | | - } |
| 59 | + makeCurrent(nextIndex); |
| 60 | + } |
73 | 61 |
|
74 | | - switch (event.which) { |
75 | | - case 78: // n |
76 | | - case 74: // j |
77 | | - goToNext(); |
78 | | - break; |
79 | | - case 66: // b |
80 | | - case 75: // k |
81 | | - case 80: // p |
82 | | - goToPrevious(); |
83 | | - break; |
84 | | - } |
85 | | - }; |
| 62 | + return function jump(event) { |
| 63 | + if ( |
| 64 | + document.getElementById('fileSearch') === document.activeElement && |
| 65 | + document.activeElement != null |
| 66 | + ) { |
| 67 | + // if we're currently focused on the search input, we don't want to navigate |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + switch (event.which) { |
| 72 | + case 78: // n |
| 73 | + case 74: // j |
| 74 | + goToNext(); |
| 75 | + break; |
| 76 | + case 66: // b |
| 77 | + case 75: // k |
| 78 | + case 80: // p |
| 79 | + goToPrevious(); |
| 80 | + break; |
| 81 | + } |
| 82 | + }; |
86 | 83 | })(); |
87 | 84 | window.addEventListener('keydown', jumpToCode); |
0 commit comments