2019-09-26 10:20:53 +02:00
|
|
|
// @ts-check
|
2019-09-22 04:06:51 +02:00
|
|
|
(function () {
|
|
|
|
/** @param {Node | null} el */
|
|
|
|
const remove = (el) => { if (el) el.parentElement.removeChild(el) };
|
2019-09-21 03:54:17 +02:00
|
|
|
|
2019-09-22 04:06:51 +02:00
|
|
|
const stripPx = (s) => +s.slice(0, -2);
|
2019-09-21 03:54:17 +02:00
|
|
|
|
2019-09-22 04:06:51 +02:00
|
|
|
/** @param {string} tag
|
|
|
|
* @param {string} cls
|
|
|
|
* @returns HTMLElement
|
|
|
|
*/
|
|
|
|
function newEl(tag, cls) {
|
|
|
|
const el = document.createElement(tag);
|
|
|
|
el.classList.add(cls);
|
|
|
|
return el;
|
|
|
|
}
|
2019-09-21 03:54:17 +02:00
|
|
|
|
2019-09-22 04:06:51 +02:00
|
|
|
/** @type {<T extends any[]>(fn: (...args: T) => void, t: number) => ((...args: T) => void)} */
|
|
|
|
function debounce(f, ms) {
|
|
|
|
let t = Date.now();
|
|
|
|
return (...args) => {
|
|
|
|
const now = Date.now();
|
|
|
|
if (now - t < ms) return;
|
|
|
|
t = now;
|
|
|
|
f(...args);
|
|
|
|
};
|
|
|
|
}
|
2019-09-21 03:54:17 +02:00
|
|
|
|
2019-09-22 04:06:51 +02:00
|
|
|
const clsPrefix = "newsfoot-footnote-";
|
|
|
|
const CONTAINER_CLS = `${clsPrefix}container`;
|
|
|
|
const POPOVER_CLS = `${clsPrefix}popover`;
|
2019-09-23 10:25:00 +02:00
|
|
|
const POPOVER_INNER_CLS = `${clsPrefix}popover-inner`;
|
|
|
|
const POPOVER_ARROW_CLS = `${clsPrefix}popover-arrow`;
|
2019-09-21 03:54:17 +02:00
|
|
|
|
|
|
|
/**
|
2019-09-26 10:20:53 +02:00
|
|
|
* @param {string} content
|
2019-09-22 04:06:51 +02:00
|
|
|
* @returns {HTMLElement}
|
2019-09-21 03:54:17 +02:00
|
|
|
*/
|
2019-09-22 04:06:51 +02:00
|
|
|
function footnoteMarkup(content) {
|
|
|
|
const popover = newEl("div", POPOVER_CLS);
|
2019-09-23 10:25:00 +02:00
|
|
|
const arrow = newEl("div", POPOVER_ARROW_CLS);
|
|
|
|
const inner = newEl("div", POPOVER_INNER_CLS);
|
|
|
|
popover.appendChild(inner);
|
|
|
|
popover.appendChild(arrow);
|
2019-09-26 10:20:53 +02:00
|
|
|
inner.innerHTML = content;
|
2019-09-22 04:06:51 +02:00
|
|
|
return popover;
|
2019-09-21 03:54:17 +02:00
|
|
|
}
|
2019-09-22 04:06:51 +02:00
|
|
|
|
|
|
|
class Footnote {
|
|
|
|
/**
|
2019-09-26 10:20:53 +02:00
|
|
|
* @param {string} content
|
2019-09-22 04:06:51 +02:00
|
|
|
* @param {Element} fnref
|
|
|
|
*/
|
|
|
|
constructor(content, fnref) {
|
|
|
|
this.popover = footnoteMarkup(content);
|
|
|
|
this.style = window.getComputedStyle(this.popover);
|
|
|
|
this.fnref = fnref;
|
2019-09-23 10:25:00 +02:00
|
|
|
this.fnref.closest(`.${CONTAINER_CLS}`).insertBefore(this.popover, fnref);
|
2019-09-26 10:20:53 +02:00
|
|
|
/** @type {HTMLElement} */
|
|
|
|
this.arrow = this.popover.querySelector(`.${POPOVER_ARROW_CLS}`);
|
2019-09-22 04:06:51 +02:00
|
|
|
this.reposition();
|
|
|
|
|
|
|
|
/** @type {(ev:MouseEvent) => void} */
|
|
|
|
this.clickoutHandler = (ev) => {
|
|
|
|
if (!(ev.target instanceof Element)) return;
|
|
|
|
if (ev.target.closest(`.${POPOVER_CLS}`) === this.popover) return;
|
2019-09-23 10:25:00 +02:00
|
|
|
if (ev.target === this.fnref) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
2019-09-22 04:06:51 +02:00
|
|
|
this.cleanup();
|
|
|
|
}
|
|
|
|
document.addEventListener("click", this.clickoutHandler, {capture: true});
|
|
|
|
|
|
|
|
this.resizeHandler = debounce(() => this.reposition(), 20);
|
|
|
|
window.addEventListener("resize", this.resizeHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
remove(this.popover);
|
|
|
|
document.removeEventListener("click", this.clickoutHandler, {capture: true});
|
|
|
|
window.removeEventListener("resize", this.resizeHandler);
|
|
|
|
delete this.popover;
|
|
|
|
delete this.clickoutHandler;
|
|
|
|
delete this.resizeHandler;
|
2019-09-21 03:54:17 +02:00
|
|
|
}
|
2019-09-22 04:06:51 +02:00
|
|
|
|
|
|
|
reposition() {
|
|
|
|
const refRect = this.fnref.getBoundingClientRect();
|
|
|
|
const center = refRect.left + (refRect.width / 2);
|
|
|
|
const popoverHalfWidth = this.popover.clientWidth / 2;
|
|
|
|
const marginLeft = stripPx(this.style.marginLeft);
|
|
|
|
const marginRight = stripPx(this.style.marginRight);
|
|
|
|
|
2019-09-23 10:25:00 +02:00
|
|
|
const rightOverhang = center + popoverHalfWidth + marginRight > window.innerWidth;
|
|
|
|
const leftOverhang = center - (popoverHalfWidth + marginLeft) < 0;
|
|
|
|
|
2019-09-22 04:06:51 +02:00
|
|
|
let offset = 0;
|
2019-09-23 10:43:29 +02:00
|
|
|
if (!leftOverhang && rightOverhang) {
|
2019-09-22 04:06:51 +02:00
|
|
|
offset = -((center + popoverHalfWidth + marginRight) - window.innerWidth);
|
|
|
|
}
|
2019-09-23 10:43:29 +02:00
|
|
|
else if (leftOverhang && !rightOverhang) {
|
2019-09-22 04:06:51 +02:00
|
|
|
offset = (popoverHalfWidth + marginLeft) - center;
|
|
|
|
}
|
|
|
|
this.popover.style.transform = `translate(${offset}px)`;
|
2019-09-26 10:20:53 +02:00
|
|
|
this.arrow.style.transform = `translate(${-offset}px) rotate(45deg)`;
|
2019-09-21 03:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 04:06:51 +02:00
|
|
|
/** @param {HTMLAnchorElement} a */
|
|
|
|
function installContainer(a) {
|
|
|
|
if (!a.parentElement.matches(`.${CONTAINER_CLS}`)) {
|
|
|
|
const container = newEl("div", CONTAINER_CLS);
|
|
|
|
a.parentElement.insertBefore(container, a);
|
|
|
|
container.appendChild(a);
|
|
|
|
}
|
2019-09-21 03:54:17 +02:00
|
|
|
}
|
2019-09-26 10:20:53 +02:00
|
|
|
|
|
|
|
function idFromHash(target) {
|
|
|
|
if (!target.hash) return;
|
2020-02-24 15:29:02 +01:00
|
|
|
return decodeURIComponent(target.hash.substring(1));
|
2019-09-26 10:20:53 +02:00
|
|
|
}
|
|
|
|
/** @type {{fnref(target:HTMLAnchorElement): string|undefined}[]} */
|
|
|
|
const footnoteFormats = [
|
|
|
|
{ // Multimarkdown
|
|
|
|
fnref(target) {
|
2020-02-24 19:14:44 +01:00
|
|
|
if (!target.matches(".footnote")) return;
|
2019-09-26 10:20:53 +02:00
|
|
|
return idFromHash(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-09-23 09:56:58 +02:00
|
|
|
// Handle clicks on the footnote reference
|
2019-09-22 04:06:51 +02:00
|
|
|
document.addEventListener("click", (ev) => {
|
|
|
|
if (!(ev.target && ev.target instanceof HTMLAnchorElement)) return;
|
2019-09-26 10:20:53 +02:00
|
|
|
|
|
|
|
let targetId = undefined;
|
|
|
|
for(const f of footnoteFormats) {
|
|
|
|
targetId = f.fnref(ev.target);
|
|
|
|
if (targetId) break;
|
|
|
|
}
|
|
|
|
if (targetId === undefined) return;
|
2021-03-10 20:16:51 +01:00
|
|
|
|
|
|
|
// Only override the default behaviour when we know we can find the
|
|
|
|
// target element
|
2021-03-10 22:54:25 +01:00
|
|
|
const targetElement = document.getElementById(targetId);
|
2021-03-10 20:16:51 +01:00
|
|
|
if (targetElement === null) return;
|
|
|
|
|
2019-09-22 04:06:51 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
installContainer(ev.target);
|
2021-03-10 20:16:51 +01:00
|
|
|
|
|
|
|
void new Footnote(targetElement.innerHTML, ev.target);
|
2019-09-22 04:06:51 +02:00
|
|
|
});
|
2019-09-23 09:56:58 +02:00
|
|
|
|
|
|
|
// Handle clicks on the footnote reverse link
|
|
|
|
document.addEventListener("click", (ev) =>
|
|
|
|
{
|
|
|
|
if (!(ev.target && ev.target instanceof HTMLAnchorElement)) return;
|
2021-04-29 09:57:08 +02:00
|
|
|
if (!ev.target.matches(".footnotes .reversefootnote, .footnotes .footnoteBackLink, .footnotes .footnote-return, .footnotes a[href*='#fn'], .footnotes a[href^='#']")) return;
|
2020-02-24 19:15:04 +01:00
|
|
|
const id = idFromHash(ev.target);
|
|
|
|
if (!id) return;
|
|
|
|
const fnref = document.getElementById(id);
|
|
|
|
|
2019-09-23 09:56:58 +02:00
|
|
|
window.scrollTo({ top: fnref.getBoundingClientRect().top + window.scrollY });
|
|
|
|
ev.preventDefault();
|
|
|
|
});
|
2019-09-22 04:06:51 +02:00
|
|
|
}());
|