1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/* We need to refresh the scroll spy after (un)hiding elements */
const refreshScrollSpy = () =>{
const dataSpyList = [].slice.call(document.querySelectorAll('[data-bs-spy="scroll"]'));
dataSpyList.forEach((dataSpyEl) => {
bootstrap.ScrollSpy.getInstance(dataSpyEl)
.refresh()
})
}
const propagateScrollSpyURL = () => {
window.addEventListener('activate.bs.scrollspy', (e) => {
history.replaceState({}, "", e.relatedTarget);
});
}
/* Show or hide elements based on their tag */
const toggleTagVisibility = (tagName) => {
const tag = tags.find((element) => {
return element.name === tagName;
});
tag.visible = !tag.visible;
const hiddenTagNames = tags.map((tag) => {
if (tag.visible) {
return
} else {
return tag.name
}
});
const elements = $(`[data-mne-tags~="${tagName}"]`);
elements.each((i) => {
const currentElement = elements[i];
const tagValuesOfCurrentElement = currentElement.getAttribute('data-mne-tags');
// TODO This can probably be refactored to not use a Set.
const tagNamesOfCurrentElement = new Set(tagValuesOfCurrentElement.match(/\S+/g)); // non-whitespace
const visibleTagNamesOfCurrentElement = new Set(
[...tagNamesOfCurrentElement].filter(e => !hiddenTagNames.includes(e))
);
if (visibleTagNamesOfCurrentElement.size === 0) { // hide
$(currentElement).slideToggle('fast', () => {
currentElement.classList.add('d-none');
});
} else if ($(currentElement).hasClass('d-none')) { // show
currentElement.classList.remove('d-none');
$(currentElement).slideToggle('fast');
}
})
const tagBadgeElements = document.querySelectorAll(`span.badge[data-mne-tag~="${tagName}"]`);
tagBadgeElements.forEach((badgeElement) => {
if (tag.visible) {
badgeElement.removeAttribute('data-mne-tag-hidden');
badgeElement.classList.remove('bg-secondary');
badgeElement.classList.add('bg-primary');
} else {
badgeElement.setAttribute('data-mne-tag-hidden', true);
badgeElement.classList.remove('bg-primary');
badgeElement.classList.add('bg-secondary');
}
})
refreshScrollSpy();
}
/* Gather all available tags and expose them in the global namespace */
let tags = []; // array of objects
const gatherTags = () => {
// only consider top-level elements
const taggedElements = document.querySelectorAll("#content > div[data-mne-tags]");
taggedElements.forEach((element) => {
const value = element.getAttribute('data-mne-tags');
const tagNames = value.match(/\S+/g); // non-whitespace
tagNames.forEach((tagName) => {
const existingTag = tags.find((element) => {
return element.name === tagName;
})
if (existingTag === undefined) {
const tag = {
name : tagName,
visible: true,
count: 1
};
tags.push(tag);
} else {
existingTag.count = existingTag.count + 1;
}
})
})
}
/* Badges do display the tag count */
const updateTagCountBadges = () => {
const menuEntries = document
.querySelectorAll("#filter-by-tags-dropdown-menu > ul > li > label[data-mne-tag]")
menuEntries.forEach((menuEntry) => {
const tagName = menuEntry.getAttribute('data-mne-tag');
const tag = tags.find((tag) => {
return tag.name === tagName;
})
const tagCount = tag.count;
const tagCountBadge = menuEntry.querySelector('span.badge');
tagCountBadge.innerHTML = tagCount.toString();
});
}
const addFilterByTagsCheckboxEventHandlers = () => {
// "Filter by tag" checkbox event handling
const selectAllTagsCheckboxLabel = document
.querySelector('#selectAllTagsCheckboxLabel');
const filterByTagsDropdownMenuLabels = document
.querySelectorAll("#filter-by-tags-dropdown-menu > ul > li > label[data-mne-tag]")
filterByTagsDropdownMenuLabels.forEach((label) => {
// Prevent dropdown menu from closing when clicking on a tag checkbox label
label.addEventListener("click", (e) => {
e.stopPropagation();
})
// Show / hide content if a tag checkbox value has changed
const tagName = label.getAttribute("data-mne-tag");
const checkbox = label.querySelector("input");
checkbox.addEventListener("change", () => {
toggleTagVisibility(tagName);
})
})
// "Select all" checkbox
selectAllTagsCheckboxLabel.addEventListener("click", (e) => {
e.stopPropagation();
})
const selectAllTagsCheckbox = selectAllTagsCheckboxLabel.querySelector('input');
selectAllTagsCheckbox.addEventListener("change", (e) => {
const selectAllCheckboxStatus = e.target.checked;
filterByTagsDropdownMenuLabels.forEach((element) => {
const checkbox = element.querySelector('input');
if (checkbox.checked !== selectAllCheckboxStatus) {
checkbox.checked = selectAllCheckboxStatus
// we need to manually trigger the change event
const changeEvent = new Event('change');
checkbox.dispatchEvent(changeEvent);
}
})
});
}
/* Avoid top of content getting hidden behind navbar after clicking on a TOC
link */
const _handleTocLinkClick = (e) => {
e.preventDefault();
const topBarHeight = document.querySelector('#top-bar').scrollHeight
const margin = 30 + topBarHeight;
const tocLinkElement = e.target;
const targetDomId = tocLinkElement.getAttribute('href');
const targetElement = document.querySelector(targetDomId);
const top = targetElement.getBoundingClientRect().top + window.scrollY;
// Update URL to reflect the current scroll position.
// We use history.pushState to change the URL without causing the browser to scroll.
history.pushState(null, "", targetDomId);
// Now scroll to the correct position.
window.scrollTo(0, top - margin);
}
const fixScrollingForTocLinks = () => {
const tocLinkElements = document.querySelectorAll('#toc-navbar > a');
tocLinkElements.forEach((element) => {
element.removeEventListener('click', _handleTocLinkClick)
element.addEventListener('click', _handleTocLinkClick)
})
}
const addSliderEventHandlers = () => {
const accordionElementsWithSlider = document.querySelectorAll('div.accordion-item.slider');
accordionElementsWithSlider.forEach((el) => {
const accordionElement = el.querySelector('div.accordion-body');
const slider = accordionElement.querySelector('input');
// const sliderLabel = accordionElement.querySelector('label');
const carousel = accordionElement.querySelector('div.carousel');
slider.addEventListener('input', (e) => {
const sliderValue = parseInt(e.target.value);
$(carousel).carousel(sliderValue);
})
// Allow focussing the slider with a click on the slider or carousel, so keyboard
// controls (left / right arrow) can be enabled.
// This also appears to be the only way to focus the slider in Safari:
// https://itnext.io/fixing-focus-for-safari-b5916fef1064?gi=c1b8b043fa9b
slider.addEventListener('click', () => {
slider.focus({preventScroll: true})
})
carousel.addEventListener('click', () => {
slider.focus({preventScroll: true})
})
})
}
/* Avoid top of content gets hidden behind the top navbar */
const fixTopMargin = () => {
const topBarHeight = document.querySelector('#top-bar').scrollHeight
const margin = 30 + topBarHeight;
document.getElementById('content').style.marginTop = `${margin}px`;
document.getElementById('toc').style.marginTop = `${margin}px`;
}
/* Show / hide all tags on keypress */
const _globalKeyHandler = (e) => {
if (e.code === "KeyT") {
const selectAllTagsCheckbox = document
.querySelector('#selectAllTagsCheckboxLabel > input');
selectAllTagsCheckbox.checked = !selectAllTagsCheckbox.checked;
// we need to manually trigger the change event
const changeEvent = new Event('change');
selectAllTagsCheckbox.dispatchEvent(changeEvent);
}
}
const enableGlobalKeyHandler = () => {
window.onkeydown = (e) => _globalKeyHandler(e);
}
const disableGlobalKeyHandler = () => {
window.onkeydown = null;
}
/* Disable processing global key events when a search box is active */
const disableGlobalKeysInSearchBox = () => {
const searchBoxElements = document.querySelectorAll('input.search-input');
searchBoxElements.forEach((el) => {
el.addEventListener('focus', () => disableGlobalKeyHandler());
el.addEventListener('blur', () => enableGlobalKeyHandler());
})
}
/* Run once all content is fully loaded. */
window.addEventListener('load', () => {
gatherTags();
updateTagCountBadges();
addFilterByTagsCheckboxEventHandlers();
addSliderEventHandlers();
fixTopMargin();
fixScrollingForTocLinks();
hljs.highlightAll(); // enable highlight.js
disableGlobalKeysInSearchBox();
enableGlobalKeyHandler();
propagateScrollSpyURL();
});
/* Resizing the window throws off the scroll spy and top-margin handling. */
window.onresize = () => {
fixTopMargin();
refreshScrollSpy();
};
|