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
|
function getById(id) {
let element = document.getElementById(id);
if (element) {
return element;
} else {
throw `Element with id '${id}' not found.`;
}
}
/**
* @param {string} href
* @returns {HTMLAnchorElement}
*/
function a(href, pageName) {
const aElem = document.createElement("a");
aElem.href = href;
aElem.text = pageName;
return aElem;
}
function div(...children) {
const divElem = document.createElement("div");
divElem.replaceChildren(...children);
return divElem;
}
function span(text) {
const spanElem = document.createElement("span");
spanElem.textContent = text;
return spanElem;
}
function h(elementTag, ...children) {
const elem = document.createElement(elementTag);
elem.replaceChildren(...children);
return elem;
}
/** @param {HTMLElement} element */
function addClass(element, newClass) {
element.classList.add(newClass);
return element;
}
function writeError(error) {
writeLog(error, "error");
}
function writeInfo(msg) {
writeLog(msg, "info");
}
function writeDebug(msg) {
writeLog(msg, "debug");
}
function writeLog(msg, className) {
if (typeof msg === "string" || msg instanceof String) {
log.appendChild(div(addClass(span(msg), className)));
} else {
log.appendChild(div(addClass(msg, className)));
}
}
function resetLog() {
log.replaceChildren();
}
const log = getById("log");
// https://stackoverflow.com/questions/75988682/debounce-in-javascript
// https://www.joshwcomeau.com/snippets/javascript/debounce/
function debounce(callback, wait) {
let timeoutId = null;
return (...args) => {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(() => {
callback(...args);
}, wait);
};
}
const stylesheet = new CSSStyleSheet();
stylesheet.replaceSync(`
a {
color: var(--color);
}
nav > ul {
list-style-type: none;
padding-inline-start: 0;
}
nav > ul > li {
display: inline-block;
}
nav > ul > li > a::after {
display: inline-block;
color: var(--color);
content: ">";
padding: 0 1em;
}
`);
customElements.define("mb-nav",
class MBNav extends HTMLElement {
constructor() {
super();
}
get pagename() {
return this.getAttribute("pagename");
}
set pagename(value) {
this.setAttribute("pagename", value);
}
connectedCallback() {
const shadow = this.attachShadow({ mode: "open" });
shadow.adoptedStyleSheets = [stylesheet];
shadow.appendChild(
h("nav",
h("ul",
h("li", a("/", "MB.bes.is")),
h("li", span(this.pagename)),
)
)
)
}
});
export {
getById,
a,
div,
span,
h,
addClass,
writeError,
writeInfo,
writeDebug,
resetLog,
debounce,
};
|