Changes between Initial Version and Version 1 of Ticket #14926, comment 4


Ignore:
Timestamp:
Mar 4, 2016, 11:06:27 AM (3 years ago)
Author:
Scott González
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #14926, comment 4

    initial v1  
    11I used the following duck punch workaround:
    22
    3   var _mouseProto = $.ui["mouse"].prototype ? $.ui["mouse"].prototype : {}.prototype;
    4   var _mouseInit = _mouseProto["_mouseInit"];
    5   var _mouseDestroy = _mouseProto["_mouseDestroy"];
    6 
    7   function _wrapTouch(event)
    8   {
    9     if (["touchstart", "touchmove", "touchend", "touchcancel"].indexOf(event["type"]) === -1)
    10     {
    11       return event;
    12     }
    13 
    14     var _getFirstTouchPoint = function(evt)
    15     {
    16       if (evt["changedTouches"] && evt["changedTouches"].length)
    17       {
    18         return evt["changedTouches"][0];
    19       }
    20       else if (evt["targetTouches"] && evt["targetTouches"].length)
    21       {
    22         return evt["targetTouches"][0];
    23       }
    24       else if (evt["touches"] && evt["touches"].length)
    25       {
    26         return evt["touches"][0];
    27       }
    28       return null;
    29     };
    30 
    31     var _TOUCH_TO_MOUSE_XREF = {"touchstart": "mousedown", "touchmove": "mousemove", "touchend": "mouseup", "touchcancel": "mouseup"};
    32     var _COPY_SAFE_EVENT_PROPERTIES = {"altKey": true, "bubbles": true, "cancelable": true, "ctrlKey": true,
    33       "currentTarget": true, "eventPhase": true, "metaKey": true, "target": true,
    34       "relatedTarget": true, "shiftKey": true, "timeStamp": true,
    35       "view": true, "which": true, "button": true, "buttons": true, "clientX": true,
    36       "clientY": true, "offsetX": true, "offsetY": true, "pageX": true,
    37       "pageY": true, "screenX": true, "screenY": true, "toElement": true,
    38       "char": true, "charCode": true, "key": true, "keyCode": true};
    39     var simulatedEvent = document.createEvent("MouseEvent");
    40     var touch = _getFirstTouchPoint(event["originalEvent"]);
    41     simulatedEvent.initMouseEvent(_TOUCH_TO_MOUSE_XREF[ event["type"] ], true, true, window, 1,
    42       touch["screenX"], touch["screenY"], touch["clientX"], touch["clientY"], false,
    43       false, false, false, 0, null);
    44 
    45     // Capture all interesting event properties.  Similar to jQuery.event.fix.
    46     var props = {};
    47     if (Object.defineProperty)
    48     {
    49       var descriptor = {"value": touch["target"], "writable": false, "enumerable": true, "configurable": false};
    50       Object.defineProperty(simulatedEvent, "target", descriptor);
    51     }
    52 
    53     for (var key in simulatedEvent)
    54     {
    55       if (_COPY_SAFE_EVENT_PROPERTIES[ key ] && !$.isFunction(simulatedEvent[ key ]))
    56       {
    57         props[ key ] = simulatedEvent[ key ];
    58       }
    59     }
    60     if (!props["target"])
    61       props["target"] = touch["target"];
    62 
    63     // Wrap a native simulated event in a jQuery.Event extending propertiea
    64     var dragEvent = $.Event(simulatedEvent, props);
    65     return dragEvent;
    66   };
    67 
    68   _mouseProto._touchStartDelegate = function(event)
    69   {
    70     if (this["_mouseDown"](_wrapTouch(event)))
    71     {
    72       this._on(this.document, {"touchmove": this._touchMoveDelegate});
    73       this._on(this.document, {"touchend": this._touchEndDelegate, "touchcancel": this._touchEndDelegate});
    74     }
    75   };
    76 
    77   _mouseProto._touchMoveDelegate = function(event)
    78   {
    79     var e = _wrapTouch(event);
    80     if (this["_mouseCapture"](e))
    81     {
    82       event.preventDefault();     // prevent browser scroll
    83       this["_mouseMove"](_wrapTouch(e));
    84     }
    85   };
    86 
    87   _mouseProto._touchEndDelegate = function(event)
    88   {
    89     this._off(this.document, "touchmove touchend touchcancel");
    90     this["_mouseUp"](_wrapTouch(event));
    91   };
    92 
    93   _mouseProto["_mouseInit"] = function()
    94   {
    95     this._on(this.element, {"touchstart": this._touchStartDelegate});
    96     _mouseInit.call(this);
    97   };
    98 
    99   _mouseProto["_mouseDestroy"] = function()
    100   {
    101     this._off(this.element, "touchstart");
    102     this._off(this.document, "touchmove touchend touchcancel");
    103     _mouseDestroy.call(this);
    104   };
     3NOTE: Code removed from comment. Use http://touchpunch.furf.com/ for now.