angular-touch.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /**
  2. * @license AngularJS v1.4.4-build.4148+sha.dc0b856
  3. * (c) 2010-2015 Google, Inc. http://angularjs.org
  4. * License: MIT
  5. */
  6. (function(window, angular, undefined) {'use strict';
  7. /**
  8. * @ngdoc module
  9. * @name ngTouch
  10. * @description
  11. *
  12. * # ngTouch
  13. *
  14. * The `ngTouch` module provides touch events and other helpers for touch-enabled devices.
  15. * The implementation is based on jQuery Mobile touch event handling
  16. * ([jquerymobile.com](http://jquerymobile.com/)).
  17. *
  18. *
  19. * See {@link ngTouch.$swipe `$swipe`} for usage.
  20. *
  21. * <div doc-module-components="ngTouch"></div>
  22. *
  23. */
  24. // define ngTouch module
  25. /* global -ngTouch */
  26. var ngTouch = angular.module('ngTouch', []);
  27. function nodeName_(element) {
  28. return angular.lowercase(element.nodeName || (element[0] && element[0].nodeName));
  29. }
  30. /* global ngTouch: false */
  31. /**
  32. * @ngdoc service
  33. * @name $swipe
  34. *
  35. * @description
  36. * The `$swipe` service is a service that abstracts the messier details of hold-and-drag swipe
  37. * behavior, to make implementing swipe-related directives more convenient.
  38. *
  39. * Requires the {@link ngTouch `ngTouch`} module to be installed.
  40. *
  41. * `$swipe` is used by the `ngSwipeLeft` and `ngSwipeRight` directives in `ngTouch`, and by
  42. * `ngCarousel` in a separate component.
  43. *
  44. * # Usage
  45. * The `$swipe` service is an object with a single method: `bind`. `bind` takes an element
  46. * which is to be watched for swipes, and an object with four handler functions. See the
  47. * documentation for `bind` below.
  48. */
  49. ngTouch.factory('$swipe', [function() {
  50. // The total distance in any direction before we make the call on swipe vs. scroll.
  51. var MOVE_BUFFER_RADIUS = 10;
  52. var POINTER_EVENTS = {
  53. 'mouse': {
  54. start: 'mousedown',
  55. move: 'mousemove',
  56. end: 'mouseup'
  57. },
  58. 'touch': {
  59. start: 'touchstart',
  60. move: 'touchmove',
  61. end: 'touchend',
  62. cancel: 'touchcancel'
  63. }
  64. };
  65. function getCoordinates(event) {
  66. var originalEvent = event.originalEvent || event;
  67. var touches = originalEvent.touches && originalEvent.touches.length ? originalEvent.touches : [originalEvent];
  68. var e = (originalEvent.changedTouches && originalEvent.changedTouches[0]) || touches[0];
  69. return {
  70. x: e.clientX,
  71. y: e.clientY
  72. };
  73. }
  74. function getEvents(pointerTypes, eventType) {
  75. var res = [];
  76. angular.forEach(pointerTypes, function(pointerType) {
  77. var eventName = POINTER_EVENTS[pointerType][eventType];
  78. if (eventName) {
  79. res.push(eventName);
  80. }
  81. });
  82. return res.join(' ');
  83. }
  84. return {
  85. /**
  86. * @ngdoc method
  87. * @name $swipe#bind
  88. *
  89. * @description
  90. * The main method of `$swipe`. It takes an element to be watched for swipe motions, and an
  91. * object containing event handlers.
  92. * The pointer types that should be used can be specified via the optional
  93. * third argument, which is an array of strings `'mouse'` and `'touch'`. By default,
  94. * `$swipe` will listen for `mouse` and `touch` events.
  95. *
  96. * The four events are `start`, `move`, `end`, and `cancel`. `start`, `move`, and `end`
  97. * receive as a parameter a coordinates object of the form `{ x: 150, y: 310 }` and the raw
  98. * `event`. `cancel` receives the raw `event` as its single parameter.
  99. *
  100. * `start` is called on either `mousedown` or `touchstart`. After this event, `$swipe` is
  101. * watching for `touchmove` or `mousemove` events. These events are ignored until the total
  102. * distance moved in either dimension exceeds a small threshold.
  103. *
  104. * Once this threshold is exceeded, either the horizontal or vertical delta is greater.
  105. * - If the horizontal distance is greater, this is a swipe and `move` and `end` events follow.
  106. * - If the vertical distance is greater, this is a scroll, and we let the browser take over.
  107. * A `cancel` event is sent.
  108. *
  109. * `move` is called on `mousemove` and `touchmove` after the above logic has determined that
  110. * a swipe is in progress.
  111. *
  112. * `end` is called when a swipe is successfully completed with a `touchend` or `mouseup`.
  113. *
  114. * `cancel` is called either on a `touchcancel` from the browser, or when we begin scrolling
  115. * as described above.
  116. *
  117. */
  118. bind: function(element, eventHandlers, pointerTypes) {
  119. // Absolute total movement, used to control swipe vs. scroll.
  120. var totalX, totalY;
  121. // Coordinates of the start position.
  122. var startCoords;
  123. // Last event's position.
  124. var lastPos;
  125. // Whether a swipe is active.
  126. var active = false;
  127. pointerTypes = pointerTypes || ['mouse', 'touch'];
  128. element.on(getEvents(pointerTypes, 'start'), function(event) {
  129. startCoords = getCoordinates(event);
  130. active = true;
  131. totalX = 0;
  132. totalY = 0;
  133. lastPos = startCoords;
  134. eventHandlers['start'] && eventHandlers['start'](startCoords, event);
  135. });
  136. var events = getEvents(pointerTypes, 'cancel');
  137. if (events) {
  138. element.on(events, function(event) {
  139. active = false;
  140. eventHandlers['cancel'] && eventHandlers['cancel'](event);
  141. });
  142. }
  143. element.on(getEvents(pointerTypes, 'move'), function(event) {
  144. if (!active) return;
  145. // Android will send a touchcancel if it thinks we're starting to scroll.
  146. // So when the total distance (+ or - or both) exceeds 10px in either direction,
  147. // we either:
  148. // - On totalX > totalY, we send preventDefault() and treat this as a swipe.
  149. // - On totalY > totalX, we let the browser handle it as a scroll.
  150. if (!startCoords) return;
  151. var coords = getCoordinates(event);
  152. totalX += Math.abs(coords.x - lastPos.x);
  153. totalY += Math.abs(coords.y - lastPos.y);
  154. lastPos = coords;
  155. if (totalX < MOVE_BUFFER_RADIUS && totalY < MOVE_BUFFER_RADIUS) {
  156. return;
  157. }
  158. // One of totalX or totalY has exceeded the buffer, so decide on swipe vs. scroll.
  159. if (totalY > totalX) {
  160. // Allow native scrolling to take over.
  161. active = false;
  162. eventHandlers['cancel'] && eventHandlers['cancel'](event);
  163. return;
  164. } else {
  165. // Prevent the browser from scrolling.
  166. event.preventDefault();
  167. eventHandlers['move'] && eventHandlers['move'](coords, event);
  168. }
  169. });
  170. element.on(getEvents(pointerTypes, 'end'), function(event) {
  171. if (!active) return;
  172. active = false;
  173. eventHandlers['end'] && eventHandlers['end'](getCoordinates(event), event);
  174. });
  175. }
  176. };
  177. }]);
  178. /* global ngTouch: false,
  179. nodeName_: false
  180. */
  181. /**
  182. * @ngdoc directive
  183. * @name ngClick
  184. *
  185. * @description
  186. * A more powerful replacement for the default ngClick designed to be used on touchscreen
  187. * devices. Most mobile browsers wait about 300ms after a tap-and-release before sending
  188. * the click event. This version handles them immediately, and then prevents the
  189. * following click event from propagating.
  190. *
  191. * Requires the {@link ngTouch `ngTouch`} module to be installed.
  192. *
  193. * This directive can fall back to using an ordinary click event, and so works on desktop
  194. * browsers as well as mobile.
  195. *
  196. * This directive also sets the CSS class `ng-click-active` while the element is being held
  197. * down (by a mouse click or touch) so you can restyle the depressed element if you wish.
  198. *
  199. * @element ANY
  200. * @param {expression} ngClick {@link guide/expression Expression} to evaluate
  201. * upon tap. (Event object is available as `$event`)
  202. *
  203. * @example
  204. <example module="ngClickExample" deps="angular-touch.js">
  205. <file name="index.html">
  206. <button ng-click="count = count + 1" ng-init="count=0">
  207. Increment
  208. </button>
  209. count: {{ count }}
  210. </file>
  211. <file name="script.js">
  212. angular.module('ngClickExample', ['ngTouch']);
  213. </file>
  214. </example>
  215. */
  216. ngTouch.config(['$provide', function($provide) {
  217. $provide.decorator('ngClickDirective', ['$delegate', function($delegate) {
  218. // drop the default ngClick directive
  219. $delegate.shift();
  220. return $delegate;
  221. }]);
  222. }]);
  223. ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
  224. function($parse, $timeout, $rootElement) {
  225. var TAP_DURATION = 750; // Shorter than 750ms is a tap, longer is a taphold or drag.
  226. var MOVE_TOLERANCE = 12; // 12px seems to work in most mobile browsers.
  227. var PREVENT_DURATION = 2500; // 2.5 seconds maximum from preventGhostClick call to click
  228. var CLICKBUSTER_THRESHOLD = 25; // 25 pixels in any dimension is the limit for busting clicks.
  229. var ACTIVE_CLASS_NAME = 'ng-click-active';
  230. var lastPreventedTime;
  231. var touchCoordinates;
  232. var lastLabelClickCoordinates;
  233. // TAP EVENTS AND GHOST CLICKS
  234. //
  235. // Why tap events?
  236. // Mobile browsers detect a tap, then wait a moment (usually ~300ms) to see if you're
  237. // double-tapping, and then fire a click event.
  238. //
  239. // This delay sucks and makes mobile apps feel unresponsive.
  240. // So we detect touchstart, touchcancel and touchend ourselves and determine when
  241. // the user has tapped on something.
  242. //
  243. // What happens when the browser then generates a click event?
  244. // The browser, of course, also detects the tap and fires a click after a delay. This results in
  245. // tapping/clicking twice. We do "clickbusting" to prevent it.
  246. //
  247. // How does it work?
  248. // We attach global touchstart and click handlers, that run during the capture (early) phase.
  249. // So the sequence for a tap is:
  250. // - global touchstart: Sets an "allowable region" at the point touched.
  251. // - element's touchstart: Starts a touch
  252. // (- touchcancel ends the touch, no click follows)
  253. // - element's touchend: Determines if the tap is valid (didn't move too far away, didn't hold
  254. // too long) and fires the user's tap handler. The touchend also calls preventGhostClick().
  255. // - preventGhostClick() removes the allowable region the global touchstart created.
  256. // - The browser generates a click event.
  257. // - The global click handler catches the click, and checks whether it was in an allowable region.
  258. // - If preventGhostClick was called, the region will have been removed, the click is busted.
  259. // - If the region is still there, the click proceeds normally. Therefore clicks on links and
  260. // other elements without ngTap on them work normally.
  261. //
  262. // This is an ugly, terrible hack!
  263. // Yeah, tell me about it. The alternatives are using the slow click events, or making our users
  264. // deal with the ghost clicks, so I consider this the least of evils. Fortunately Angular
  265. // encapsulates this ugly logic away from the user.
  266. //
  267. // Why not just put click handlers on the element?
  268. // We do that too, just to be sure. If the tap event caused the DOM to change,
  269. // it is possible another element is now in that position. To take account for these possibly
  270. // distinct elements, the handlers are global and care only about coordinates.
  271. // Checks if the coordinates are close enough to be within the region.
  272. function hit(x1, y1, x2, y2) {
  273. return Math.abs(x1 - x2) < CLICKBUSTER_THRESHOLD && Math.abs(y1 - y2) < CLICKBUSTER_THRESHOLD;
  274. }
  275. // Checks a list of allowable regions against a click location.
  276. // Returns true if the click should be allowed.
  277. // Splices out the allowable region from the list after it has been used.
  278. function checkAllowableRegions(touchCoordinates, x, y) {
  279. for (var i = 0; i < touchCoordinates.length; i += 2) {
  280. if (hit(touchCoordinates[i], touchCoordinates[i + 1], x, y)) {
  281. touchCoordinates.splice(i, i + 2);
  282. return true; // allowable region
  283. }
  284. }
  285. return false; // No allowable region; bust it.
  286. }
  287. // Global click handler that prevents the click if it's in a bustable zone and preventGhostClick
  288. // was called recently.
  289. function onClick(event) {
  290. if (Date.now() - lastPreventedTime > PREVENT_DURATION) {
  291. return; // Too old.
  292. }
  293. var touches = event.touches && event.touches.length ? event.touches : [event];
  294. var x = touches[0].clientX;
  295. var y = touches[0].clientY;
  296. // Work around desktop Webkit quirk where clicking a label will fire two clicks (on the label
  297. // and on the input element). Depending on the exact browser, this second click we don't want
  298. // to bust has either (0,0), negative coordinates, or coordinates equal to triggering label
  299. // click event
  300. if (x < 1 && y < 1) {
  301. return; // offscreen
  302. }
  303. if (lastLabelClickCoordinates &&
  304. lastLabelClickCoordinates[0] === x && lastLabelClickCoordinates[1] === y) {
  305. return; // input click triggered by label click
  306. }
  307. // reset label click coordinates on first subsequent click
  308. if (lastLabelClickCoordinates) {
  309. lastLabelClickCoordinates = null;
  310. }
  311. // remember label click coordinates to prevent click busting of trigger click event on input
  312. if (nodeName_(event.target) === 'label') {
  313. lastLabelClickCoordinates = [x, y];
  314. }
  315. // Look for an allowable region containing this click.
  316. // If we find one, that means it was created by touchstart and not removed by
  317. // preventGhostClick, so we don't bust it.
  318. if (checkAllowableRegions(touchCoordinates, x, y)) {
  319. return;
  320. }
  321. // If we didn't find an allowable region, bust the click.
  322. event.stopPropagation();
  323. event.preventDefault();
  324. // Blur focused form elements
  325. event.target && event.target.blur && event.target.blur();
  326. }
  327. // Global touchstart handler that creates an allowable region for a click event.
  328. // This allowable region can be removed by preventGhostClick if we want to bust it.
  329. function onTouchStart(event) {
  330. var touches = event.touches && event.touches.length ? event.touches : [event];
  331. var x = touches[0].clientX;
  332. var y = touches[0].clientY;
  333. touchCoordinates.push(x, y);
  334. $timeout(function() {
  335. // Remove the allowable region.
  336. for (var i = 0; i < touchCoordinates.length; i += 2) {
  337. if (touchCoordinates[i] == x && touchCoordinates[i + 1] == y) {
  338. touchCoordinates.splice(i, i + 2);
  339. return;
  340. }
  341. }
  342. }, PREVENT_DURATION, false);
  343. }
  344. // On the first call, attaches some event handlers. Then whenever it gets called, it creates a
  345. // zone around the touchstart where clicks will get busted.
  346. function preventGhostClick(x, y) {
  347. if (!touchCoordinates) {
  348. $rootElement[0].addEventListener('click', onClick, true);
  349. $rootElement[0].addEventListener('touchstart', onTouchStart, true);
  350. touchCoordinates = [];
  351. }
  352. lastPreventedTime = Date.now();
  353. checkAllowableRegions(touchCoordinates, x, y);
  354. }
  355. // Actual linking function.
  356. return function(scope, element, attr) {
  357. var clickHandler = $parse(attr.ngClick),
  358. tapping = false,
  359. tapElement, // Used to blur the element after a tap.
  360. startTime, // Used to check if the tap was held too long.
  361. touchStartX,
  362. touchStartY;
  363. function resetState() {
  364. tapping = false;
  365. element.removeClass(ACTIVE_CLASS_NAME);
  366. }
  367. element.on('touchstart', function(event) {
  368. tapping = true;
  369. tapElement = event.target ? event.target : event.srcElement; // IE uses srcElement.
  370. // Hack for Safari, which can target text nodes instead of containers.
  371. if (tapElement.nodeType == 3) {
  372. tapElement = tapElement.parentNode;
  373. }
  374. element.addClass(ACTIVE_CLASS_NAME);
  375. startTime = Date.now();
  376. // Use jQuery originalEvent
  377. var originalEvent = event.originalEvent || event;
  378. var touches = originalEvent.touches && originalEvent.touches.length ? originalEvent.touches : [originalEvent];
  379. var e = touches[0];
  380. touchStartX = e.clientX;
  381. touchStartY = e.clientY;
  382. });
  383. element.on('touchcancel', function(event) {
  384. resetState();
  385. });
  386. element.on('touchend', function(event) {
  387. var diff = Date.now() - startTime;
  388. // Use jQuery originalEvent
  389. var originalEvent = event.originalEvent || event;
  390. var touches = (originalEvent.changedTouches && originalEvent.changedTouches.length) ?
  391. originalEvent.changedTouches :
  392. ((originalEvent.touches && originalEvent.touches.length) ? originalEvent.touches : [originalEvent]);
  393. var e = touches[0];
  394. var x = e.clientX;
  395. var y = e.clientY;
  396. var dist = Math.sqrt(Math.pow(x - touchStartX, 2) + Math.pow(y - touchStartY, 2));
  397. if (tapping && diff < TAP_DURATION && dist < MOVE_TOLERANCE) {
  398. // Call preventGhostClick so the clickbuster will catch the corresponding click.
  399. preventGhostClick(x, y);
  400. // Blur the focused element (the button, probably) before firing the callback.
  401. // This doesn't work perfectly on Android Chrome, but seems to work elsewhere.
  402. // I couldn't get anything to work reliably on Android Chrome.
  403. if (tapElement) {
  404. tapElement.blur();
  405. }
  406. if (!angular.isDefined(attr.disabled) || attr.disabled === false) {
  407. element.triggerHandler('click', [event]);
  408. }
  409. }
  410. resetState();
  411. });
  412. // Hack for iOS Safari's benefit. It goes searching for onclick handlers and is liable to click
  413. // something else nearby.
  414. element.onclick = function(event) { };
  415. // Actual click handler.
  416. // There are three different kinds of clicks, only two of which reach this point.
  417. // - On desktop browsers without touch events, their clicks will always come here.
  418. // - On mobile browsers, the simulated "fast" click will call this.
  419. // - But the browser's follow-up slow click will be "busted" before it reaches this handler.
  420. // Therefore it's safe to use this directive on both mobile and desktop.
  421. element.on('click', function(event, touchend) {
  422. scope.$apply(function() {
  423. clickHandler(scope, {$event: (touchend || event)});
  424. });
  425. });
  426. element.on('mousedown', function(event) {
  427. element.addClass(ACTIVE_CLASS_NAME);
  428. });
  429. element.on('mousemove mouseup', function(event) {
  430. element.removeClass(ACTIVE_CLASS_NAME);
  431. });
  432. };
  433. }]);
  434. /* global ngTouch: false */
  435. /**
  436. * @ngdoc directive
  437. * @name ngSwipeLeft
  438. *
  439. * @description
  440. * Specify custom behavior when an element is swiped to the left on a touchscreen device.
  441. * A leftward swipe is a quick, right-to-left slide of the finger.
  442. * Though ngSwipeLeft is designed for touch-based devices, it will work with a mouse click and drag
  443. * too.
  444. *
  445. * To disable the mouse click and drag functionality, add `ng-swipe-disable-mouse` to
  446. * the `ng-swipe-left` or `ng-swipe-right` DOM Element.
  447. *
  448. * Requires the {@link ngTouch `ngTouch`} module to be installed.
  449. *
  450. * @element ANY
  451. * @param {expression} ngSwipeLeft {@link guide/expression Expression} to evaluate
  452. * upon left swipe. (Event object is available as `$event`)
  453. *
  454. * @example
  455. <example module="ngSwipeLeftExample" deps="angular-touch.js">
  456. <file name="index.html">
  457. <div ng-show="!showActions" ng-swipe-left="showActions = true">
  458. Some list content, like an email in the inbox
  459. </div>
  460. <div ng-show="showActions" ng-swipe-right="showActions = false">
  461. <button ng-click="reply()">Reply</button>
  462. <button ng-click="delete()">Delete</button>
  463. </div>
  464. </file>
  465. <file name="script.js">
  466. angular.module('ngSwipeLeftExample', ['ngTouch']);
  467. </file>
  468. </example>
  469. */
  470. /**
  471. * @ngdoc directive
  472. * @name ngSwipeRight
  473. *
  474. * @description
  475. * Specify custom behavior when an element is swiped to the right on a touchscreen device.
  476. * A rightward swipe is a quick, left-to-right slide of the finger.
  477. * Though ngSwipeRight is designed for touch-based devices, it will work with a mouse click and drag
  478. * too.
  479. *
  480. * Requires the {@link ngTouch `ngTouch`} module to be installed.
  481. *
  482. * @element ANY
  483. * @param {expression} ngSwipeRight {@link guide/expression Expression} to evaluate
  484. * upon right swipe. (Event object is available as `$event`)
  485. *
  486. * @example
  487. <example module="ngSwipeRightExample" deps="angular-touch.js">
  488. <file name="index.html">
  489. <div ng-show="!showActions" ng-swipe-left="showActions = true">
  490. Some list content, like an email in the inbox
  491. </div>
  492. <div ng-show="showActions" ng-swipe-right="showActions = false">
  493. <button ng-click="reply()">Reply</button>
  494. <button ng-click="delete()">Delete</button>
  495. </div>
  496. </file>
  497. <file name="script.js">
  498. angular.module('ngSwipeRightExample', ['ngTouch']);
  499. </file>
  500. </example>
  501. */
  502. function makeSwipeDirective(directiveName, direction, eventName) {
  503. ngTouch.directive(directiveName, ['$parse', '$swipe', function($parse, $swipe) {
  504. // The maximum vertical delta for a swipe should be less than 75px.
  505. var MAX_VERTICAL_DISTANCE = 75;
  506. // Vertical distance should not be more than a fraction of the horizontal distance.
  507. var MAX_VERTICAL_RATIO = 0.3;
  508. // At least a 30px lateral motion is necessary for a swipe.
  509. var MIN_HORIZONTAL_DISTANCE = 30;
  510. return function(scope, element, attr) {
  511. var swipeHandler = $parse(attr[directiveName]);
  512. var startCoords, valid;
  513. function validSwipe(coords) {
  514. // Check that it's within the coordinates.
  515. // Absolute vertical distance must be within tolerances.
  516. // Horizontal distance, we take the current X - the starting X.
  517. // This is negative for leftward swipes and positive for rightward swipes.
  518. // After multiplying by the direction (-1 for left, +1 for right), legal swipes
  519. // (ie. same direction as the directive wants) will have a positive delta and
  520. // illegal ones a negative delta.
  521. // Therefore this delta must be positive, and larger than the minimum.
  522. if (!startCoords) return false;
  523. var deltaY = Math.abs(coords.y - startCoords.y);
  524. var deltaX = (coords.x - startCoords.x) * direction;
  525. return valid && // Short circuit for already-invalidated swipes.
  526. deltaY < MAX_VERTICAL_DISTANCE &&
  527. deltaX > 0 &&
  528. deltaX > MIN_HORIZONTAL_DISTANCE &&
  529. deltaY / deltaX < MAX_VERTICAL_RATIO;
  530. }
  531. var pointerTypes = ['touch'];
  532. if (!angular.isDefined(attr['ngSwipeDisableMouse'])) {
  533. pointerTypes.push('mouse');
  534. }
  535. $swipe.bind(element, {
  536. 'start': function(coords, event) {
  537. startCoords = coords;
  538. valid = true;
  539. },
  540. 'cancel': function(event) {
  541. valid = false;
  542. },
  543. 'end': function(coords, event) {
  544. if (validSwipe(coords)) {
  545. scope.$apply(function() {
  546. element.triggerHandler(eventName);
  547. swipeHandler(scope, {$event: event});
  548. });
  549. }
  550. }
  551. }, pointerTypes);
  552. };
  553. }]);
  554. }
  555. // Left is negative X-coordinate, right is positive.
  556. makeSwipeDirective('ngSwipeLeft', -1, 'swipeleft');
  557. makeSwipeDirective('ngSwipeRight', 1, 'swiperight');
  558. })(window, window.angular);