-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute-element.js
More file actions
75 lines (64 loc) · 3.55 KB
/
route-element.js
File metadata and controls
75 lines (64 loc) · 3.55 KB
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
import { parse } from './markdown/marked.min.js';
const importMetaUrl = new URL(import.meta.url);
// Implemenets early prototype of Stealifys Markdown handling behavior like parsing of inlineMarkdown
// when used inside div or button elemenets that get rendered.
// URL Handling
const baseUrl = new URL(window.location);
// While this can be used as custom element it mainly is used most time as full application container
// when you render and use this as customComponent / Element you will want to store data on it like
// routeElement.dataset.url = 'protocol://<any url>'
const routeElementDefinition = {
connectedCallback(el=document.body) { // Fires before layout paint cycles
// Above is Atomic. Mutation Hooks still work ofcourse.
const targetEl = this || el;
// fetchWithLineIterator(url,(line)=>doc.write(parse(line))).then((done=true)=>
// Here comes the put cache logic.
//);
// caches.match(importMetaUrl.pathname).then(exists=>exists || caches.open(importMetaUrl.pathname).then(cache=>
// cache.addAll([importMetaUrl.pathname,'./modules/marked.esm.js','./service-worker.js'])))
// DOM Utils
/**
AND (both classes)
el.getElementsByClassName("class1 class2");
el.querySelectorAll(".class1.class2");
OR (at least one class)
el.querySelectorAll(".class1,.class2");
XOR (one class but not the other)
el.querySelectorAll(".class1:not(.class2),.class2:not(.class1)");
NAND (not both classes)
el.querySelectorAll(":not(.class1),:not(.class2)");
NOR (not any of the two classes)
el.querySelectorAll(":not(.class1):not(.class2)");
*/
// deps
// !document.querySelector('link[href="./layout/layout.css"]') && document.head.appendChild(cEl(
// { rel:"stylesheet",href:"./layout/layout.css"},'link'));
// !globalThis.gradiantCss && document.head.appendChild(cEl(
// {id:'gradiant-css',rel:"stylesheet",href:"./layout/gradient.css"},'link'));
// Routing
window.onhashchange = () => fetch(new URL(window.location.hash.slice(2),baseUrl))
.then((r) => r.text()).then(parse).then((innerHTML)=>Object.assign(this,{innerHTML}));
!baseUrl.searchParams?.has('url') && baseUrl.hash && window.onhashchange();
if (baseUrl.searchParams?.has('url')) {
window.location.hash=`#!${baseUrl.searchParams.get('url')}`;
window.location.search = baseUrl.searchParams.delete('url') || baseUrl.search;
} else if (!window.location.hash && importMetaUrl.searchParams?.has('url')) {
window.location.hash = `#!${importMetaUrl.searchParams.get('url')}`;
} else if (!window.location.hash) {
window.location.hash = `#!${'./index.md'}`;
//console.log(window.location.hash, window.location.search, importMetaUrl.search)
}
new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
(mutation.type === "childList") ? mutation
.addedNodes.forEach((el)=>{
(el.nodeName === "DIV" && el.classList.contains('box') || el.nodeName === 'BUTTON')
&& Object.assign(el,{innerHTML:parse(el.innerHTML)});
}) : (mutation.type === "attributes") &&
console.log(`The ${mutation.attributeName} attribute was modified.`);
}
}).observe(document.body,{ attributes: false, childList: true, subtree: true });
}, // When used as HTML Element this makes it return the full rendered page for caching.
// You Maybe want to adjust that for other usecases.
toString(){return `${document.head.outerHTML}\n${document.body.outerHTML}`;}
}