forked from fatlinesofcode/ngDraggable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngDraggable.js
444 lines (379 loc) · 19.2 KB
/
ngDraggable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
*
* https://github.com/fatlinesofcode/ngDraggable
*/
angular.module("ngDraggable", [])
.service('ngDraggable', ['$window', function($window) {
var isDef = function(val) { return typeof val !== 'undefined'; };
this.getEventProp = function getEventProp(evt, prop, skipOriginal) {
if (isDef(evt.touches) && evt.touches[0]) {
return evt.touches[0][prop];
}
if (isDef(evt[prop])) {
return evt[prop];
}
if (evt.originalEvent && !skipOriginal) {
return this.getEventProp(evt.originalEvent, prop, true);
}
};
this.getPrivOffset = function getPrivOffset(docElem) {
var box = { top: 0, left: 0 };
if (isDef(docElem[0].getBoundingClientRect)) {
box = docElem[0].getBoundingClientRect();
}
return {
top: box.top + $window.pageYOffset - docElem[0].clientTop,
left: box.left + $window.pageXOffset - docElem[0].clientLeft
};
}
}])
.directive('ngDrag', ['$rootScope', '$parse', '$document', '$window', 'ngDraggable', function ($rootScope, $parse, $document, $window, ngDraggable) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.value = attrs.ngDrag;
var offset,_centerAnchor=false,_mx,_my,_tx,_ty,_mrx,_mry;
var _hasTouch = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
var _pressEvents = 'touchstart mousedown';
var _moveEvents = 'touchmove mousemove';
var _releaseEvents = 'touchend mouseup';
// to identify the element in order to prevent getting superflous events when a single element has both drag and drop directives on it.
var _myid = scope.$id;
var _data = null;
var _dragOffset = null;
var _dragEnabled = false;
var _pressTimer = null;
var onDragSuccessCallback = $parse(attrs.ngDragSuccess) || null;
var initialize = function () {
element.attr('draggable', 'false'); // prevent native drag
toggleListeners(true);
};
var toggleListeners = function (enable) {
if (!enable)return;
// add listeners.
scope.$on('$destroy', onDestroy);
scope.$watch(attrs.ngDrag, onEnableChange);
scope.$watch(attrs.ngCenterAnchor, onCenterAnchor);
scope.$watch(attrs.ngDragData, onDragDataChange);
element.on(_pressEvents, onpress);
if(! _hasTouch && element[0].nodeName.toLowerCase() == "img"){
element.on('mousedown', function(){ return false;}); // prevent native drag for images
}
};
var onDestroy = function (enable) {
toggleListeners(false);
};
var onDragDataChange = function (newVal, oldVal) {
_data = newVal;
};
var onEnableChange = function (newVal, oldVal) {
_dragEnabled = (newVal);
};
var onCenterAnchor = function (newVal, oldVal) {
if(angular.isDefined(newVal))
_centerAnchor = (newVal || 'true');
}
var isClickableElement = function (evt) {
return (
angular.isDefined(angular.element(evt.target).attr("ng-click"))
|| angular.isDefined(angular.element(evt.target).attr("ng-dblclick"))
|| angular.isDefined(angular.element(evt.target).attr("ng-cancel-drag"))
);
}
/*
* When the element is clicked start the drag behaviour
* On touch devices as a small delay so as not to prevent native window scrolling
*/
var onpress = function(evt) {
if(! _dragEnabled)return;
// disable drag on clickable element
if (isClickableElement(evt)) {
return;
}
if(_hasTouch){
cancelPress();
_pressTimer = setTimeout(function(){
cancelPress();
onlongpress(evt);
},100);
$document.on(_moveEvents, cancelPress);
$document.on(_releaseEvents, cancelPress);
}else{
onlongpress(evt);
}
};
var cancelPress = function() {
clearTimeout(_pressTimer);
$document.off(_moveEvents, cancelPress);
$document.off(_releaseEvents, cancelPress);
};
var onlongpress = function(evt) {
if(! _dragEnabled)return;
evt.preventDefault();
element.addClass('dragging');
offset = ngDraggable.getPrivOffset(element);
_dragOffset = offset;
element.centerX = element[0].offsetWidth / 2;
element.centerY = element[0].offsetHeight / 2;
_mx = ngDraggable.getEventProp(evt, 'pageX');
_my = ngDraggable.getEventProp(evt, 'pageY');
_mrx = _mx - offset.left;
_mry = _my - offset.top;
if (_centerAnchor) {
_tx = _mx - element.centerX - $window.pageXOffset;
_ty = _my - element.centerY - $window.pageYOffset;
} else {
_tx = _mx - _mrx - $window.pageXOffset;
_ty = _my - _mry - $window.pageYOffset;
}
$document.on(_moveEvents, onmove);
$document.on(_releaseEvents, onrelease);
$rootScope.$broadcast('draggable:start', {x:_mx, y:_my, tx:_tx, ty:_ty, event:evt, element:element, data:_data});
}
var onmove = function (evt) {
if (!_dragEnabled)return;
evt.preventDefault();
_mx = ngDraggable.getEventProp(evt, 'pageX');
_my = ngDraggable.getEventProp(evt, 'pageY');
if (_centerAnchor) {
_tx = _mx - element.centerX - _dragOffset.left;
_ty = _my - element.centerY - _dragOffset.top;
} else {
_tx = _mx - _mrx - _dragOffset.left;
_ty = _my - _mry - _dragOffset.top;
}
moveElement(_tx, _ty);
$rootScope.$broadcast('draggable:move', { x: _mx, y: _my, tx: _tx, ty: _ty, event: evt, element: element, data: _data, uid: _myid });
}
var onrelease = function(evt) {
if (!_dragEnabled)
return;
evt.preventDefault();
$rootScope.$broadcast('draggable:end', {x:_mx, y:_my, tx:_tx, ty:_ty, event:evt, element:element, data:_data, callback:onDragComplete, uid: _myid});
element.removeClass('dragging');
element.closest('.drag-enter').removeClass('drag-enter');
reset();
$document.off(_moveEvents, onmove);
$document.off(_releaseEvents, onrelease);
}
var onDragComplete = function(evt) {
if (!onDragSuccessCallback )return;
scope.$apply(function () {
onDragSuccessCallback(scope, {$data: _data, $event: evt});
});
}
var reset = function() {
element.css({transform:'', 'z-index':'', '-webkit-transform':''});
}
var moveElement = function (x, y) {
element.css({
transform: 'matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, '+x+', '+y+', 0, 1)','z-index': 99999,
'-webkit-transform': 'matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, '+x+', '+y+', 0, 1)'
//,margin: '0' don't monkey with the margin,
});
}
initialize();
}
};
}])
.directive('ngDrop', ['$parse', '$timeout', '$window', 'ngDraggable', function ($parse, $timeout, $window, ngDraggable) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.value = attrs.ngDrop;
var _myid = scope.$id;
var _dropEnabled=false;
var onDropCallback = $parse(attrs.ngDropSuccess);// || function(){};
var onDragStartCallback = $parse(attrs.ngDragStart);
var onDragStopCallback = $parse(attrs.ngDragStop);
var onDragMoveCallback = $parse(attrs.ngDragMove);
var initialize = function () {
toggleListeners(true);
};
var toggleListeners = function (enable) {
// remove listeners
if (!enable)return;
// add listeners.
attrs.$observe("ngDrop", onEnableChange);
scope.$on('$destroy', onDestroy);
scope.$on('draggable:start', onDragStart);
scope.$on('draggable:move', onDragMove);
scope.$on('draggable:end', onDragEnd);
};
var onDestroy = function (enable) {
toggleListeners(false);
};
var onEnableChange = function (newVal, oldVal) {
_dropEnabled=scope.$eval(newVal);
};
var onDragStart = function(evt, obj) {
if(! _dropEnabled)return;
isTouching(obj.x,obj.y,obj.element);
$timeout(function(){
onDragStartCallback(scope, {$data: obj.data, $event: obj});
});
};
var onDragMove = function(evt, obj) {
if(! _dropEnabled)return;
isTouching(obj.x,obj.y,obj.element);
$timeout(function(){
onDragMoveCallback(scope, {$data: obj.data, $event: obj});
});
}
var onDragEnd = function (evt, obj) {
// don't listen to drop events if this is the element being dragged
if (!_dropEnabled || _myid === obj.uid)return;
if (isTouching(obj.x, obj.y, obj.element)) {
// call the ngDraggable ngDragSuccess element callback
if(obj.callback){
obj.callback(obj);
}
$timeout(function(){
onDropCallback(scope, {$data: obj.data, $event: obj});
});
}
$timeout(function(){
onDragStopCallback(scope, {$data: obj.data, $event: obj});
});
updateDragStyles(false, obj.element);
}
var isTouching = function(mouseX, mouseY, dragElement) {
var touching= hitTest(mouseX, mouseY);
updateDragStyles(touching, dragElement);
return touching;
}
var updateDragStyles = function(touching, dragElement) {
if(touching){
element.addClass('drag-enter');
dragElement.addClass('drag-over');
}else{
element.removeClass('drag-enter');
dragElement.removeClass('drag-over');
}
}
var hitTest = function(x, y) {
var bounds = ngDraggable.getPrivOffset(element);
bounds.right = bounds.left + element[0].offsetWidth;
bounds.bottom = bounds.top + element[0].offsetHeight;
return x >= bounds.left
&& x <= bounds.right
&& y <= bounds.bottom
&& y >= bounds.top;
};
initialize();
}
};
}])
.directive('ngDragClone', ['$parse', '$timeout', 'ngDraggable', function ($parse, $timeout, ngDraggable) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var img, _allowClone=true;
var _dragOffset = null;
scope.clonedData = {};
var initialize = function () {
img = element.find('img');
element.attr('draggable', 'false');
img.attr('draggable', 'false');
reset();
toggleListeners(true);
};
var toggleListeners = function (enable) {
// remove listeners
if (!enable)return;
// add listeners.
scope.$on('draggable:start', onDragStart);
scope.$on('draggable:move', onDragMove);
scope.$on('draggable:end', onDragEnd);
preventContextMenu();
};
var preventContextMenu = function() {
// element.off('mousedown touchstart touchmove touchend touchcancel', absorbEvent_);
img.off('mousedown touchstart touchmove touchend touchcancel', absorbEvent_);
// element.on('mousedown touchstart touchmove touchend touchcancel', absorbEvent_);
img.on('mousedown touchstart touchmove touchend touchcancel', absorbEvent_);
};
var onDragStart = function(evt, obj, elm) {
_allowClone=true;
if(angular.isDefined(obj.data.allowClone)){
_allowClone=obj.data.allowClone;
}
if(_allowClone) {
scope.$apply(function () {
scope.clonedData = obj.data;
});
element.css('width', obj.element[0].offsetWidth);
element.css('height', obj.element[0].offsetHeight);
moveElement(obj.tx, obj.ty);
}
_dragOffset = ngDraggable.getPrivOffset(element);
}
var onDragMove = function(evt, obj) {
if(_allowClone) {
_tx = obj.tx + _dragOffset.left;
_ty = obj.ty + _dragOffset.top;
moveElement(_tx, _ty);
}
};
var onDragEnd = function(evt, obj) {
//moveElement(obj.tx,obj.ty);
if(_allowClone) {
reset();
}
};
var reset = function() {
element.css({left:0,top:0, position:'fixed', 'z-index':-1, visibility:'hidden'});
};
var moveElement = function(x,y) {
element.css({
transform: 'matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, '+x+', '+y+', 0, 1)', 'z-index': 99999, 'visibility': 'visible',
'-webkit-transform': 'matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, '+x+', '+y+', 0, 1)'
//,margin: '0' don't monkey with the margin,
});
}
var absorbEvent_ = function (event) {
var e = event.originalEvent;
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
return false;
};
initialize();
}
};
}])
.directive('ngPreventDrag', ['$parse', '$timeout', function ($parse, $timeout) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var initialize = function () {
element.attr('draggable', 'false');
toggleListeners(true);
};
var toggleListeners = function (enable) {
// remove listeners
if (!enable)return;
// add listeners.
element.on('mousedown touchstart touchmove touchend touchcancel', absorbEvent_);
};
var absorbEvent_ = function (event) {
var e = event.originalEvent;
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
return false;
}
initialize();
}
}
}])
.directive('ngCancelDrag', [function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.find('*').attr('ng-cancel-drag', 'ng-cancel-drag');
}
}
}]);