maplair
Version:
JavaScript library for mobile-friendly interactive maps
164 lines (163 loc) • 5.55 kB
JavaScript
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
import { Point } from "../geometry/Point.js";
import { off, on, preventDefault } from "./DomEvent.js";
// src/dom/DomUtil.js
var DomUtil_exports = /* @__PURE__ */ __exportAll({
create: () => create,
disableImageDrag: () => disableImageDrag,
enableImageDrag: () => enableImageDrag,
get: () => get,
getPosition: () => getPosition,
getScale: () => getScale,
getSizedParentNode: () => getSizedParentNode,
isHTML: () => isHTML,
setPosition: () => setPosition,
setTransform: () => setTransform,
toBack: () => toBack,
toFront: () => toFront
});
/**
* Returns an element given its DOM id, or returns the element itself
* if it was passed directly.
* @param {string|HTMLElement} id
* @returns {HTMLElement|null}
*/
function get(id) {
return typeof id === "string" ? document.getElementById(id) : id;
}
/**
* Creates an HTML element with `tagName`, sets its class to `className`, and
* optionally appends it to `container` element.
* @param {string} tagName
* @param {string} [className]
* @param {HTMLElement} [container]
* @returns {HTMLElement}
*/
function create(tagName, className, container) {
const el = document.createElement(tagName);
el.className = className ?? "";
container?.append(el);
return el;
}
/**
* Returns `true` if `value` is HTML to set as content: a string, or a `TrustedHTML`
* object of a [Trusted Types](https://developer.mozilla.org/docs/Web/API/Trusted_Types_API)
* policy, which pages enforcing Trusted Types require.
* @param {unknown} value
* @returns {boolean}
*/
function isHTML(value) {
return typeof value === "string" || typeof TrustedHTML !== "undefined" && value instanceof TrustedHTML;
}
function moveBefore(parent, node, ref) {
if (parent.moveBefore) try {
parent.moveBefore(node, ref);
return;
} catch {}
parent.insertBefore(node, ref);
}
/**
* Makes `el` the last child of its parent, so it renders in front of the other
* children.
* @param {Element} el
* @returns {void}
*/
function toFront(el) {
const parent = el.parentNode;
if (!parent || parent.lastChild === el) return;
if (el.matches(":hover")) while (el.nextSibling) moveBefore(parent, el.nextSibling, el);
else moveBefore(parent, el, null);
}
/**
* Makes `el` the first child of its parent, so it renders behind the other children.
* @param {Element} el
* @returns {void}
*/
function toBack(el) {
const parent = el.parentNode;
if (!parent || parent.firstChild === el) return;
if (el.matches(":hover")) while (el.previousSibling) moveBefore(parent, el.previousSibling, el.nextSibling);
else moveBefore(parent, el, parent.firstChild);
}
/**
* Resets the 3D CSS transform of `el` so it is translated by `offset` pixels
* and optionally scaled by `scale`. Does not have an effect if the
* browser doesn't support 3D CSS transforms.
* @param {HTMLElement|SVGElement} el
* @param {Point} [offset]
* @param {number} [scale]
* @returns {void}
*/
function setTransform(el, offset, scale) {
const pos = offset ?? new Point(0, 0);
const transform = `translate3d(${pos.x}px, ${pos.y}px, 0px)${scale ? ` scale(${scale})` : ""}`;
if (el.style.transform !== transform) el.style.transform = transform;
}
const positions = /* @__PURE__ */ new WeakMap();
/**
* Sets the position of `el` to coordinates specified by `point` using a CSS translate
* (used by Maplair internally to position its layers). With `flat`, the translate is 2D,
* so `el` is painted into its parent's compositor layer instead of getting a layer of its own.
* @param {HTMLElement|SVGElement} el
* @param {Point} point
* @param {boolean} [flat]
* @returns {void}
*/
function setPosition(el, point, flat) {
positions.set(el, point);
if (flat) el.style.transform = `translate(${point.x}px, ${point.y}px)`;
else setTransform(el, point);
}
/**
* Returns the coordinates of an element previously positioned with setPosition.
* @param {HTMLElement|SVGElement} el
* @returns {Point}
*/
function getPosition(el) {
return positions.get(el) ?? new Point(0, 0);
}
/**
* Prevents the user from generating `dragstart` DOM events, usually generated when
* the user drags an image.
* @returns {void}
*/
function disableImageDrag() {
on(window, "dragstart", preventDefault);
}
/**
* Cancels the effects of a previous
* [`DomUtil.disableImageDrag`](#domutil-disableimagedrag).
* @returns {void}
*/
function enableImageDrag() {
off(window, "dragstart", preventDefault);
}
/**
* Finds the closest parent node which size (width and height) is not null.
* @param {HTMLElement} element
* @returns {HTMLElement}
*/
function getSizedParentNode(element) {
do
element = element.parentNode;
while ((!element.offsetWidth || !element.offsetHeight) && element !== document.body);
return element;
}
/**
* Computes the CSS scale currently applied on the element.
* Returns an object with `x` and `y` members as horizontal and vertical scales
* respectively, and `boundingClientRect` as the result of
* [`getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect).
* @param {HTMLElement} element
* @returns {{x: number, y: number, boundingClientRect: DOMRect}}
*/
function getScale(element) {
const rect = element.getBoundingClientRect();
return {
x: rect.width / element.offsetWidth || 1,
y: rect.height / element.offsetHeight || 1,
boundingClientRect: rect
};
}
export { DomUtil_exports, create, disableImageDrag, enableImageDrag, get, getPosition, getScale, getSizedParentNode, isHTML, setPosition, setTransform, toBack, toFront };
//# sourceMappingURL=DomUtil.js.map