1 |
|
2 | import { onEventType } from "./helpers/onEvent";
|
3 | export interface AttributeMap {
|
4 |
|
5 | type: AttributeTypes;
|
6 |
|
7 | prop?: string;
|
8 |
|
9 | parse?: (attrValue: string) => any;
|
10 | }
|
11 | export declare const enum AttributeTypes {
|
12 |
|
13 | bool = 0,
|
14 | number = 1,
|
15 | string = 2,
|
16 | reference = 3,
|
17 | parsedObject = 4,
|
18 | parseCustom = 5,
|
19 |
|
20 | selector = 6
|
21 | }
|
22 | declare global {
|
23 | |
24 |
|
25 |
|
26 |
|
27 | const __wupln: Window["__wupln"];
|
28 | interface Window {
|
29 | |
30 |
|
31 |
|
32 |
|
33 | __wupln: (text: string, type: "aria" | "content" | "validation") => string;
|
34 | }
|
35 | }
|
36 |
|
37 | export default abstract class WUPBaseElement<TOptions extends Record<string, any> = Record<string, any>, Events extends WUP.Base.EventMap = WUP.Base.EventMap> extends HTMLElement {
|
38 | #private;
|
39 |
|
40 | static $use(): void;
|
41 |
|
42 | static get $refStyle(): HTMLStyleElement | null;
|
43 | static set $refStyle(v: HTMLStyleElement | null);
|
44 |
|
45 | static get $style(): string;
|
46 |
|
47 | static get $styleRoot(): string;
|
48 |
|
49 | static get $uniqueId(): string;
|
50 |
|
51 | static get classNameHidden(): string;
|
52 |
|
53 | static get classNameBtnIcon(): string;
|
54 |
|
55 | static mapAttribute(value: any): AttributeTypes;
|
56 |
|
57 | static get mappedAttributes(): Record<string, AttributeMap>;
|
58 | |
59 |
|
60 | static get observedOptions(): Array<string> | null;
|
61 | |
62 |
|
63 | static get observedAttributes(): Array<string>;
|
64 |
|
65 | static $defaults: Record<string, any>;
|
66 |
|
67 | static cloneValue<T>(v: T): T;
|
68 |
|
69 | static cloneDefaults<T extends Record<string, any>>(): T;
|
70 |
|
71 | static mergeDefaults<T extends Record<string, any>>(opts: T): T;
|
72 |
|
73 | protected _opts: TOptions;
|
74 | get $options(): Partial<TOptions>;
|
75 |
|
76 | set $options(v: Partial<TOptions>);
|
77 | static findAllProtos(t: unknown, protos: typeof WUPBaseElement[]): typeof WUPBaseElement[];
|
78 | constructor();
|
79 | /** Returns true if element is appended (result of setTimeout on connectedCallback) */
|
80 | get $isReady(): boolean;
|
81 | /** Returns if current element or some nested child is active/focused */
|
82 | get $isFocused(): boolean;
|
83 | /** Try to focus self or first possible children; returns true if successful */
|
84 | focus(): boolean;
|
85 | /** Remove focus from element on any nested active element */
|
86 | blur(): void;
|
87 | /** Called when need to parse attribute */
|
88 | parse(text: string): any;
|
89 | /** Clear this to prevent autofocus */
|
90 | _willFocus?: ReturnType<typeof setTimeout>;
|
91 | /** Called when element is added to document (after empty timeout - at the end of call stack) */
|
92 | protected gotReady(): void;
|
93 | /** Called when element is removed from document */
|
94 | protected gotRemoved(): void;
|
95 | /** Called on Init and every time as options/attributes changed */
|
96 | protected gotChanges(propsChanged: Array<string> | null): void;
|
97 | /** Called when element isReady and at least one of observedOptions is changed */
|
98 | protected gotOptionsChanged(e: WUP.Base.OptionEvent): void;
|
99 | /** Called once on Init */
|
100 | protected gotRender(): void;
|
101 | /** Browser calls this method when the element is added to the document */
|
102 | protected connectedCallback(): void;
|
103 | /** Browser calls this method when the element is removed from the document;
|
104 | * (can be called many times if an element is repeatedly added/removed) */
|
105 | protected disconnectedCallback(): void;
|
106 | /** Prevent gotChanges call during the some attribute or options custom changes */
|
107 | _isStopChanges: boolean;
|
108 | /** Called when any of observedAttributes is changed */
|
109 | protected gotAttributeChanged(name: string, value: string | null): void;
|
110 | /** Browser calls this method when attrs pointed in observedAttributes is changed */
|
111 | protected attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
|
112 | addEventListener<K extends keyof Events>(type: K, listener: (this: WUPBaseElement, ev: Events[K]) => any, options?: boolean | AddEventListenerOptions): void;
|
113 | addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
|
114 | removeEventListener<K extends keyof Events>(type: K, listener: (this: WUPBaseElement, ev: Events[K]) => any, options?: boolean | EventListenerOptions): void;
|
115 | removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
|
116 | /** Inits customEvent & calls dispatchEvent and returns created event; by default `cancelable: false, bubbles: true`
|
117 | * @tutorial Troubleshooting
|
118 | * * Default event bubbling: el.event.click > el.onclick >>> parent.event.click > parent.onclick etc.
|
119 | * * Custom event bubbling: el.$onclick > el.event.$click >>> parent.event.$click otherwise impossible to stop propagation from on['event] of target directly */
|
120 | fireEvent<K extends keyof Events, T extends Events[K]["detail"]>(type: K, eventInit?: CustomEventInit<T>): Event;
|
121 | /** Array of removeEventListener() that called on remove */
|
122 | protected disposeLst: Array<() => void>;
|
123 | /** Add event listener and remove after component removed; @options.passive=true by default */
|
124 | appendEvent<T extends keyof E, K extends HTMLElement | Document, E extends HTMLElementEventMap & Record<string, Event>>(...args: Parameters<onEventType<T, K, E>>): () => void;
|
125 | /** Remove events/functions that was appended */
|
126 | protected dispose(): void;
|
127 | /** Returns true if el is instance of Node and contains pointed element
|
128 | * @tutorial Troubleshooting
|
129 | * * if element has position `fixed` or `absolute` then returns false */
|
130 | includes(el: unknown): boolean;
|
131 | /** Returns true if element contains eventTarget or it's eventTarget
|
132 | * @tutorial Troubleshooting
|
133 | * * if element has position `fixed` or `absolute` then returns false */
|
134 | includesTarget(e: Event): boolean;
|
135 | /** Find parent/self according with callback */
|
136 | findParent(callback: (el: HTMLElement) => boolean, options?: {
|
137 | bubbleCount: number;
|
138 | }): HTMLElement | null;
|
139 | /** Returns true if contains pointed element or has itself
|
140 | * @tutorial Troubleshooting
|
141 | * * if element has position `fixed` or `absolute` then returns false */
|
142 | itsMe(el: Element | EventTarget | null): boolean;
|
143 | /** Returns parsed value according to pointed type OR current value if something wrong;
|
144 | * override method for implementation custom parsing OR static method mappedAttributes to redefine map-types */
|
145 | parseAttr(type: AttributeTypes, attrValue: string, propName: string, attrName: string): any;
|
146 | /** Remove attr if value falseOrEmpty; set '' or 'true' if true for HTMLELement
|
147 | * @param isSetEmpty set if need '' instead of 'value' */
|
148 | setAttr(attr: string, v: boolean | string | undefined | null, isSetEmpty?: boolean): void;
|
149 | /** Remove all children in fastest way */
|
150 | removeChildren(): void;
|
151 | /** Throws unhandled error via empty setTimeout */
|
152 | throwError(err: string | Error | unknown): void;
|
153 | /** Removes empty attr [style] */
|
154 | removeEmptyStyle(): void;
|
155 | }
|
156 | declare global {
|
157 | namespace WUP.Base {
|
158 |
|
159 | type toJSX<T> = {
|
160 | [P in keyof T]?: T[P] extends number | boolean | string | undefined ? T[P] : string;
|
161 | };
|
162 | type OnlyNames<T> = {
|
163 | [K in keyof T as K extends string ? `w-${K}` : never]?: unknown;
|
164 | };
|
165 | type OptionEvent<T extends Record<string, any> = Record<string, any>> = {
|
166 | props: Array<Extract<keyof T, string>>;
|
167 | target: T;
|
168 | };
|
169 | type EventMap<T = HTMLElementEventMap> = HTMLElementEventMap & Record<keyof T, Event>;
|
170 | type ReactHTML<T> = React.DetailedHTMLProps<Omit<React.HTMLAttributes<T>, "className"> & {
|
171 | class?: string | undefined;
|
172 | |
173 |
|
174 | className?: never;
|
175 | }, T>;
|
176 | type JSXProps<T> = React.DetailedHTMLProps<Omit<React.HTMLAttributes<T>, "className"> & {
|
177 | class?: string | undefined;
|
178 | }, T>;
|
179 | }
|
180 | }
|