2024-10-17 11:54:28 +08:00
|
|
|
import { $EventTarget } from "../$EventTarget";
|
|
|
|
import { $, $Element, $EventManager, $State, $HTMLElement, $Container } from "../../index";
|
2024-02-01 23:47:13 +08:00
|
|
|
|
2024-10-17 11:54:28 +08:00
|
|
|
export abstract class $Node<N extends Node = Node, $EM extends $NodeEventMap = $NodeEventMap, EM extends GlobalEventHandlersEventMap = GlobalEventHandlersEventMap> extends $EventTarget<$EM, EM> {
|
2024-02-01 23:47:13 +08:00
|
|
|
abstract readonly dom: N;
|
2024-10-17 11:54:28 +08:00
|
|
|
protected __$property__ = {
|
|
|
|
hidden: false,
|
|
|
|
coordinate: undefined as $NodeCoordinate | undefined
|
2024-02-03 09:33:34 +08:00
|
|
|
}
|
2024-10-17 11:54:28 +08:00
|
|
|
readonly parent?: $Container;
|
2024-02-03 09:33:34 +08:00
|
|
|
|
2024-02-13 19:38:46 +08:00
|
|
|
hide(): boolean;
|
2024-04-24 20:57:01 +08:00
|
|
|
hide(hide?: boolean | $State<boolean>, render?: boolean): this;
|
2024-10-17 11:54:28 +08:00
|
|
|
hide(hide?: boolean | $State<boolean>, render = true) { return $.fluent(this, arguments, () => this.__$property__.hidden, () => {
|
2024-02-13 19:38:46 +08:00
|
|
|
if (hide === undefined) return;
|
2024-10-17 11:54:28 +08:00
|
|
|
if (hide instanceof $State) { this.__$property__.hidden = hide.value; hide.use(this, 'hide')}
|
|
|
|
else this.__$property__.hidden = hide;
|
2024-04-24 20:57:01 +08:00
|
|
|
if (render) this.parent?.children.render();
|
2024-02-03 09:33:34 +08:00
|
|
|
return this;
|
2024-02-13 19:38:46 +08:00
|
|
|
})}
|
|
|
|
|
|
|
|
/**Remove this element from parent */
|
|
|
|
remove() {
|
|
|
|
this.parent?.children.remove(this).render();
|
|
|
|
return this;
|
2024-02-03 09:33:34 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 19:38:46 +08:00
|
|
|
/**Replace $Node with this element */
|
|
|
|
replace($node: $Node) {
|
|
|
|
this.parent?.children.replace(this, $node).render();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-04-23 18:18:43 +08:00
|
|
|
contains(target: $Node | EventTarget | Node | null): boolean {
|
2024-02-13 19:38:46 +08:00
|
|
|
if (!target) return false;
|
2024-02-03 09:33:34 +08:00
|
|
|
if (target instanceof $Node) return this.dom.contains(target.dom);
|
|
|
|
else if (target instanceof EventTarget) return this.dom.contains($(target).dom)
|
|
|
|
else return this.dom.contains(target)
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:54:28 +08:00
|
|
|
coordinate(): $NodeCoordinate | undefined;
|
|
|
|
coordinate(coordinate: $NodeCoordinate): this;
|
|
|
|
coordinate(coordinate?: $NodeCoordinate) { return $.fluent(this, arguments, () => this.__$property__.coordinate, () => $.set(this.__$property__, 'coordinate', coordinate))}
|
|
|
|
|
|
|
|
self(callback: OrArray<($node: this) => void>) { $.orArrayResolve(callback).forEach(fn => fn(this)); return this; }
|
2024-02-13 19:38:46 +08:00
|
|
|
inDOM() { return document.contains(this.dom); }
|
2024-10-17 11:54:28 +08:00
|
|
|
|
2024-04-24 19:02:36 +08:00
|
|
|
isElement(): this is $Element {
|
|
|
|
if (this instanceof $Element) return true;
|
|
|
|
else return false;
|
2024-03-16 13:33:05 +08:00
|
|
|
}
|
2024-10-03 23:21:55 +08:00
|
|
|
get element(): $Element | null {
|
|
|
|
if (this instanceof $Element) return this;
|
|
|
|
else return null;
|
|
|
|
}
|
2024-10-17 11:54:28 +08:00
|
|
|
get htmlElement(): $HTMLElement | null {
|
|
|
|
if (this instanceof $HTMLElement) return this;
|
|
|
|
else return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface $NodeCoordinate {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
height: number;
|
|
|
|
width: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface $NodeEventMap {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type $HTMLElementEventMap<$N> = {
|
|
|
|
[keys in keyof HTMLElementEventMap]: [event: HTMLElementEventMap[keys], $this: $N];
|
|
|
|
};
|