|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc directive |
| 5 | + * @name ngMobile.directive:ngSwipeLeft |
| 6 | + * |
| 7 | + * @description |
| 8 | + * Specify custom behavior when an element is swiped to the left on a touchscreen device. |
| 9 | + * A leftward swipe is a quick, right-to-left slide of the finger. |
| 10 | + * Though ngSwipeLeft is designed for touch-based devices, it will work with a mouse click and drag too. |
| 11 | + * |
| 12 | + * @element ANY |
| 13 | + * @param {expression} ngSwipeLeft {@link guide/expression Expression} to evaluate |
| 14 | + * upon left swipe. (Event object is available as `$event`) |
| 15 | + * |
| 16 | + * @example |
| 17 | + <doc:example> |
| 18 | + <doc:source> |
| 19 | + <div ng-show="!showActions" ng-swipe-left="showActions = true"> |
| 20 | + Some list content, like an email in the inbox |
| 21 | + </div> |
| 22 | + <div ng-show="showActions" ng-swipe-right="showActions = false"> |
| 23 | + <button ng-click="reply()">Reply</button> |
| 24 | + <button ng-click="delete()">Delete</button> |
| 25 | + </div> |
| 26 | + </doc:source> |
| 27 | + </doc:example> |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * @ngdoc directive |
| 32 | + * @name ngMobile.directive:ngSwipeRight |
| 33 | + * |
| 34 | + * @description |
| 35 | + * Specify custom behavior when an element is swiped to the right on a touchscreen device. |
| 36 | + * A rightward swipe is a quick, left-to-right slide of the finger. |
| 37 | + * Though ngSwipeRight is designed for touch-based devices, it will work with a mouse click and drag too. |
| 38 | + * |
| 39 | + * @element ANY |
| 40 | + * @param {expression} ngSwipeRight {@link guide/expression Expression} to evaluate |
| 41 | + * upon right swipe. (Event object is available as `$event`) |
| 42 | + * |
| 43 | + * @example |
| 44 | + <doc:example> |
| 45 | + <doc:source> |
| 46 | + <div ng-show="!showActions" ng-swipe-left="showActions = true"> |
| 47 | + Some list content, like an email in the inbox |
| 48 | + </div> |
| 49 | + <div ng-show="showActions" ng-swipe-right="showActions = false"> |
| 50 | + <button ng-click="reply()">Reply</button> |
| 51 | + <button ng-click="delete()">Delete</button> |
| 52 | + </div> |
| 53 | + </doc:source> |
| 54 | + </doc:example> |
| 55 | + */ |
| 56 | + |
| 57 | +function makeSwipeDirective(directiveName, direction) { |
| 58 | + ngMobile.directive(directiveName, ['$parse', function($parse) { |
| 59 | + // The maximum vertical delta for a swipe should be less than 75px. |
| 60 | + var MAX_VERTICAL_DISTANCE = 75; |
| 61 | + // Vertical distance should not be more than a fraction of the horizontal distance. |
| 62 | + var MAX_VERTICAL_RATIO = 0.3; |
| 63 | + // At least a 30px lateral motion is necessary for a swipe. |
| 64 | + var MIN_HORIZONTAL_DISTANCE = 30; |
| 65 | + // The total distance in any direction before we make the call on swipe vs. scroll. |
| 66 | + var MOVE_BUFFER_RADIUS = 10; |
| 67 | + |
| 68 | + function getCoordinates(event) { |
| 69 | + var touches = event.touches && event.touches.length ? event.touches : [event]; |
| 70 | + var e = (event.changedTouches && event.changedTouches[0]) || |
| 71 | + (event.originalEvent && event.originalEvent.changedTouches && |
| 72 | + event.originalEvent.changedTouches[0]) || |
| 73 | + touches[0].originalEvent || touches[0]; |
| 74 | + |
| 75 | + return { |
| 76 | + x: e.clientX, |
| 77 | + y: e.clientY |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + return function(scope, element, attr) { |
| 82 | + var swipeHandler = $parse(attr[directiveName]); |
| 83 | + var startCoords, valid; |
| 84 | + var totalX, totalY; |
| 85 | + var lastX, lastY; |
| 86 | + |
| 87 | + function validSwipe(event) { |
| 88 | + // Check that it's within the coordinates. |
| 89 | + // Absolute vertical distance must be within tolerances. |
| 90 | + // Horizontal distance, we take the current X - the starting X. |
| 91 | + // This is negative for leftward swipes and positive for rightward swipes. |
| 92 | + // After multiplying by the direction (-1 for left, +1 for right), legal swipes |
| 93 | + // (ie. same direction as the directive wants) will have a positive delta and |
| 94 | + // illegal ones a negative delta. |
| 95 | + // Therefore this delta must be positive, and larger than the minimum. |
| 96 | + if (!startCoords) return false; |
| 97 | + var coords = getCoordinates(event); |
| 98 | + var deltaY = Math.abs(coords.y - startCoords.y); |
| 99 | + var deltaX = (coords.x - startCoords.x) * direction; |
| 100 | + return valid && // Short circuit for already-invalidated swipes. |
| 101 | + deltaY < MAX_VERTICAL_DISTANCE && |
| 102 | + deltaX > 0 && |
| 103 | + deltaX > MIN_HORIZONTAL_DISTANCE && |
| 104 | + deltaY / deltaX < MAX_VERTICAL_RATIO; |
| 105 | + } |
| 106 | + |
| 107 | + element.bind('touchstart mousedown', function(event) { |
| 108 | + startCoords = getCoordinates(event); |
| 109 | + valid = true; |
| 110 | + totalX = 0; |
| 111 | + totalY = 0; |
| 112 | + lastX = startCoords.x; |
| 113 | + lastY = startCoords.y; |
| 114 | + }); |
| 115 | + |
| 116 | + element.bind('touchcancel', function(event) { |
| 117 | + valid = false; |
| 118 | + }); |
| 119 | + |
| 120 | + element.bind('touchmove mousemove', function(event) { |
| 121 | + if (!valid) return; |
| 122 | + |
| 123 | + // Android will send a touchcancel if it thinks we're starting to scroll. |
| 124 | + // So when the total distance (+ or - or both) exceeds 10px in either direction, |
| 125 | + // we either: |
| 126 | + // - On totalX > totalY, we send preventDefault() and treat this as a swipe. |
| 127 | + // - On totalY > totalX, we let the browser handle it as a scroll. |
| 128 | + |
| 129 | + // Invalidate a touch while it's in progress if it strays too far away vertically. |
| 130 | + // We don't want a scroll down and back up while drifting sideways to be a swipe just |
| 131 | + // because you happened to end up vertically close in the end. |
| 132 | + if (!startCoords) return; |
| 133 | + var coords = getCoordinates(event); |
| 134 | + |
| 135 | + if (Math.abs(coords.y - startCoords.y) > MAX_VERTICAL_DISTANCE) { |
| 136 | + valid = false; |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + totalX += Math.abs(coords.x - lastX); |
| 141 | + totalY += Math.abs(coords.y - lastY); |
| 142 | + |
| 143 | + lastX = coords.x; |
| 144 | + lastY = coords.y; |
| 145 | + |
| 146 | + if (totalX < MOVE_BUFFER_RADIUS && totalY < MOVE_BUFFER_RADIUS) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // One of totalX or totalY has exceeded the buffer, so decide on swipe vs. scroll. |
| 151 | + if (totalY > totalX) { |
| 152 | + valid = false; |
| 153 | + return; |
| 154 | + } else { |
| 155 | + event.preventDefault(); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + element.bind('touchend mouseup', function(event) { |
| 160 | + if (validSwipe(event)) { |
| 161 | + // Prevent this swipe from bubbling up to any other elements with ngSwipes. |
| 162 | + event.stopPropagation(); |
| 163 | + scope.$apply(function() { |
| 164 | + swipeHandler(scope, {$event:event}); |
| 165 | + }); |
| 166 | + } |
| 167 | + }); |
| 168 | + }; |
| 169 | + }]); |
| 170 | +} |
| 171 | + |
| 172 | +// Left is negative X-coordinate, right is positive. |
| 173 | +makeSwipeDirective('ngSwipeLeft', -1); |
| 174 | +makeSwipeDirective('ngSwipeRight', 1); |
| 175 | + |
0 commit comments