2019-06-07 12:46:19 -04:00
|
|
|
import * as DOM from "../services/dom";
|
2019-06-07 19:02:51 -04:00
|
|
|
import {scrollAndHighlightElement} from "../services/util";
|
2018-04-01 07:46:27 -04:00
|
|
|
|
|
|
|
class PageDisplay {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.pageId = elem.getAttribute('page-display');
|
|
|
|
|
2022-02-08 06:10:01 -05:00
|
|
|
window.importVersioned('code').then(Code => Code.highlight());
|
2018-04-01 07:46:27 -04:00
|
|
|
this.setupNavHighlighting();
|
2021-01-30 12:04:30 -05:00
|
|
|
this.setupDetailsCodeBlockRefresh();
|
2018-04-01 07:46:27 -04:00
|
|
|
|
|
|
|
// Check the hash on load
|
|
|
|
if (window.location.hash) {
|
|
|
|
let text = window.location.hash.replace(/\%20/g, ' ').substr(1);
|
|
|
|
this.goToText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sidebar page nav click event
|
2019-06-07 12:46:19 -04:00
|
|
|
const sidebarPageNav = document.querySelector('.sidebar-page-nav');
|
2019-06-07 19:02:51 -04:00
|
|
|
if (sidebarPageNav) {
|
|
|
|
DOM.onChildEvent(sidebarPageNav, 'a', 'click', (event, child) => {
|
2019-06-16 07:46:23 -04:00
|
|
|
event.preventDefault();
|
2022-11-14 18:19:02 -05:00
|
|
|
window.$components.first('tri-layout').showContent();
|
2019-06-16 07:46:23 -04:00
|
|
|
const contentId = child.getAttribute('href').substr(1);
|
|
|
|
this.goToText(contentId);
|
|
|
|
window.history.pushState(null, null, '#' + contentId);
|
2019-06-07 19:02:51 -04:00
|
|
|
});
|
|
|
|
}
|
2018-04-01 07:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
goToText(text) {
|
2019-06-07 12:46:19 -04:00
|
|
|
const idElem = document.getElementById(text);
|
|
|
|
|
|
|
|
DOM.forEach('.page-content [data-highlighted]', elem => {
|
|
|
|
elem.removeAttribute('data-highlighted');
|
|
|
|
elem.style.backgroundColor = null;
|
|
|
|
});
|
|
|
|
|
2018-04-01 07:46:27 -04:00
|
|
|
if (idElem !== null) {
|
2019-06-07 19:02:51 -04:00
|
|
|
scrollAndHighlightElement(idElem);
|
2018-04-01 07:46:27 -04:00
|
|
|
} else {
|
2019-06-07 12:46:19 -04:00
|
|
|
const textElem = DOM.findText('.page-content > div > *', text);
|
|
|
|
if (textElem) {
|
2019-06-07 19:02:51 -04:00
|
|
|
scrollAndHighlightElement(textElem);
|
2019-06-07 12:46:19 -04:00
|
|
|
}
|
2018-04-01 07:46:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupNavHighlighting() {
|
|
|
|
// Check if support is present for IntersectionObserver
|
2019-05-06 11:08:08 -04:00
|
|
|
if (!('IntersectionObserver' in window) ||
|
|
|
|
!('IntersectionObserverEntry' in window) ||
|
|
|
|
!('intersectionRatio' in window.IntersectionObserverEntry.prototype)) {
|
2018-04-01 07:46:27 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let pageNav = document.querySelector('.sidebar-page-nav');
|
|
|
|
|
|
|
|
// fetch all the headings.
|
|
|
|
let headings = document.querySelector('.page-content').querySelectorAll('h1, h2, h3, h4, h5, h6');
|
|
|
|
// if headings are present, add observers.
|
|
|
|
if (headings.length > 0 && pageNav !== null) {
|
|
|
|
addNavObserver(headings);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addNavObserver(headings) {
|
|
|
|
// Setup the intersection observer.
|
|
|
|
let intersectOpts = {
|
|
|
|
rootMargin: '0px 0px 0px 0px',
|
|
|
|
threshold: 1.0
|
|
|
|
};
|
|
|
|
let pageNavObserver = new IntersectionObserver(headingVisibilityChange, intersectOpts);
|
|
|
|
|
|
|
|
// observe each heading
|
2019-02-16 10:39:11 -05:00
|
|
|
for (let heading of headings) {
|
|
|
|
pageNavObserver.observe(heading);
|
2018-04-01 07:46:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function headingVisibilityChange(entries, observer) {
|
|
|
|
for (let entry of entries) {
|
|
|
|
let isVisible = (entry.intersectionRatio === 1);
|
|
|
|
toggleAnchorHighlighting(entry.target.id, isVisible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleAnchorHighlighting(elementId, shouldHighlight) {
|
2019-06-07 12:46:19 -04:00
|
|
|
DOM.forEach('a[href="#' + elementId + '"]', anchor => {
|
2019-02-16 10:39:11 -05:00
|
|
|
anchor.closest('li').classList.toggle('current-heading', shouldHighlight);
|
2019-06-07 12:46:19 -04:00
|
|
|
});
|
2018-04-01 07:46:27 -04:00
|
|
|
}
|
|
|
|
}
|
2021-01-30 12:04:30 -05:00
|
|
|
|
|
|
|
setupDetailsCodeBlockRefresh() {
|
|
|
|
const onToggle = event => {
|
|
|
|
const codeMirrors = [...event.target.querySelectorAll('.CodeMirror')];
|
|
|
|
codeMirrors.forEach(cm => cm.CodeMirror && cm.CodeMirror.refresh());
|
|
|
|
};
|
|
|
|
|
|
|
|
const details = [...this.elem.querySelectorAll('details')];
|
|
|
|
details.forEach(detail => detail.addEventListener('toggle', onToggle));
|
|
|
|
}
|
2018-04-01 07:46:27 -04:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default PageDisplay;
|