1 |
|
2 | import { onEventType } from "./helpers/onEvent";
|
3 |
|
4 | export default abstract class WUPBaseElement<TOptions extends Record<string, any> = Record<string, any>, Events extends WUP.Base.EventMap = WUP.Base.EventMap> extends HTMLElement {
|
5 | #private;
|
6 |
|
7 | static get $refStyle(): HTMLStyleElement | null;
|
8 | static set $refStyle(v: HTMLStyleElement | null);
|
9 |
|
10 | static get $style(): string;
|
11 |
|
12 | static get $styleRoot(): string;
|
13 |
|
14 | static get $uniqueId(): string;
|
15 | static get classNameHidden(): string;
|
16 | static get observedOptions(): Array<string> | undefined;
|
17 | static get observedAttributes(): Array<string> | undefined;
|
18 |
|
19 | static $defaults: Record<string, any>;
|
20 |
|
21 | protected _opts: TOptions;
|
22 | get $options(): TOptions;
|
23 |
|
24 | set $options(v: TOptions);
|
25 | static findAllProtos(t: unknown, protos: typeof WUPBaseElement[]): typeof WUPBaseElement[];
|
26 | constructor();
|
27 | /** Returns true if element is appended (result of setTimeout on connectedCallback) */
|
28 | get $isReady(): boolean;
|
29 | /** Try to focus self or first possible children; returns true if succesful */
|
30 | focus(): boolean;
|
31 | /** Remove focus from element on any nested active element */
|
32 | blur(): void;
|
33 | /** Called when need to parse attribute */
|
34 | parse(text: string): any;
|
35 | /** Called when element is added to document (after empty timeout - at the end of call stack) */
|
36 | protected gotReady(): void;
|
37 | /** Called when element is removed from document */
|
38 | protected gotRemoved(): void;
|
39 | /** Called on Init and every time as options/attributes changed */
|
40 | protected gotChanges(propsChanged: Array<string> | null): void;
|
41 | /** Called when element isReady and at least one of observedOptions is changed */
|
42 | protected gotOptionsChanged(e: WUP.Base.OptionEvent): void;
|
43 | /** Called once on Init */
|
44 | protected gotRender(): void;
|
45 | /** Browser calls this method when the element is added to the document */
|
46 | protected connectedCallback(): void;
|
47 | /** Browser calls this method when the element is removed from the document;
|
48 | * (can be called many times if an element is repeatedly added/removed) */
|
49 | protected disconnectedCallback(): void;
|
50 | _isStopChanges: boolean;
|
51 | /** Called when element isReady and one of observedAttributes is changed */
|
52 | protected gotAttributeChanged(name: string, oldValue: string, newValue: string): void;
|
53 | /** Browser calls this method when attrs pointed in observedAttributes is changed */
|
54 | protected attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
|
55 | addEventListener<K extends keyof Events>(type: K, listener: (this: WUPBaseElement, ev: Events[K]) => any, options?: boolean | AddEventListenerOptions): void;
|
56 | addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
|
57 | removeEventListener<K extends keyof Events>(type: K, listener: (this: WUPBaseElement, ev: Events[K]) => any, options?: boolean | EventListenerOptions): void;
|
58 | removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
|
59 | /** Inits customEvent & calls dispatchEvent and returns created event
|
60 | * @tutorial Troubleshooting
|
61 | * * Default event bubbling: el.event.click > el.onclick >>> parent.event.click > parent.onclick etc.
|
62 | * * Custom event bubbling: el.$onclick > el.event.$click >>> parent.event.$click otherwise impossible to stop propagation from on['event] of target directly */
|
63 | fireEvent<K extends keyof Events>(type: K, eventInit?: CustomEventInit): Event;
|
64 | /** Array of removeEventListener() that called on remove */
|
65 | protected disposeLst: Array<() => void>;
|
66 | /** Add event listener and remove after component removed; @options.passive=true by default */
|
67 | appendEvent<T extends keyof E, K extends HTMLElement | Document, E extends HTMLElementEventMap & Record<string, Event>>(...args: Parameters<onEventType<T, K, E>>): () => void;
|
68 | /** Remove events/functions that was appended */
|
69 | protected dispose(): void;
|
70 | /** Returns true if el is instance of Node and contains pointed element
|
71 | * @tutorial Troubleshooting
|
72 | * * if element has position `fixed` or `absolute` then returns false */
|
73 | includes(el: unknown): boolean;
|
74 | /** Returns true if element contains eventTarget or it's eventTarget
|
75 | * @tutorial Troubleshooting
|
76 | * * if element has position `fixed` or `absolute` then returns false */
|
77 | includesTarget(e: Event): boolean;
|
78 | /** Parse attribute and return result; if attr missed or invalid => returns pointed alt value OR $options[attr] */
|
79 | getAttr(attr: string, type?: "string", alt?: string): string | undefined;
|
80 | /** Returns `bool if attr is `false` or `true`, `true` if "" or string if exists */
|
81 | getAttr(attr: string, type: "boolOrString", alt?: string | boolean): string | boolean | undefined;
|
82 | getAttr(attr: string, type: "bool", alt?: boolean): boolean | undefined;
|
83 | getAttr(attr: string, type: "number", alt?: number): number | undefined;
|
84 | /** Returns value from window[key] according to [attr]="key"; if attr missed or invalid => returns pointed alt value OR $options[attr] */
|
85 | getAttr<T>(attr: string, type: "obj", alt?: T): T;
|
86 | /** Returns value according to this.parse(); if attr missed or invalid => returns pointed alt value OR $options[attr] */
|
87 | getAttr<T>(attr: string, type: "ref", alt?: T): T;
|
88 | /** Remove attr if value falseOrEmpty; set '' or 'true' if true for HTMLELement
|
89 | * @param isSetEmpty set if need '' instead of 'value' */
|
90 | setAttr(attr: string, v: boolean | string | undefined | null, isSetEmpty?: boolean): void;
|
91 | /** Remove all children in fastest way */
|
92 | removeChildren(): void;
|
93 | /** Throws unhanled error via empty setTimeout */
|
94 | throwError(err: string | Error | unknown): void;
|
95 | }
|
96 | declare global {
|
97 | namespace WUP.Base {
|
98 |
|
99 | type toJSX<T> = {
|
100 | [P in keyof T]?: T[P] extends number | boolean | string | undefined ? T[P] : string;
|
101 | };
|
102 | type OptionEvent<T extends Record<string, any> = Record<string, any>> = {
|
103 | props: Array<Extract<keyof T, string>>;
|
104 | target: T;
|
105 | };
|
106 | type EventMap<T = HTMLElementEventMap> = HTMLElementEventMap & Record<keyof T, Event>;
|
107 | type JSXProps<T, Opts extends Record<string, any> = any> = React.DetailedHTMLProps<Omit<React.HTMLAttributes<T>, "className"> & {
|
108 | class?: string | undefined;
|
109 | }, T> & toJSX<Opts>;
|
110 | }
|
111 | }
|