UNPKG

@adaptabletools/adaptable-cjs

Version:

Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

48 lines (47 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveContainerElement = resolveContainerElement; /** * Resolves a container option value to an `HTMLElement`. * * Resolution order: * 1. If `container` is `null`/`undefined`, returns `null`. * 2. If `container` is a function, it is called with the provided `context` and the result is resolved. * 3. If the value is an `HTMLElement`, it is returned directly. * 4. If the value is an `AdaptableCSSSelector` (`{ selector: string }`), `document.querySelectorAll` is used. * If multiple elements match, the first is returned and a console warning is logged. * 5. If the value is a `string`, it is treated as an element ID and `document.getElementById` is used. * * @param container - The container reference to resolve. * @param context - Optional context passed to the function form of the container. * @param doc - The document to query against (defaults to `globalThis.document`). * @returns The resolved `HTMLElement`, or `null` if not found. */ function resolveContainerElement(container, context, doc = globalThis.document) { if (container == null) { return null; } // Unwrap function form — pass context if the container is a function const value = typeof container === 'function' ? container(context) : container; // Guard against null/undefined returned by a callback (e.g. document.getElementById(...)) if (value == null) { return null; } // Direct HTMLElement reference if (value instanceof HTMLElement) { return value; } // CSS Selector object if (typeof value === 'object' && 'selector' in value) { const matches = doc.querySelectorAll(value.selector); if (matches.length > 1) { console.warn(`[AdapTable] CSS selector "${value.selector}" matched ${matches.length} elements. Using the first match. Consider using a more specific selector.`); } return matches[0] ?? null; } // String element ID if (typeof value === 'string') { return doc.getElementById(value); } return null; }