2021-04-09 22:00:24 +03:00
|
|
|
import userInit from "/index.js";
|
|
|
|
|
2022-02-21 20:00:34 +03:00
|
|
|
let prefetchedPages;
|
|
|
|
let initialLocationHash;
|
2021-05-17 22:51:04 +03:00
|
|
|
/**
|
|
|
|
* @returns
|
|
|
|
*/
|
2021-06-25 01:55:41 +03:00
|
|
|
function loadContentAndInitializeApp() {
|
2021-04-09 22:00:24 +03:00
|
|
|
let path = window.location.pathname.replace(/(\w)$/, "$1/");
|
|
|
|
if (!path.endsWith("/")) {
|
|
|
|
path = path + "/";
|
|
|
|
}
|
2021-12-24 01:28:52 +03:00
|
|
|
const app = Elm.Main.init({
|
2021-06-25 01:55:41 +03:00
|
|
|
flags: {
|
|
|
|
secrets: null,
|
|
|
|
isPrerendering: false,
|
|
|
|
isDevServer: false,
|
|
|
|
isElmDebugMode: false,
|
2022-02-04 07:08:45 +03:00
|
|
|
contentJson: {},
|
2022-02-02 04:59:57 +03:00
|
|
|
pageDataBase64: document.getElementById("__ELM_PAGES_BYTES_DATA__")
|
|
|
|
.innerHTML,
|
2021-06-25 01:55:41 +03:00
|
|
|
userFlags: userInit.flags(),
|
|
|
|
},
|
|
|
|
});
|
2021-04-09 22:00:24 +03:00
|
|
|
|
2021-06-25 01:55:41 +03:00
|
|
|
app.ports.toJsPort.subscribe((fromElm) => {
|
|
|
|
loadNamedAnchor();
|
2021-04-09 22:00:24 +03:00
|
|
|
});
|
2021-06-25 01:55:41 +03:00
|
|
|
|
2022-01-29 20:15:19 +03:00
|
|
|
app.ports.elmPagesReloadData &&
|
2022-02-17 22:51:46 +03:00
|
|
|
app.ports.elmPagesReloadData.subscribe((data) => {
|
2022-01-29 20:15:19 +03:00
|
|
|
console.log("RELOAD DATA PORT!");
|
2022-02-17 22:51:46 +03:00
|
|
|
app.ports.fromJsPort.send({ tag: "Reload", data });
|
2022-01-29 20:15:19 +03:00
|
|
|
});
|
2022-01-25 22:20:18 +03:00
|
|
|
|
2021-06-25 01:55:41 +03:00
|
|
|
return app;
|
2021-04-09 22:00:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadNamedAnchor() {
|
|
|
|
if (initialLocationHash !== "") {
|
|
|
|
const namedAnchor = document.querySelector(`[name=${initialLocationHash}]`);
|
|
|
|
namedAnchor && namedAnchor.scrollIntoView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prefetchIfNeeded(/** @type {HTMLAnchorElement} */ target) {
|
2021-06-25 01:55:41 +03:00
|
|
|
if (
|
|
|
|
target.host === window.location.host &&
|
|
|
|
!prefetchedPages.includes(target.pathname)
|
|
|
|
) {
|
|
|
|
prefetchedPages.push(target.pathname);
|
|
|
|
const link = document.createElement("link");
|
|
|
|
link.setAttribute("as", "fetch");
|
|
|
|
|
|
|
|
link.setAttribute("rel", "prefetch");
|
2022-02-01 21:11:47 +03:00
|
|
|
link.setAttribute("href", origin + target.pathname + "/content.dat");
|
2021-06-25 01:55:41 +03:00
|
|
|
document.head.appendChild(link);
|
2021-04-09 22:00:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:00:34 +03:00
|
|
|
export function setup() {
|
|
|
|
prefetchedPages = [window.location.pathname];
|
|
|
|
initialLocationHash = document.location.hash.replace(/^#/, "");
|
|
|
|
const appPromise = new Promise(function (resolve, reject) {
|
|
|
|
document.addEventListener("DOMContentLoaded", (_) => {
|
|
|
|
resolve(loadContentAndInitializeApp());
|
2021-04-18 07:21:17 +03:00
|
|
|
});
|
2021-04-09 22:00:24 +03:00
|
|
|
});
|
2022-02-21 20:00:34 +03:00
|
|
|
userInit.load(appPromise);
|
2021-05-15 05:33:52 +03:00
|
|
|
|
2022-02-21 20:00:34 +03:00
|
|
|
if (typeof connect === "function") {
|
|
|
|
connect(function (bytesData) {
|
|
|
|
appPromise.then((app) => {
|
|
|
|
app.ports.hotReloadData.send(bytesData);
|
|
|
|
});
|
|
|
|
});
|
2021-05-15 05:33:52 +03:00
|
|
|
}
|
|
|
|
|
2022-02-21 20:00:34 +03:00
|
|
|
/** @param {MouseEvent} event */
|
|
|
|
const trigger_prefetch = (event) => {
|
|
|
|
const a = find_anchor(/** @type {Node} */ (event.target));
|
|
|
|
if (a && a.href && a.hasAttribute("elm-pages:prefetch")) {
|
|
|
|
prefetchIfNeeded(a);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @type {NodeJS.Timeout} */
|
|
|
|
let mousemove_timeout;
|
|
|
|
|
|
|
|
/** @param {MouseEvent} event */
|
|
|
|
const handle_mousemove = (event) => {
|
|
|
|
clearTimeout(mousemove_timeout);
|
|
|
|
mousemove_timeout = setTimeout(() => {
|
|
|
|
trigger_prefetch(event);
|
|
|
|
}, 20);
|
|
|
|
};
|
|
|
|
|
|
|
|
addEventListener("touchstart", trigger_prefetch);
|
|
|
|
addEventListener("mousemove", handle_mousemove);
|
|
|
|
}
|
2021-05-15 05:33:52 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Node} node
|
|
|
|
// * @rturns {HTMLAnchorElement | SVGAElement}
|
|
|
|
* @returns {HTMLAnchorElement}
|
|
|
|
*/
|
|
|
|
function find_anchor(node) {
|
|
|
|
while (node && node.nodeName.toUpperCase() !== "A") node = node.parentNode; // SVG <a> elements have a lowercase name
|
|
|
|
return /** @type {HTMLAnchorElement} */ (node);
|
|
|
|
}
|
2022-02-23 23:20:38 +03:00
|
|
|
|
|
|
|
setup();
|