UNPKG

doevisualizations

Version:

Data Visualization Library based on RequireJS and D3.js (v4+)

142 lines (131 loc) 3.67 kB
steal('jquery', 'jquerypp/event/livehack', function($) { var isPhantom = /Phantom/.test(navigator.userAgent), supportTouch = !isPhantom && "ontouchend" in document, scrollEvent = "touchmove scroll", // Use touch events or map it to mouse events touchStartEvent = supportTouch ? "touchstart" : "mousedown", touchStopEvent = supportTouch ? "touchend" : "mouseup", touchMoveEvent = supportTouch ? "touchmove" : "mousemove", data = function(event){ var d = event.originalEvent.touches ? event.originalEvent.touches[ 0 ] : event; return { time: (new Date).getTime(), coords: [ d.clientX, d.clientY ], origin: $( event.target ) }; }; /** * @add jQuery.event.swipe */ var swipe = $.event.swipe = { /** * @property {Number} jQuery.event.swipe.delay delay * @parent jQuery.event.swipe * * @body * * Delay is the upper limit of time the swipe motion can take in milliseconds. This defaults to 500. * * A user must perform the swipe motion in this much time. */ delay : 500, /** * @property {Number} jQuery.event.swipe.max max * @parent jQuery.event.swipe * * @body * * The maximum distance the pointer must travel in pixels. The default is 75 pixels. */ max : 320, /** * @property {Number} jQuery.event.swipe.min min * @parent jQuery.event.swipe * * @body * * The minimum distance the pointer must travel in pixels. The default is 30 pixels. */ min : 30 }; $.event.setupHelper( [ /** * @hide * @attribute swipe */ "swipe", /** * @hide * @attribute swipeleft */ 'swipeleft', /** * @hide * @attribute swiperight */ 'swiperight', /** * @hide * @attribute swipeup */ 'swipeup', /** * @hide * @attribute swipedown */ 'swipedown'], touchStartEvent, function(ev){ var // update with data when the event was started start = data(ev), stop, delegate = ev.delegateTarget || ev.currentTarget, selector = ev.handleObj.selector, entered = this; function moveHandler(event){ if ( !start ) { return; } // update stop with the data from the current event stop = data(event); // prevent scrolling if ( Math.abs( start.coords[0] - stop.coords[0] ) > 10 ) { event.preventDefault(); } }; // Attach to the touch move events $(document.documentElement).bind(touchMoveEvent, moveHandler) .one(touchStopEvent, function(event){ $(this).unbind( touchMoveEvent, moveHandler); // if start and stop contain data figure out if we have a swipe event if ( start && stop ) { // calculate the distance between start and stop data var deltaX = Math.abs(start.coords[0] - stop.coords[0]), deltaY = Math.abs(start.coords[1] - stop.coords[1]), distance = Math.sqrt(deltaX*deltaX+deltaY*deltaY); // check if the delay and distance are matched if ( stop.time - start.time < swipe.delay && distance >= swipe.min && distance <= swipe.max ) { var events = ['swipe']; // check if we moved horizontally if( deltaX >= swipe.min && deltaY < swipe.min) { // based on the x coordinate check if we moved left or right events.push( start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight" ); } else // check if we moved vertically if(deltaY >= swipe.min && deltaX < swipe.min){ // based on the y coordinate check if we moved up or down events.push( start.coords[1] < stop.coords[1] ? "swipedown" : "swipeup" ); } // trigger swipe events on this guy $.each($.event.find(delegate, events, selector), function(){ this.call(entered, ev, {start : start, end: stop}) }) } } // reset start and stop start = stop = undefined; }) }); return $; });