-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathelement.flagmeister-list.js
75 lines (71 loc) · 2.72 KB
/
element.flagmeister-list.js
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
// Original 2018 WebComponent for index and documentation HTML pages
// could do with some refactoring to modern ES code
// List all SVG flags with headers
customElements.define(
"flagmeister-list",
class extends HTMLElement {
connectedCallback() {
const GZsize = 32;
const delay = 500; // execute after delay so header IMGs is available
setTimeout(() => {
let signals = this.hasAttribute("signals");
let img;
img = (iso, char = false) =>
`<flag-${iso} ${char ? "char=" + char : ""} style="" title="flag-${
iso + (char ? " char=" + char : "")
}"></flag-${iso}>`;
let isoNames = Object.keys(document.querySelector(`[is*=flag]`).flags);
let header = (x) => `<h3>${x}</h3>`;
this.innerHTML =
`<h2 class="flagmeister_SVGinfo"></h2>` +
header("ISO country code flags") +
isoNames.map((iso) => {
let counter = (id, items, title) =>
`<div id='${id}'>` +
["<h3>", 0, title, "</h3>", ...items].reduceRight(
(reducer, char, idx, redarr) =>
(idx > 3 ? (redarr[1]++, img(iso, char)) : char) + reducer,
""
) +
"</div>";
if (iso == "signal") {
if (signals)
return counter(
"FlagMeisterNATOFlags",
"abcdefghijklmnopqrstuvwxyz0123456789".split``,
" signal flags: <flag-signal char=x >"
);
else return "";
} else if (iso == "ics") {
if (signals)
return counter(
"FlagMeisterICSFlags",
"0123456789".split``,
" ICS signal flags: <flag-ics char=x >"
);
else return "";
} else
return (
(iso == "eu" ? header("non country flags") : "") + img(iso)
);
}).join``;
// process all injected flag
let flags = [...this.querySelectorAll("img")];
// set the correct flagcount and filesize in all text elements
let replaceHTML = (sel, x) =>
[...document.querySelectorAll(sel)].map((el) => (el.innerHTML = x));
replaceHTML(".flagmeister_flagcount", flags.length);
replaceHTML(".flagmeister_size", GZsize);
replaceHTML(
".flagmeister_SVGinfo",
`FlagMeister created <span class=highlightText>${
flags.length
} SVG flags</span>, totalling ${flags.reduce(
(totalsvg, flag) => totalsvg + flag.src.length,
0
)} hydrated bytes`
);
}, this.getAttribute("delay") || delay);
}
}
);