SHARE
TWEET

Untitled

a guest Dec 8th, 2014 142 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .directive('interactDrag', function() {
  2.   return {
  3.     restrict: 'E',
  4.     require: 'ngModel',
  5.     link: function(scope, element, attrs, ngModel) {
  6.       console.log(element);
  7.       interact('.draggable')
  8.         .draggable({
  9.             // allow dragging of multple elements at the same time
  10.             max: Infinity,
  11.  
  12.             // call this function on every dragmove event
  13.             onmove: function (event) {
  14.                 var target = event.target,
  15.                     // keep the dragged position in the data-x/data-y attributes
  16.                     x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
  17.                     y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
  18.  
  19.                 // translate the element
  20.                 target.style.webkitTransform =
  21.                 target.style.transform =
  22.                     'translate(' + x + 'px, ' + y + 'px)';
  23.  
  24.                 // update the posiion attributes
  25.                 target.setAttribute('data-x', x);
  26.                 target.setAttribute('data-y', y);
  27.                 ngModel.$setViewValue("moved");
  28.                 scope.$apply();
  29.             },
  30.             // call this function on every dragend event
  31.             onend: function (event) {
  32.                 var textEl = event.target.querySelector('p');
  33.                
  34.                 textEl && (textEl.textContent =
  35.                     'moved a distance of '
  36.                     + (Math.sqrt(event.dx * event.dx +
  37.                                  event.dy * event.dy)|0) + 'px');
  38.             }
  39.         })
  40.         // enable inertial throwing
  41.         .inertia(true)
  42.         // keep the element within the area of it's parent
  43.         .restrict({
  44.             drag: "parent",
  45.             endOnly: true,
  46.             elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
  47.         });
  48.  
  49.         // allow more than one interaction at a time
  50.         interact.maxInteractions(Infinity);
  51.     }
  52.   }
  53. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top