function createHelpLayer(rootElement, helpData, blocking = true) {
let [width, height] = [rootElement.clientWidth, rootElement.clientHeight];
let layer = html`<div class="helpLayer">`;
if (!blocking) layer.style.pointerEvents = "none";
Object.assign(layer.style, {
position: "absolute",
top: "0px",
left: "0px",
minWidth: `${width}px`,
minHeight: `${height}px`,
zIndex: 1000,
background: "rgba(0,0,0,0)"
});
const helpGeometry = function (helpItem, layerGeom) {
const iconSize = 18;
const boxWidth = helpBoxWidth;
const sep = 5;
let [xoff, yoff] = [+helpItem.xoffset, +helpItem.yoffset];
let icon = {};
let box = measure(boxWidth, helpItem.text);
if (helpItem.corner.includes("N")) {
icon.y = yoff;
box.y = yoff + iconSize + sep;
} else if (helpItem.corner.includes("S")) {
icon.y = layerGeom.height + yoff - iconSize;
box.y = icon.y - sep - box.height;
} else {
icon.y = layerGeom.height / 2 + yoff - iconSize / 2;
box.y = icon.y + iconSize / 2 - box.height / 2;
}
if (helpItem.corner.includes("W")) {
icon.x = xoff;
box.x = icon.x + sep + iconSize;
} else if (helpItem.corner.includes("E")) {
icon.x = layerGeom.width + xoff - iconSize;
box.x = icon.x - sep - box.width;
} else {
icon.x = layerGeom.width / 2 + xoff - iconSize / 2;
box.x = icon.x - box.width / 2 + iconSize / 2;
if (!helpItem.corner.includes("S") && !helpItem.corner.includes("N")) {
icon.y -= box.height / 4 + iconSize / 2 + sep;
box.y += box.height / 4;
}
}
return { icon, box };
};
let helpBoxes = [];
const showHelpBox = (box) => {
for (let hb of helpBoxes) {
if (hb === box) {
hb.style.display = hb.style.display == "block" ? "none" : "block";
} else {
hb.style.display = "none";
}
}
};
for (let helpItem of helpData) {
const node = rootElement.querySelector(helpItem.selector);
if (!node) {
console.log("Can't find:" + helpItem.selector);
continue;
}
//const rect = node.BoundingClientRect();
const geom = {
x: node.offsetLeft,
y: node.offsetTop,
width: node.clientWidth,
height: node.clientHeight
};
if (geom.x == 0 && geom.y == 0 && geom.width == 0 && geom.height == 0) {
console.log("Element with null geometry:" + helpItem.selector);
continue;
}
const panel = html`<div class="helpPanel">`;
Object.assign(panel.style, {
position: "absolute",
top: `${geom.y}px`,
left: `${geom.x}px`,
minWidth: `${geom.width}px`,
minHeight: `${geom.height}px`,
pointerEvents: "none"
// border: "1px solid blue",
// background: "rgba(0,0,0,0.1)"
});
const helpGeom = helpGeometry(helpItem, geom);
const icon = helpIcon();
Object.assign(icon.style, {
position: "absolute",
top: `${helpGeom.icon.y}px`,
left: `${helpGeom.icon.x}px`,
pointerEvents: "auto"
});
const helpBox = htl.html`<div class=helpBox>${helpItem.text}`;
Object.assign(helpBox.style, {
display: "none",
position: "absolute",
pointerEvents: "none",
top: `${helpGeom.box.y}px`,
left: `${helpGeom.box.x}px`,
width: `${helpGeom.box.width}px`
});
helpBoxes.push(helpBox);
icon.onclick = () => showHelpBox(helpBox);
panel.append(icon, helpBox);
layer.append(panel);
}
return layer;
}