// @ts-nocheck import { isNil } from '@antv/util'; import type { Cullable, Geometry, RBushNode, Renderable, Sortable, Transform, } from '../components'; import { Strategy } from '../components/Cullable'; import { unsetKeywordValue } from '../css/CSSStyleValuePool'; import type { AABB, Rectangle } from '../shapes'; import type { BaseStyleProps, ParsedBaseStyleProps } from '../types'; import { ERROR_MSG_APPEND_DESTROYED_ELEMENT, ERROR_MSG_METHOD_NOT_IMPLEMENTED } from '../utils/error'; import { formatAttributeName, isSymbol } from '../utils/assert' import { CustomEvent } from './CustomEvent'; import type { IChildNode, ICSSStyleDeclaration, IElement, IEventTarget, INode, } from './interfaces'; import { ElementEvent } from './interfaces'; import { MutationEvent } from './MutationEvent'; import { Node } from './Node'; import { enableCSSParsing } from '../globalContants/contants'; import { sceneGraphService } from '../globalContants/sceneGrpahContants' let entityCounter = 0; export function resetEntityCounter() { entityCounter = 0; } const insertedEvent = new MutationEvent( ElementEvent.INSERTED, null, '', '', '', 0, '', '', ); const removedEvent = new MutationEvent( ElementEvent.REMOVED, null, '', '', '', 0, '', '', ); const destroyEvent = new CustomEvent(ElementEvent.DESTROY); /** * Has following capabilities: * * Node insert/remove, eg. appendChild, removeChild, remove... * * Query eg. querySelector getElementById... * * Animation */ export class Element< StyleProps extends BaseStyleProps = any, ParsedStyleProps extends ParsedBaseStyleProps = any, > extends Node implements IElement { static isElement( target: IEventTarget | INode | IElement, ): target is IElement { return !!(target as IElement).getAttribute; } /** * Unique id. */ entity = entityCounter++; renderable: Renderable = { bounds: undefined, boundsDirty: true, renderBounds: undefined, renderBoundsDirty: true, dirtyRenderBounds: undefined, dirty: false, proxyNodeName: undefined, }; cullable: Cullable = { strategy: Strategy.Standard, visibilityPlaneMask: -1, visible: true, enable: true, }; transformable: Transform = { dirtyFlag: false, localDirtyFlag: false, frozen: false, // localPosition: vec3.fromValues(0, 0, 0), // localRotation: quat.fromValues(0, 0, 0, 1), // localScale: vec3.fromValues(1, 1, 1), // localTransform: mat4.create(), // localSkew: vec2.fromValues(0, 0), // position: vec3.fromValues(0, 0, 0), // rotation: quat.fromValues(0, 0, 0, 1), // scaling: vec3.fromValues(1, 1, 1), // worldTransform: mat4.create(), // origin: vec3.fromValues(0, 0, 0), localPosition: [0, 0, 0], localRotation: [0, 0, 0, 1], localScale: [1, 1, 1], localTransform: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], localSkew: [0, 0], position: [0, 0, 0], rotation: [0, 0, 0, 1], scaling: [1, 1, 1], worldTransform: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], origin: [0, 0, 0], }; sortable: Sortable = { dirty: false, sorted: undefined, lastSortedIndex: undefined, renderOrder: 0, }; geometry: Geometry = { contentBounds: undefined, renderBounds: undefined, }; rBushNode: RBushNode = { aabb: undefined, }; /** * used with `getElementById()` * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/id */ id: string; /** * used in `getElementsByClassName` * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName */ get className() { // @ts-ignore return this.getAttribute('class') || ''; } set className(className: string) { this.setAttribute('class', className); } /** * used in `getElementsByName` * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName */ name: string; /** * https://developer.mozilla.org/zh-CN/docs/Web/API/Element/namespaceURI */ namespaceURI = 'g'; /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/classList */ get classList() { return this.className.split(' ').filter((c) => c !== ''); } scrollLeft = 0; scrollTop = 0; /** * We don't support border now * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/clientTop */ clientTop = 0; clientLeft = 0; get tagName(): string { return this.nodeName; } get children(): IElement[] { return this.childNodes as IElement[]; } get childElementCount(): number { return this.childNodes.length; } get firstElementChild(): IElement | null { return this.firstChild as IElement; } get lastElementChild(): IElement | null { return this.lastChild as IElement; } get parentElement(): IElement | null { return this.parentNode as IElement; } get nextSibling(): IElement | null { if (this.parentNode) { const index = this.parentNode.childNodes.indexOf(this); return (this.parentNode.childNodes[index + 1] as IElement) || null; } return null; } get previousSibling(): IElement | null { if (this.parentNode) { const index = this.parentNode.childNodes.indexOf(this); return (this.parentNode.childNodes[index - 1] as IElement) || null; } return null; } cloneNode(deep?: boolean): this { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } appendChild(child: T, index?: number): T { if ((child as unknown as Element).destroyed) { throw new Error(ERROR_MSG_APPEND_DESTROYED_ELEMENT); } sceneGraphService.attach(child, this, index); if (this.ownerDocument?.defaultView) { this.ownerDocument.defaultView.mountChildren(child); } insertedEvent.relatedNode = this as IElement; child.dispatchEvent(insertedEvent); return child; } insertBefore(newChild: T, refChild: INode | null): T { if (!refChild) { this.appendChild(newChild); } else { const index = this.childNodes.indexOf(refChild as IChildNode); this.appendChild(newChild, index - 1); } return newChild; } replaceChild(newChild: INode, oldChild: T): T { const index = this.childNodes.indexOf(oldChild as unknown as IChildNode); this.removeChild(oldChild); this.appendChild(newChild, index); return oldChild; } removeChild(child: T): T { // should emit on itself before detach removedEvent.relatedNode = this as IElement; child.dispatchEvent(removedEvent); if (child.ownerDocument?.defaultView) { child.ownerDocument.defaultView.unmountChildren(child); } // remove from scene graph sceneGraphService.detach(child); return child; } /** * Remove all children which can be appended to its original parent later again. */ removeChildren() { for (let i = this.childNodes.length - 1; i >= 0; i--) { const child = this.childNodes[i] as this; this.removeChild(child); } } /** * Recursively destroy all children which can not be appended to its original parent later again. */ destroyChildren() { for (let i = this.childNodes.length - 1; i >= 0; i--) { const child = this.childNodes[i] as this; if (child.childNodes.length) { child.destroyChildren(); } child.destroy(); } } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/matches */ matches(selector: string): boolean { return sceneGraphService.matches(selector, this as IElement); } getElementById(id: string): E | null { return sceneGraphService.querySelector( `#${id}`, this as IElement, ); } getElementsByName(name: string): E[] { return sceneGraphService.querySelectorAll( `[name="${name}"]`, this as IElement, ); } getElementsByClassName( className: string, ): E[] { return sceneGraphService.querySelectorAll( `.${className}`, this as IElement, ); } getElementsByTagName(tagName: string): E[] { return sceneGraphService.querySelectorAll( tagName, this as IElement, ); } querySelector(selectors: string): E | null { return sceneGraphService.querySelector( selectors, this as IElement, ); } querySelectorAll(selectors: string): E[] { return sceneGraphService.querySelectorAll( selectors, this as IElement, ); } /** * should traverses the element and its parents (heading toward the document root) * until it finds a node that matches the specified CSS selector. * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/closest * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#polyfill */ closest(selectors: string): E | null { let el = this as unknown as E; do { if (sceneGraphService.matches(selectors, el)) return el; el = el.parentElement as E; } while (el !== null); return null; } /** * search in scene group, but should not include itself */ find(filter: (node: E) => boolean): E | null { let target: E | null = null; this.forEach((object) => { if (object !== this && filter(object as E)) { target = object as E; return true; } return false; }); return target; } findAll(filter: (node: E) => boolean): E[] { const objects: E[] = []; this.forEach((object) => { if (object !== this && filter(object as E)) { objects.push(object as E); } }); return objects; } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/after */ after(...nodes: INode[]) { if (this.parentNode) { const index = this.parentNode.childNodes.indexOf(this); nodes.forEach((node, i) => this.parentNode?.appendChild(node!, index + i + 1), ); } } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/before */ before(...nodes: INode[]) { if (this.parentNode) { const index = this.parentNode.childNodes.indexOf(this); const [first, ...rest] = nodes; this.parentNode.appendChild(first!, index); (first as IChildNode).after(...rest); } } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/replaceWith */ replaceWith(...nodes: INode[]) { this.after(...nodes); this.remove(); } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/append */ append(...nodes: INode[]) { nodes.forEach((node) => this.appendChild(node)); } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/prepend */ prepend(...nodes: INode[]) { nodes.forEach((node, i) => this.appendChild(node, i)); } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/replaceChildren */ replaceChildren(...nodes: INode[]) { while (this.childNodes.length && this.firstChild) { this.removeChild(this.firstChild); } this.append(...nodes); } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/remove */ remove(): this { if (this.parentNode) { return this.parentNode.removeChild(this); } return this; } /** * is destroyed or not */ destroyed = false; destroy() { // destroy itself before remove this.dispatchEvent(destroyEvent); // remove from scenegraph first this.remove(); // remove event listeners this.emitter.removeAllListeners(); this.destroyed = true; } getGeometryBounds(): AABB { return sceneGraphService.getGeometryBounds(this); } getRenderBounds(): AABB { return sceneGraphService.getBounds(this, true); } /** * get bounds in world space, account for children */ getBounds(): AABB { return sceneGraphService.getBounds(this); } /** * get bounds in local space, account for children */ getLocalBounds(): AABB { return sceneGraphService.getLocalBounds(this); } /** * account for context's bounds in client space, * but not accounting for children * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect */ getBoundingClientRect(): Rectangle { return sceneGraphService.getBoundingClientRect(this); } /** * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getClientRects */ getClientRects() { return [this.getBoundingClientRect()]; } /** * compatible with `style` * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style */ style: StyleProps & ICSSStyleDeclaration = {} as StyleProps & ICSSStyleDeclaration; computedStyle: any = enableCSSParsing ? { anchor: unsetKeywordValue, opacity: unsetKeywordValue, fillOpacity: unsetKeywordValue, strokeOpacity: unsetKeywordValue, fill: unsetKeywordValue, stroke: unsetKeywordValue, transform: unsetKeywordValue, transformOrigin: unsetKeywordValue, visibility: unsetKeywordValue, pointerEvents: unsetKeywordValue, lineWidth: unsetKeywordValue, lineCap: unsetKeywordValue, lineJoin: unsetKeywordValue, increasedLineWidthForHitTesting: unsetKeywordValue, fontSize: unsetKeywordValue, fontFamily: unsetKeywordValue, fontStyle: unsetKeywordValue, fontWeight: unsetKeywordValue, fontVariant: unsetKeywordValue, textAlign: unsetKeywordValue, textBaseline: unsetKeywordValue, textTransform: unsetKeywordValue, zIndex: unsetKeywordValue, filter: unsetKeywordValue, shadowType: unsetKeywordValue, } : null; /** * Renderers will use these used values. */ parsedStyle: ParsedStyleProps = { // opacity: '', // fillOpacity: '', // strokeOpacity: '', // transformOrigin: '', // visibility: '', // pointerEvents: '', // lineWidth: '', // lineCap: '', // lineJoin: '', // increasedLineWidthForHitTesting: '', // fontSize: '', // fontFamily: '', // fontStyle: '', // fontWeight: '', // fontVariant: '', // textAlign: '', // textBaseline: '', // textTransform: '', } as ParsedStyleProps; /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/computedStyleMap * eg. circle.computedStyleMap().get('fill'); */ computedStyleMap() { return new Map(Object.entries(this.computedStyle)); } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes */ readonly attributes: StyleProps = {} as StyleProps; /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttributeNames */ getAttributeNames(): string[] { return Object.keys(this.attributes); } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute */ getAttribute(name: keyof StyleProps) { // @see https://github.com/antvis/G/issues/1267 if (isSymbol(name)) { return enableCSSParsing ? null : undefined; } let value = this.attributes[name]; if (value === undefined) { const attributeName = formatAttributeName(name as string); value = this.attributes[attributeName]; // if the given attribute does not exist, the value returned will either be null or "" return enableCSSParsing ? (isNil(value) ? null : value) : value; } else { return value; } } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute */ hasAttribute(qualifiedName: string): boolean { return this.getAttributeNames().includes(qualifiedName); } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttributes */ hasAttributes(): boolean { return !!this.getAttributeNames().length; } /** * should use removeAttribute() instead of setting the attribute value to null either directly or using setAttribute(). Many attributes will not behave as expected if you set them to null. * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute */ removeAttribute(attributeName: keyof StyleProps) { this.setAttribute(attributeName, null); delete this.attributes[attributeName]; } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute */ setAttribute( attributeName: Key, value: StyleProps[Key], force = false, ) { this.attributes[attributeName] = value; } getAttributeNS(namespace: string, localName: string): string { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } getAttributeNode(qualifiedName: string): Attr { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } getAttributeNodeNS(namespace: string, localName: string): Attr { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } hasAttributeNS(namespace: string, localName: string): boolean { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } removeAttributeNS(namespace: string, localName: string): void { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } removeAttributeNode(attr: Attr): Attr { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } setAttributeNS( namespace: string, qualifiedName: string, value: string, ): void { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } setAttributeNode(attr: Attr): Attr { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } setAttributeNodeNS(attr: Attr): Attr { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } toggleAttribute(qualifiedName: string, force?: boolean): boolean { throw new Error(ERROR_MSG_METHOD_NOT_IMPLEMENTED); } }