40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
const svgns = 'http://www.w3.org/2000/svg';
|
|
|
|
function createUse(type, id) {
|
|
const c = typeof consts !== 'undefined' ? consts : {};
|
|
const path = c.path || '';
|
|
const ver = c.resver == null ? '' : `?${c.resver}`;
|
|
const use = document.createElementNS(svgns, 'use');
|
|
use.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', `${path}fonts/${type}.svg${ver}#${id}`);
|
|
return use;
|
|
}
|
|
|
|
export function changeIcon(svg, type, id) {
|
|
if (svg instanceof SVGElement) {
|
|
svg.replaceChildren(createUse(type, id));
|
|
}
|
|
return svg;
|
|
}
|
|
|
|
export function createIcon(type, id, style) {
|
|
const svg = document.createElementNS(svgns, 'svg');
|
|
svg.appendChild(createUse(type, id));
|
|
if (style != null) {
|
|
for (let css of Object.entries(style)) {
|
|
svg.style.setProperty(css[0], css[1]);
|
|
}
|
|
}
|
|
return svg;
|
|
}
|
|
|
|
export function resolveIcon(container) {
|
|
const svgs = container.querySelectorAll('svg[data-id]');
|
|
for (let icon of svgs) {
|
|
const type = icon.dataset.type;
|
|
const id = icon.dataset.id;
|
|
icon.replaceChildren(createUse(type, id));
|
|
icon.removeAttribute('data-type');
|
|
icon.removeAttribute('data-id');
|
|
}
|
|
return container;
|
|
} |