ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
86 lines (72 loc) • 2.99 kB
text/typescript
import { NzSafeAny } from 'ng-zorro-antd/core/types';
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/** Creates a browser MouseEvent with the specified options. */
export function createMouseEvent(type: string, x: number = 0, y: number = 0): MouseEvent {
const event = document.createEvent('MouseEvent');
event.initMouseEvent(
type,
false /* canBubble */,
false /* cancelable */,
window /* view */,
0 /* detail */,
x /* screenX */,
y /* screenY */,
x /* clientX */,
y /* clientY */,
false /* ctrlKey */,
false /* altKey */,
false /* shiftKey */,
false /* metaKey */,
0 /* button */,
null /* relatedTarget */,
);
return event;
}
/** Creates a browser TouchEvent with the specified pointer coordinates. */
export function createTouchEvent(type: string, pageX: number = 0, pageY: number = 0): UIEvent {
// In favor of creating events that work for most of the browsers, the event is created
// as a basic UI Event. The necessary details for the event will be set manually.
const event = document.createEvent('UIEvent');
const touchDetails = { pageX, pageY };
(event as NzSafeAny).initUIEvent(type, true, true, window, 0);
// Most of the browsers don't have a "initTouchEvent" method that can be used to define
// the touch details.
Object.defineProperties(event, {
touches: { value: [touchDetails] },
});
return event;
}
/** Dispatches a keydown event from an element. */
export function createKeyboardEvent(type: string, keyCode: number, target?: Element, key?: string): any {
const event = document.createEvent('KeyboardEvent') as any;
// Firefox does not support `initKeyboardEvent`, but supports `initKeyEvent`.
const initEventFn = (event.initKeyEvent || event.initKeyboardEvent).bind(event);
const originalPreventDefault = event.preventDefault;
initEventFn(type, true, true, window, 0, 0, 0, 0, 0, keyCode);
// Webkit Browsers don't set the keyCode when calling the init function.
// See related bug https://bugs.webkit.org/show_bug.cgi?id=16735
Object.defineProperties(event, {
keyCode: { get: () => keyCode },
key: { get: () => key },
target: { get: () => target },
});
// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
event.preventDefault = function (): any {
Object.defineProperty(event, 'defaultPrevented', { get: () => true });
// tslint:disable-next-line:no-invalid-this
return originalPreventDefault.apply(this, arguments);
};
return event;
}
/** Creates a fake event object with any desired event type. */
export function createFakeEvent(type: string, canBubble: boolean = true, cancelable: boolean = true): Event {
const event = document.createEvent('Event');
event.initEvent(type, canBubble, cancelable);
return event;
}