|
| 1 | +function deselectAllExcept(selector) { |
| 2 | + var allSelected = document.getElementsByClassName('selected'); |
| 3 | + for (var i = 0; i < allSelected.length; i++) { |
| 4 | + if ( |
| 5 | + allSelected[i].id !== selector && |
| 6 | + allSelected[i].getAttribute('aria-details') !== selector |
| 7 | + ) { |
| 8 | + allSelected[i].classList.remove('selected'); |
| 9 | + } |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +function makeClickHandler(isHighlight) { |
| 14 | + return function onClick(event) { |
| 15 | + var targetElement, selector, corresponding; |
| 16 | + if (isHighlight) { |
| 17 | + selector = event.target.getAttribute('aria-details'); |
| 18 | + targetElement = event.target; |
| 19 | + } else { |
| 20 | + if (event.target.getAttribute('role') === 'comment') { |
| 21 | + selector = event.target.id; |
| 22 | + targetElement = event.target; |
| 23 | + } else { |
| 24 | + // Depending on where they click, they may have targeted a child element |
| 25 | + var annotation = event.target.closest('[role="comment"]'); |
| 26 | + targetElement = annotation; |
| 27 | + selector = annotation.id; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if (isHighlight) { |
| 32 | + corresponding = document.getElementById(selector); |
| 33 | + } else { |
| 34 | + corresponding = document.querySelector(`[aria-details="${selector}"]`); |
| 35 | + } |
| 36 | + |
| 37 | + // Highlight click target and corresponding element, and scroll to corresponding element |
| 38 | + // If target is already highlighted, dehilight (and don't scroll) |
| 39 | + const isSelected = targetElement.classList.toggle('selected'); |
| 40 | + corresponding.classList.toggle('selected'); |
| 41 | + if (isSelected) { |
| 42 | + var prefersReducedMotionQuery = window.matchMedia('(prefers-reduced-motion: reduce)'); |
| 43 | + var prefersReducedMotion = !prefersReducedMotionQuery || prefersReducedMotionQuery.matches; |
| 44 | + corresponding.scrollIntoView({ |
| 45 | + behavior: prefersReducedMotion ? 'auto' : 'smooth', |
| 46 | + block: 'nearest', |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + // Ensure this is the only highlighted pair |
| 51 | + deselectAllExcept(selector); |
| 52 | + |
| 53 | + // Avoid bubbling through to the deselectAll function |
| 54 | + event.stopPropagation(); |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function deselectAll() { |
| 59 | + var selectedComments = document.querySelectorAll('.selected'); |
| 60 | + for (var i = 0; i < selectedComments.length; i++) { |
| 61 | + selectedComments[i].classList.remove('selected'); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function onInitialLoad() { |
| 66 | + document.documentElement.className = document.documentElement.className.replace( |
| 67 | + /\bno-js\b/, |
| 68 | + 'js', |
| 69 | + ); |
| 70 | + |
| 71 | + var highlights = document.getElementsByTagName('mark'); |
| 72 | + for (var i = 0; i < highlights.length; i++) { |
| 73 | + highlights[i].addEventListener('click', makeClickHandler(true)); |
| 74 | + } |
| 75 | + var comments = document.getElementsByClassName('annotation'); |
| 76 | + for (var j = 0; j < comments.length; j++) { |
| 77 | + comments[j].addEventListener('click', makeClickHandler(false)); |
| 78 | + } |
| 79 | + |
| 80 | + document.addEventListener('click', deselectAll); |
| 81 | +} |
| 82 | + |
| 83 | +(function () { |
| 84 | + if (document.readyState != 'loading') { |
| 85 | + onInitialLoad(); |
| 86 | + } else { |
| 87 | + document.addEventListener('DOMContentLoaded', onInitialLoad); |
| 88 | + } |
| 89 | +})(); |
0 commit comments