@discoveryjs/discovery
Version:
Frontend framework for rapid data (JSON) analysis, shareable serverless reports and dashboards
108 lines (107 loc) • 3.5 kB
JavaScript
import { createElement } from "../../core/utils/dom.js";
import { numDelim } from "../../core/utils/html.js";
const styles = ["none", "default", "badge"];
const annotationsElByEl = /* @__PURE__ */ new WeakMap();
export function concatAnnotations(a, b) {
if (Array.isArray(a)) {
return Array.isArray(b) ? a.concat(b) : a;
}
return b;
}
export function preprocessAnnotations(annotations) {
if (Array.isArray(annotations) && annotations.length > 0) {
return annotations.map(
(annotation) => typeof annotation === "string" || typeof annotation === "function" ? { query: annotation } : annotation
);
}
return false;
}
export function getDefaultAnnotations(host) {
return preprocessAnnotations([...host.objectMarkers.values].map(
({ name, lookup }) => (value, context) => {
const marker = lookup(value, true);
if (marker !== null && marker.object !== context.host) {
return {
place: "before",
style: "badge",
text: name,
href: marker.href
};
}
}
));
}
export function prepareAnnotations(viewAnnotations, customAnnotations) {
return concatAnnotations(customAnnotations, preprocessAnnotations(viewAnnotations));
}
function isValidStyle(value) {
return styles.includes(value);
}
export function renderAnnotations(annotations) {
const renderUntil = Date.now() + 8;
let i = 0;
for (; i < annotations.length; i++) {
if (Date.now() > renderUntil) {
break;
}
const { el, config, renderer, data, context } = annotations[i];
const {
place = "after",
className,
text: _text,
icon,
href,
external,
tooltip
} = config;
const style = isValidStyle(config.style) ? config.style : place === "before" ? "none" : "default";
const text = _text === void 0 ? typeof config !== "object" ? String(config) : "" : String(_text);
const hasText = text !== "";
const elClassName = [
"value-annotation",
"style-" + style,
place === "before" ? "before" : "after",
hasText ? "has-text" : "",
className || ""
].join(" ");
const annotationEl = createElement(href ? "a" : "span", {
class: elClassName,
href: typeof href === "string" ? href : void 0,
target: external ? "_blank" : void 0
});
if (hasText) {
if (/\d{4}/.test(text)) {
annotationEl.innerHTML = numDelim(text);
} else {
annotationEl.append(text);
}
}
if (typeof icon === "string") {
annotationEl.classList.add("icon");
if (/^[a-z_$][a-z0-9_$-]*$/i.test(icon)) {
annotationEl.classList.add("icon-" + icon);
} else {
annotationEl.style.setProperty("--annotation-image", `url("${icon}")`);
}
}
if (tooltip) {
renderer.tooltip(annotationEl, tooltip, data, { ...context, config });
}
if (place === "before") {
el.before(annotationEl);
} else {
const parentEl = el.parentNode;
let annotationsEl = annotationsElByEl.get(el);
if (annotationsEl === void 0) {
annotationsElByEl.set(el, annotationsEl = createElement("span", "value-annotations"));
if (parentEl.classList.contains("struct-expanded-value")) {
el.querySelector(":scope > .value-size, :scope > .string-length")?.after(annotationsEl);
} else {
parentEl.append(annotationsEl);
}
}
annotationsEl.append(annotationEl);
}
}
annotations.splice(0, i);
}