-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-demo.js
More file actions
35 lines (29 loc) · 1.05 KB
/
template-demo.js
File metadata and controls
35 lines (29 loc) · 1.05 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
// find the "spriteName" from the URL
var fileName = window.location.pathname.split("/").slice(-1)[0];
var spriteName = fileName.replace("-demo.html", "");
// add the
document.addEventListener("DOMContentLoaded", async function (event) {
const url = spriteName + ".svg";
const svgContent = await (await fetch(url)).text();
const xml = (new DOMParser()).parseFromString(svgContent, "application/xml");
const symbols = Array.from(xml.querySelectorAll("symbol")).map(sym => ({ name: sym.getAttribute("id") }));
// do after the symbols above as the firstElementChild will be moved
document.querySelector("head").appendChild(xml.firstElementChild);
document.querySelector("#icons-ctn").innerHTML = renderSymbols(symbols);
});
function renderSymbols(symbols) {
let htmlContent = '';
for (const { name } of symbols) {
htmlContent += `
<div class="icon-card">
<div class="icon">
<svg class="svg-ico">
<use xlink:href="#${name}"></use>
</svg>
</div>
<label>${name}</label>
</div>
`
}
return htmlContent;
}