Guest User

Untitled

a guest
Nov 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. app.directive('onLongPress', function($timeout) {
  2. //Global variable, to cancel timer on touchend.
  3. var timer;
  4.  
  5. return {
  6. restrict: 'A',
  7. link: function($scope, $elm, $attrs) {
  8. $elm.bind('touchstart', function(evt) {
  9. // Locally scoped variable that will keep track of the long press
  10. $scope.longPress = true;
  11.  
  12. // We'll set a timeout for 600 ms for a long press
  13. timer = $timeout(function() {
  14. if ($scope.longPress) {
  15. // If the touchend event hasn't fired,
  16. // apply the function given in on the element's on-long-press attribute
  17. $scope.$apply(function() {
  18. $scope.$eval($attrs.onLongPress)
  19. });
  20. }
  21. }, 600);
  22. timer;
  23. });
  24.  
  25. $elm.bind('touchend', function(evt) {
  26. // Prevent the onLongPress event from firing
  27. $scope.longPress = false;
  28.  
  29. // Prevent on quick presses, unwanted onLongPress selection.
  30. $timeout.cancel(timer);
  31.  
  32. // If there is an on-touch-end function attached to this element, apply it
  33. if ($attrs.onTouchEnd) {
  34. $scope.$apply(function() {
  35. $scope.$eval($attrs.onTouchEnd)
  36. });
  37. }
  38. });
  39. }
  40. };
  41. });
Add Comment
Please, Sign In to add comment