@@ -56,41 +56,24 @@ function matchesSelector(el, selector) {
5656 return el [ method ] . call ( el , selector ) ;
5757}
5858
59- // @credits : http://stackoverflow.com/questions/4817029/whats-the-best-way-to-detect-a-touch-screen-device-using-javascript/4819886#4819886
60- /* Conditional to fix node server side rendering of component */
61- if ( typeof window === 'undefined' ) {
62- // Do Node Stuff
63- var isTouchDevice = false ;
64- } else {
65- // Do Browser Stuff
66- var isTouchDevice = 'ontouchstart' in window || // works on most browsers
67- 'onmsgesturechange' in window ; // works on ie10 on ms surface
68-
69- }
70-
71- // look ::handleDragStart
72- //function isMultiTouch(e) {
73- // return e.touches && Array.isArray(e.touches) && e.touches.length > 1
74- //}
75-
7659/**
7760 * simple abstraction for dragging events names
7861 * */
79- var dragEventFor = ( function ( ) {
80- var eventsFor = {
81- touch : {
82- start : 'touchstart ' ,
83- move : 'touchmove' ,
84- end : 'touchend'
85- } ,
86- mouse : {
87- start : 'mousedown ' ,
88- move : 'mousemove' ,
89- end : 'mouseup'
90- }
91- } ;
92- return eventsFor [ isTouchDevice ? 'touch' : ' mouse' ] ;
93- } ) ( ) ;
62+ var eventsFor = {
63+ touch : {
64+ start : 'touchstart' ,
65+ move : 'touchmove ' ,
66+ end : 'touchend'
67+ } ,
68+ mouse : {
69+ start : 'mousedown' ,
70+ move : 'mousemove ' ,
71+ end : 'mouseup'
72+ }
73+ } ;
74+
75+ // Default to mouse events
76+ var dragEventFor = eventsFor [ 'mouse' ] ;
9477
9578/**
9679 * get {clientX, clientY} positions of control
@@ -528,13 +511,6 @@ module.exports = React.createClass({
528511 } ,
529512
530513 handleDragStart : function ( e ) {
531- // todo: write right implementation to prevent multitouch drag
532- // prevent multi-touch events
533- // if (isMultiTouch(e)) {
534- // this.handleDragEnd.apply(e, arguments);
535- // return
536- // }
537-
538514 // Make it possible to attach event handlers on top of this one
539515 this . props . onMouseDown ( e ) ;
540516
@@ -619,6 +595,24 @@ module.exports = React.createClass({
619595 } ) ;
620596 } ,
621597
598+ onMouseDown : function ( ev ) {
599+ // Prevent 'ghost click' which happens 300ms after touchstart if the event isn't cancelled.
600+ // We don't cancel the event on touchstart because of #37; we might want to make a scrollable item draggable.
601+ // More on ghost clicks: http://ariatemplates.com/blog/2014/05/ghost-clicks-in-mobile-browsers/
602+ if ( dragEventFor == eventsFor [ 'touch' ] ) {
603+ return ev . preventDefault ( ) ;
604+ }
605+
606+ return this . handleDragStart . apply ( this , arguments ) ;
607+ } ,
608+
609+ onTouchStart : function ( ev ) {
610+ // We're on a touch device now, so change the event handlers
611+ dragEventFor = eventsFor [ 'touch' ] ;
612+
613+ return this . handleDragStart . apply ( this , arguments ) ;
614+ } ,
615+
622616 render : function ( ) {
623617 // Create style object. We extend from existing styles so we don't
624618 // remove anything already set (like background, color, etc).
@@ -639,7 +633,14 @@ module.exports = React.createClass({
639633 this . state . clientY :
640634 0
641635 } ) ;
642- var style = assign ( { } , childStyle , transform ) ;
636+
637+ // Workaround IE pointer events; see #51
638+ // https://github.com/mzabriskie/react-draggable/issues/51#issuecomment-103488278
639+ var touchHacks = {
640+ touchAction : 'none'
641+ } ;
642+
643+ var style = assign ( { } , childStyle , transform , touchHacks ) ;
643644
644645 // Set zIndex if currently dragging and prop has been provided
645646 if ( this . state . dragging && ! isNaN ( this . props . zIndex ) ) {
@@ -657,12 +658,8 @@ module.exports = React.createClass({
657658 style : style ,
658659 className : className ,
659660
660- onMouseDown : this . handleDragStart ,
661- onTouchStart : function ( ev ) {
662- ev . preventDefault ( ) ; // prevent for scroll
663- return this . handleDragStart . apply ( this , arguments ) ;
664- } . bind ( this ) ,
665-
661+ onMouseDown : this . onMouseDown ,
662+ onTouchStart : this . onTouchStart ,
666663 onMouseUp : this . handleDragEnd ,
667664 onTouchEnd : this . handleDragEnd
668665 } ) ;
0 commit comments