import 'core-js/fn/weak-map'; import FlexPointList from './FlexPointList'; import { DOMSelector } from './types'; const NS_SVG = 'http://www.w3.org/2000/svg'; const ATTRIBUTE_NAME = 'data-polycon-node'; const STYLE_NODE_NAME = 'style'; const ROOT_NODE_NAME = 'root'; const BACKGROUND_NODE_NAME = 'background'; const STYLE_SELECTOR = `[${ATTRIBUTE_NAME}="${STYLE_NODE_NAME}"]`; const ROOT_SELECTOR = `[${ATTRIBUTE_NAME}="${ROOT_NODE_NAME}"]`; const BACKGROUND_SELECTOR = `[${ATTRIBUTE_NAME}="${BACKGROUND_NODE_NAME}"]`; const SVG_LENGTHTYPE_PX = SVGLength.SVG_LENGTHTYPE_PX; const rootMap: WeakMap = new WeakMap(); const svgMap: WeakMap = new WeakMap(); const polygonMap: WeakMap = new WeakMap(); export default class Polycon { /** * Create a new instance or instance list * * @param selector A selector string or an element or element List */ public static new (selector: DOMSelector): Polycon | Polycon[] { let nodeList: NodeListOf; if (selector instanceof Node) { return new Polycon(selector); } else if (typeof selector === 'string') { nodeList = document.querySelectorAll(selector); } else if (selector instanceof NodeList) { nodeList = selector; } else { throw new TypeError(`Invalid argument type`); } const polycons: Polycon[] = []; for (const el of nodeList) { polycons.push(new Polycon(el)); } return polycons; } /** * Create string of UUID */ private static _createUUID (): string { return Math.round(Date.now() * Math.random()).toString(36); } /** * Get path of background image from an element */ private static _getBackgroundImagePath (el: HTMLElement): string { const style = window.getComputedStyle(el); const styleValue = style.getPropertyValue('background-image'); if (!styleValue) { return ''; } const matchArray = styleValue.match(/url\(("|')?([^\)]*)\1\)/); if (!matchArray) { return ''; } const [ , , path] = matchArray; return path || ''; } /** * Get color of background from an element */ private static _getBackgroundColor (el: HTMLElement): string { const style = window.getComputedStyle(el); const colorCode = style.getPropertyValue('background-color'); return colorCode || ''; } /** * Create a `