All files / ts/components/entities/base entityParent.ts

100% Statements 23/23
100% Branches 0/0
100% Functions 9/9
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x   1x           1x 1x 1x 1x             150x 150x 150x 150x       2x       44x       1x       47x 47x 47x       1x 1x 1x 1x    
import { IParent, Parent } from "../../../models/parent.js";
import { IEntityState } from "./entityState.js";
import { EventCursor } from "../events/eventCursor.js";
 
export interface IEntityParent<T extends IEntityState> extends IParent<T> {
    invalidate(): void;
    refresh(): void;
}
export class EntityParent<T extends IEntityState> extends Parent<T> implements IEntityParent<T> {
    public static readonly invalidateEvent: string = "invalidate";
    public static readonly refreshEvent: string = "refresh";
    public static readonly changeCursorEvent: string = "changecursor";
 
    protected readonly _onInvalidate: () => void;
    protected readonly _onRefresh: () => void;
    protected readonly _onChangeCursor: (event: Event) => void;
 
    public constructor() {
        super();
        this._onInvalidate = (): void => this.invalidate();
        this._onRefresh = (): void => this.refresh();
        this._onChangeCursor = (event: Event): void => this.changeCursor((<EventCursor>event).style);
    }
 
    public invalidate(): void {
        this.dispatchEvent(new Event(EntityParent.invalidateEvent));
    }
 
    public refresh(): void {
        this.dispatchEvent(new Event(EntityParent.refreshEvent));
    }
 
    protected changeCursor(style: string): void {
        this.dispatchEvent(new EventCursor(style, EntityParent.changeCursorEvent));
    }
 
    protected initializeChild(child: T): void {
        child.addEventListener(EntityParent.invalidateEvent, this._onInvalidate);
        child.addEventListener(EntityParent.refreshEvent, this._onRefresh);
        child.addEventListener(EntityParent.changeCursorEvent, this._onChangeCursor);
    }
 
    protected override destroyChild(child: T): void {
        child.removeEventListener(EntityParent.invalidateEvent, this._onInvalidate);
        child.removeEventListener(EntityParent.refreshEvent, this._onRefresh);
        child.removeEventListener(EntityParent.changeCursorEvent, this._onChangeCursor);
        child.destroy();
    }
}