@@ -445,6 +445,19 @@ bot.events.KeyboardEventFactory_.prototype.create = function(target, opt_args) {
445445
446446
447447
448+ /**
449+ * Enum representing which mechanism to use for creating touch events.
450+ * @enum {number}
451+ * @private
452+ */
453+ bot . events . TouchEventStrategy_ = {
454+ MOUSE_EVENTS : 1 ,
455+ INIT_TOUCH_EVENT : 2 ,
456+ TOUCH_EVENT_CTOR : 3
457+ } ;
458+
459+
460+
448461/**
449462 * Factory for touch event objects of a specific type.
450463 *
@@ -506,22 +519,61 @@ bot.events.TouchEventFactory_.prototype.create = function(target, opt_args) {
506519 return touches ;
507520 }
508521
509- function createTouchList ( touches ) {
510- return bot . events . BROKEN_TOUCH_API_ ?
511- createGenericTouchList ( touches ) :
512- createNativeTouchList ( touches ) ;
522+ function createTouchEventTouchList ( touchListArgs ) {
523+ /** @type {!Array<!Touch> } */
524+ var touches = goog . array . map ( touchListArgs , function ( touchArg ) {
525+ return new Touch ( {
526+ identifier : touchArg . identifier ,
527+ screenX : touchArg . screenX ,
528+ screenY : touchArg . screenY ,
529+ clientX : touchArg . clientX ,
530+ clientY : touchArg . clientY ,
531+ pageX : touchArg . pageX ,
532+ pageY : touchArg . pageY ,
533+ target : target
534+ } ) ;
535+ } ) ;
536+ return touches ;
537+ }
538+
539+ function createTouchList ( touchStrategy , touches ) {
540+ switch ( touchStrategy ) {
541+ case bot . events . TouchEventStrategy_ . MOUSE_EVENTS :
542+ return createGenericTouchList ( touches ) ;
543+ case bot . events . TouchEventStrategy_ . INIT_TOUCH_EVENT :
544+ return createNativeTouchList ( touches ) ;
545+ case bot . events . TouchEventStrategy_ . TOUCH_EVENT_CTOR :
546+ return createTouchEventTouchList ( touches ) ;
547+ }
548+ return null ;
549+ }
550+
551+ // TODO(juangj): Always use the TouchEvent constructor, if available.
552+ var strategy ;
553+ if ( bot . events . BROKEN_TOUCH_API_ ) {
554+ strategy = bot . events . TouchEventStrategy_ . MOUSE_EVENTS ;
555+ } else {
556+ if ( TouchEvent . prototype . initTouchEvent ) {
557+ strategy = bot . events . TouchEventStrategy_ . INIT_TOUCH_EVENT ;
558+ } else if ( TouchEvent && TouchEvent . length > 0 ) {
559+ strategy = bot . events . TouchEventStrategy_ . TOUCH_EVENT_CTOR ;
560+ } else {
561+ throw new bot . Error (
562+ bot . ErrorCode . UNSUPPORTED_OPERATION ,
563+ 'Not able to create touch events in this browser' ) ;
564+ }
513565 }
514566
515567 // As a performance optimization, reuse the created touchlist when the lists
516568 // are the same, which is often the case in practice.
517- var changedTouches = createTouchList ( args . changedTouches ) ;
569+ var changedTouches = createTouchList ( strategy , args . changedTouches ) ;
518570 var touches = ( args . touches == args . changedTouches ) ?
519- changedTouches : createTouchList ( args . touches ) ;
571+ changedTouches : createTouchList ( strategy , args . touches ) ;
520572 var targetTouches = ( args . targetTouches == args . changedTouches ) ?
521- changedTouches : createTouchList ( args . targetTouches ) ;
573+ changedTouches : createTouchList ( strategy , args . targetTouches ) ;
522574
523575 var event ;
524- if ( bot . events . BROKEN_TOUCH_API_ ) {
576+ if ( strategy == bot . events . TouchEventStrategy_ . MOUSE_EVENTS ) {
525577 event = doc . createEvent ( 'MouseEvents' ) ;
526578 event . initMouseEvent ( this . type_ , this . bubbles_ , this . cancelable_ , view ,
527579 /*detail*/ 1 , /*screenX*/ 0 , /*screenY*/ 0 , args . clientX , args . clientY ,
@@ -532,7 +584,7 @@ bot.events.TouchEventFactory_.prototype.create = function(target, opt_args) {
532584 event . changedTouches = changedTouches ;
533585 event . scale = args . scale ;
534586 event . rotation = args . rotation ;
535- } else {
587+ } else if ( strategy == bot . events . TouchEventStrategy_ . INIT_TOUCH_EVENT ) {
536588 event = doc . createEvent ( 'TouchEvent' ) ;
537589 // Different browsers have different implementations of initTouchEvent.
538590 if ( event . initTouchEvent . length == 0 ) {
@@ -548,6 +600,23 @@ bot.events.TouchEventFactory_.prototype.create = function(target, opt_args) {
548600 touches , targetTouches , changedTouches , args . scale , args . rotation ) ;
549601 }
550602 event . relatedTarget = args . relatedTarget ;
603+ } else if ( strategy == bot . events . TouchEventStrategy_ . TOUCH_EVENT_CTOR ) {
604+ var touchProperties = /** @type {!TouchEventInit } */ ( {
605+ touches : touches ,
606+ targetTouches : targetTouches ,
607+ changedTouches : changedTouches ,
608+ bubbles : this . bubbles_ ,
609+ cancelable : this . cancelable_ ,
610+ ctrlKey : args . ctrlKey ,
611+ shiftKey : args . shiftKey ,
612+ altKey : args . altKey ,
613+ metaKey : args . metaKey
614+ } ) ;
615+ event = new TouchEvent ( this . type_ , touchProperties ) ;
616+ } else {
617+ throw new bot . Error (
618+ bot . ErrorCode . UNSUPPORTED_OPERATION ,
619+ 'Illegal TouchEventStrategy_ value (this is a bug)' ) ;
551620 }
552621
553622 return event ;
0 commit comments