@discoveryjs/discovery
Version:
Frontend framework for rapid data (JSON) analysis, shareable serverless reports and dashboards
65 lines (64 loc) • 1.78 kB
JavaScript
import { hasOwn } from "./object-utils.js";
export function createElement(tag, attrs, children) {
const el = document.createElement(tag);
if (typeof attrs === "string") {
attrs = {
class: attrs
};
}
for (const attrName in attrs) {
if (hasOwn(attrs, attrName)) {
const value = attrs[attrName];
if (typeof value === "undefined") {
continue;
}
if (typeof value === "function") {
el.addEventListener(attrName.slice(2), value);
} else {
el.setAttribute(attrName, value);
}
}
}
if (Array.isArray(children)) {
el.append(...children);
} else if (typeof children === "string") {
el.innerHTML = children;
}
return el;
}
export function createText(text) {
return document.createTextNode(String(text));
}
export function isDocumentFragment(value) {
return value.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
}
export function createFragment(...children) {
const fragment = document.createDocumentFragment();
children.forEach(
(child) => fragment.appendChild(child instanceof Node ? child : createText(child))
);
return fragment;
}
export const passiveSupported = /* @__PURE__ */ (() => {
let passiveSupported2 = false;
try {
const options = {
// This function will be called when the browser
// attempts to access the passive property.
get passive() {
passiveSupported2 = true;
return false;
}
};
const cb = () => {
};
window.addEventListener("test-passive", cb, options);
window.removeEventListener("test-passive", cb);
} catch {
}
return passiveSupported2;
})();
export const passiveCaptureOptions = !passiveSupported ? true : /* @__PURE__ */ Object.freeze({
passive: true,
capture: true
});