mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
/* eslint-disable no-empty */
|
|
/* eslint-disable getter-return */
|
|
/* eslint-disable import/no-mutable-exports */
|
|
import { isServer } from '.';
|
|
|
|
type EventHanlder = (event?: Event) => void;
|
|
|
|
export let supportsPassive = false;
|
|
|
|
if (!isServer) {
|
|
try {
|
|
const opts = {};
|
|
Object.defineProperty(opts, 'passive', {
|
|
get() {
|
|
/* istanbul ignore next */
|
|
supportsPassive = true;
|
|
}
|
|
});
|
|
window.addEventListener('test-passive', null as any, opts);
|
|
} catch (e) {}
|
|
}
|
|
|
|
export function on(
|
|
target: HTMLElement,
|
|
event: string,
|
|
handler: EventHanlder,
|
|
passive = false
|
|
) {
|
|
if (!isServer) {
|
|
target.addEventListener(
|
|
event,
|
|
handler,
|
|
supportsPassive ? { capture: false, passive } : false
|
|
);
|
|
}
|
|
}
|
|
|
|
export function off(target: HTMLElement, event: string, handler: EventHanlder) {
|
|
if (!isServer) {
|
|
target.removeEventListener(event, handler);
|
|
}
|
|
}
|
|
|
|
export function stopPropagation(event: Event) {
|
|
event.stopPropagation();
|
|
}
|
|
|
|
export function preventDefault(event: Event, isStopPropagation?: boolean) {
|
|
/* istanbul ignore else */
|
|
if (typeof event.cancelable !== 'boolean' || event.cancelable) {
|
|
event.preventDefault();
|
|
}
|
|
|
|
if (isStopPropagation) {
|
|
stopPropagation(event);
|
|
}
|
|
}
|