redom
Version:
Tiny turboboosted JavaScript library for creating user interfaces.
95 lines (84 loc) • 2.25 kB
JavaScript
/* global SVGElement */
import { setStyle } from "./setstyle.js";
import { getEl } from "./util.js";
const xlinkns = "http://www.w3.org/1999/xlink";
export function setAttr(view, arg1, arg2) {
setAttrInternal(view, arg1, arg2);
}
export function setAttrInternal(view, arg1, arg2, initial) {
const el = getEl(view);
const isObj = typeof arg1 === "object";
if (isObj) {
for (const key in arg1) {
setAttrInternal(el, key, arg1[key], initial);
}
} else {
const isSVG = el instanceof SVGElement;
const isFunc = typeof arg2 === "function";
if (arg1 === "style" && typeof arg2 === "object") {
setStyle(el, arg2);
} else if (isSVG && isFunc) {
el[arg1] = arg2;
} else if (arg1 === "dataset") {
setData(el, arg2);
} else if (!isSVG && (arg1 in el || isFunc) && arg1 !== "list") {
el[arg1] = arg2;
} else {
if (isSVG && arg1 === "xlink") {
setXlink(el, arg2);
return;
}
if (initial && arg1 === "class") {
setClassName(el, arg2);
return;
}
if (arg2 == null) {
el.removeAttribute(arg1);
} else {
el.setAttribute(arg1, arg2);
}
}
}
}
function setClassName(el, additionToClassName) {
if (additionToClassName == null) {
el.removeAttribute("class");
} else if (el.classList) {
el.classList.add(additionToClassName);
} else if (
typeof el.className === "object" &&
el.className &&
el.className.baseVal
) {
el.className.baseVal =
`${el.className.baseVal} ${additionToClassName}`.trim();
} else {
el.className = `${el.className} ${additionToClassName}`.trim();
}
}
export function setXlink(el, arg1, arg2) {
if (typeof arg1 === "object") {
for (const key in arg1) {
setXlink(el, key, arg1[key]);
}
} else {
if (arg2 != null) {
el.setAttributeNS(xlinkns, arg1, arg2);
} else {
el.removeAttributeNS(xlinkns, arg1, arg2);
}
}
}
export function setData(el, arg1, arg2) {
if (typeof arg1 === "object") {
for (const key in arg1) {
setData(el, key, arg1[key]);
}
} else {
if (arg2 != null) {
el.dataset[arg1] = arg2;
} else {
delete el.dataset[arg1];
}
}
}