I am trying to handle a TouchEvent by re-dispatching it to another DOM element. Everything works perfectly on iOS and desktop browsers with touch simulation using the following code:
onTouched(event: TouchEvent) {
this.targetElement.getNativeElement()
.dispatchEvent(new TouchEvent(event.type, event));
}
However, I can't get this working on Android device. I tried with a mobile phone and simulator. The problem lies in the code constructing a TouchEvent.
Here is what I tried:
new TouchEvent(event.type, event)givesIllegal constructorerrornew event.constructor(event.type, event)givesIllegal constructorerrorI tried to strip
eventfrom all other fields other thantouchesto fit Typescript'sTouchEventInitinterface:let touchArr: Touch[] = []; for (var _i = 0; _i < event.touches.length; _i++) { touchArr[_i] = event.touches[_i]; } let newEvent = new TouchEvent(event.type, <TouchEventInit>{ touches: touchArr });but it still gives
Illegal constructorexception.In the act of desperation, I tried to use a deprecated
document.createEvent:let newEvent: any = document.createEvent('TouchEvent'); newEvent.initTouchEvent(event);However, the
initTouchEventdoes not set thetypefor some reason (probably because of this one) and it shouldn't be used anyway as it's going to be removed.
I am simply out of ideas. I tried with the newest Android System WebView (60.0.3112.116). I could understand this to happen on a desktop browser, but it's a mobile device...