|
| 1 | +'use strict'; |
| 2 | + |
| 3 | + /** |
| 4 | + * @ngdoc object |
| 5 | + * @name ngMobile.$swipe |
| 6 | + * |
| 7 | + * @description |
| 8 | + * The `$swipe` service is a service that abstracts the messier details of hold-and-drag swipe |
| 9 | + * behavior, to make implementing swipe-related directives more convenient. |
| 10 | + * |
| 11 | + * It is used by the `ngSwipeLeft` and `ngSwipeRight` directives in `ngMobile`, and by |
| 12 | + * `ngCarousel` in a separate component. |
| 13 | + * |
| 14 | + * # Usage |
| 15 | + * The `$swipe` service is an object with a single method: `bind`. `bind` takes an element |
| 16 | + * which is to be watched for swipes, and an object with four handler functions. See the |
| 17 | + * documentation for `bind` below. |
| 18 | + */ |
| 19 | + |
| 20 | +ngMobile.factory('$swipe', [function() { |
| 21 | + // The total distance in any direction before we make the call on swipe vs. scroll. |
| 22 | + var MOVE_BUFFER_RADIUS = 10; |
| 23 | + |
| 24 | + function getCoordinates(event) { |
| 25 | + var touches = event.touches && event.touches.length ? event.touches : [event]; |
| 26 | + var e = (event.changedTouches && event.changedTouches[0]) || |
| 27 | + (event.originalEvent && event.originalEvent.changedTouches && |
| 28 | + event.originalEvent.changedTouches[0]) || |
| 29 | + touches[0].originalEvent || touches[0]; |
| 30 | + |
| 31 | + return { |
| 32 | + x: e.clientX, |
| 33 | + y: e.clientY |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + return { |
| 38 | + /** |
| 39 | + * @ngdoc method |
| 40 | + * @name ngMobile.$swipe#bind |
| 41 | + * @methodOf ngMobile.$swipe |
| 42 | + * |
| 43 | + * @description |
| 44 | + * The main method of `$swipe`. It takes an element to be watched for swipe motions, and an |
| 45 | + * object containing event handlers. |
| 46 | + * |
| 47 | + * The four events are `start`, `move`, `end`, and `cancel`. `start`, `move`, and `end` |
| 48 | + * receive as a parameter a coordinates object of the form `{ x: 150, y: 310 }`. |
| 49 | + * |
| 50 | + * `start` is called on either `mousedown` or `touchstart`. After this event, `$swipe` is |
| 51 | + * watching for `touchmove` or `mousemove` events. These events are ignored until the total |
| 52 | + * distance moved in either dimension exceeds a small threshold. |
| 53 | + * |
| 54 | + * Once this threshold is exceeded, either the horizontal or vertical delta is greater. |
| 55 | + * - If the horizontal distance is greater, this is a swipe and `move` and `end` events follow. |
| 56 | + * - If the vertical distance is greater, this is a scroll, and we let the browser take over. |
| 57 | + * A `cancel` event is sent. |
| 58 | + * |
| 59 | + * `move` is called on `mousemove` and `touchmove` after the above logic has determined that |
| 60 | + * a swipe is in progress. |
| 61 | + * |
| 62 | + * `end` is called when a swipe is successfully completed with a `touchend` or `mouseup`. |
| 63 | + * |
| 64 | + * `cancel` is called either on a `touchcancel` from the browser, or when we begin scrolling |
| 65 | + * as described above. |
| 66 | + * |
| 67 | + */ |
| 68 | + bind: function(element, eventHandlers) { |
| 69 | + // Absolute total movement, used to control swipe vs. scroll. |
| 70 | + var totalX, totalY; |
| 71 | + // Coordinates of the start position. |
| 72 | + var startCoords; |
| 73 | + // Last event's position. |
| 74 | + var lastPos; |
| 75 | + // Whether a swipe is active. |
| 76 | + var active = false; |
| 77 | + |
| 78 | + element.bind('touchstart mousedown', function(event) { |
| 79 | + startCoords = getCoordinates(event); |
| 80 | + active = true; |
| 81 | + totalX = 0; |
| 82 | + totalY = 0; |
| 83 | + lastPos = startCoords; |
| 84 | + eventHandlers['start'] && eventHandlers['start'](startCoords); |
| 85 | + }); |
| 86 | + |
| 87 | + element.bind('touchcancel', function(event) { |
| 88 | + active = false; |
| 89 | + eventHandlers['cancel'] && eventHandlers['cancel'](); |
| 90 | + }); |
| 91 | + |
| 92 | + element.bind('touchmove mousemove', function(event) { |
| 93 | + if (!active) return; |
| 94 | + |
| 95 | + // Android will send a touchcancel if it thinks we're starting to scroll. |
| 96 | + // So when the total distance (+ or - or both) exceeds 10px in either direction, |
| 97 | + // we either: |
| 98 | + // - On totalX > totalY, we send preventDefault() and treat this as a swipe. |
| 99 | + // - On totalY > totalX, we let the browser handle it as a scroll. |
| 100 | + |
| 101 | + if (!startCoords) return; |
| 102 | + var coords = getCoordinates(event); |
| 103 | + |
| 104 | + totalX += Math.abs(coords.x - lastPos.x); |
| 105 | + totalY += Math.abs(coords.y - lastPos.y); |
| 106 | + |
| 107 | + lastPos = coords; |
| 108 | + |
| 109 | + if (totalX < MOVE_BUFFER_RADIUS && totalY < MOVE_BUFFER_RADIUS) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // One of totalX or totalY has exceeded the buffer, so decide on swipe vs. scroll. |
| 114 | + if (totalY > totalX) { |
| 115 | + // Allow native scrolling to take over. |
| 116 | + active = false; |
| 117 | + eventHandlers['cancel'] && eventHandlers['cancel'](); |
| 118 | + return; |
| 119 | + } else { |
| 120 | + // Prevent the browser from scrolling. |
| 121 | + event.preventDefault(); |
| 122 | + |
| 123 | + eventHandlers['move'] && eventHandlers['move'](coords); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + element.bind('touchend mouseup', function(event) { |
| 128 | + if (!active) return; |
| 129 | + active = false; |
| 130 | + eventHandlers['end'] && eventHandlers['end'](getCoordinates(event)); |
| 131 | + }); |
| 132 | + } |
| 133 | + }; |
| 134 | +}]); |
| 135 | + |
| 136 | + |
0 commit comments