2022-04-21 18:51:03 +02:00
|
|
|
import { Controller } from '@hotwired/stimulus';
|
2022-10-20 18:13:36 +02:00
|
|
|
import debounce from 'debounce';
|
2023-01-12 18:49:46 +01:00
|
|
|
import invariant from 'tiny-invariant';
|
2022-04-21 18:51:03 +02:00
|
|
|
|
|
|
|
export type Detail = Record<string, unknown>;
|
|
|
|
|
2023-01-17 13:36:38 +01:00
|
|
|
// see: https://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
|
|
|
|
const FOCUS_EVENTS = ['focus', 'blur'];
|
|
|
|
|
2022-04-21 18:51:03 +02:00
|
|
|
export class ApplicationController extends Controller {
|
2023-01-11 21:45:41 +01:00
|
|
|
#debounced = new Map<() => void, ReturnType<typeof debounce>>();
|
2022-04-21 18:51:03 +02:00
|
|
|
|
|
|
|
protected debounce(fn: () => void, interval: number): void {
|
2022-10-24 20:15:09 +02:00
|
|
|
this.globalDispatch('debounced:added');
|
|
|
|
|
2022-04-21 18:51:03 +02:00
|
|
|
let debounced = this.#debounced.get(fn);
|
|
|
|
if (!debounced) {
|
2022-10-25 10:56:22 +02:00
|
|
|
const wrapper = () => {
|
2022-10-24 20:15:09 +02:00
|
|
|
fn.bind(this)();
|
|
|
|
this.#debounced.delete(fn);
|
|
|
|
if (this.#debounced.size == 0) {
|
|
|
|
this.globalDispatch('debounced:empty');
|
|
|
|
}
|
2022-10-25 10:56:22 +02:00
|
|
|
};
|
2022-10-24 20:15:09 +02:00
|
|
|
|
|
|
|
debounced = debounce(wrapper.bind(this), interval);
|
|
|
|
|
2022-04-21 18:51:03 +02:00
|
|
|
this.#debounced.set(fn, debounced);
|
|
|
|
}
|
|
|
|
debounced();
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:45:41 +01:00
|
|
|
protected cancelDebounce(fn: () => void) {
|
|
|
|
this.#debounced.get(fn)?.clear();
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:44:33 +02:00
|
|
|
protected globalDispatch<T = Detail>(type: string, detail?: T): void {
|
2022-04-21 18:51:03 +02:00
|
|
|
this.dispatch(type, {
|
2022-08-31 15:57:37 +02:00
|
|
|
detail: detail as object,
|
2022-04-21 18:51:03 +02:00
|
|
|
prefix: '',
|
|
|
|
target: document.documentElement
|
|
|
|
});
|
|
|
|
}
|
2022-05-04 12:42:42 +02:00
|
|
|
|
2023-01-12 18:49:46 +01:00
|
|
|
protected on<HandlerEvent extends Event = Event>(
|
|
|
|
target: EventTarget,
|
|
|
|
eventName: string,
|
|
|
|
handler: (event: HandlerEvent) => void
|
|
|
|
): void;
|
2022-05-04 12:42:42 +02:00
|
|
|
protected on<HandlerEvent extends Event = Event>(
|
|
|
|
eventName: string,
|
|
|
|
handler: (event: HandlerEvent) => void
|
2023-01-12 18:49:46 +01:00
|
|
|
): void;
|
|
|
|
protected on<HandlerEvent extends Event = Event>(
|
|
|
|
targetOrEventName: EventTarget | string,
|
|
|
|
eventNameOrHandler: string | ((event: HandlerEvent) => void),
|
|
|
|
handler?: (event: HandlerEvent) => void
|
2022-05-06 19:44:33 +02:00
|
|
|
): void {
|
2023-01-12 18:49:46 +01:00
|
|
|
if (typeof targetOrEventName == 'string') {
|
|
|
|
invariant(typeof eventNameOrHandler != 'string', 'handler is required');
|
2023-01-17 13:36:38 +01:00
|
|
|
this.onTarget(
|
|
|
|
this.element,
|
|
|
|
targetOrEventName,
|
|
|
|
eventNameOrHandler,
|
|
|
|
FOCUS_EVENTS.includes(targetOrEventName)
|
|
|
|
);
|
2023-01-12 18:49:46 +01:00
|
|
|
} else {
|
2023-01-30 11:00:17 +01:00
|
|
|
invariant(
|
|
|
|
typeof eventNameOrHandler == 'string',
|
|
|
|
'event name is required'
|
|
|
|
);
|
2023-01-12 18:49:46 +01:00
|
|
|
invariant(handler, 'handler is required');
|
|
|
|
this.onTarget(targetOrEventName, eventNameOrHandler, handler);
|
|
|
|
}
|
2022-05-06 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected onGlobal<HandlerEvent extends Event = Event>(
|
|
|
|
eventName: string,
|
|
|
|
handler: (event: HandlerEvent) => void
|
|
|
|
): void {
|
|
|
|
this.onTarget(document.documentElement, eventName, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
private onTarget<HandlerEvent extends Event = Event>(
|
|
|
|
target: EventTarget,
|
|
|
|
eventName: string,
|
2023-01-17 13:36:38 +01:00
|
|
|
handler: (event: HandlerEvent) => void,
|
|
|
|
capture?: boolean
|
2022-05-04 12:42:42 +02:00
|
|
|
): void {
|
|
|
|
const disconnect = this.disconnect;
|
|
|
|
const callback = (event: Event): void => {
|
|
|
|
handler(event as HandlerEvent);
|
|
|
|
};
|
2023-01-17 13:36:38 +01:00
|
|
|
target.addEventListener(eventName, callback, capture);
|
2022-05-04 12:42:42 +02:00
|
|
|
this.disconnect = () => {
|
2023-01-17 13:36:38 +01:00
|
|
|
target.removeEventListener(eventName, callback, capture);
|
2022-05-04 12:42:42 +02:00
|
|
|
disconnect.call(this);
|
|
|
|
};
|
|
|
|
}
|
2022-04-21 18:51:03 +02:00
|
|
|
}
|