-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy-drag-drop.js
701 lines (534 loc) · 19.9 KB
/
easy-drag-drop.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
* The drag and drop plugin is created completely using angular js.
* There is no dependecy of Jquery
* It is supported in touch devices
* The plugin uses the basic functionalities from https://github.com/fatlinesofcode/ngDraggable
*
*
*/
angular.module("easyDraggable", []);
angular.module("easyDraggable").factory('easyDraggableUtils', [function() {
var svc = {
newUuid: function() {
function _p8(s) {
var p = (Math.random().toString(16) + "000000000").substr(2, 8);
return s ? "-" + p.substr(0, 4) + "-" + p.substr(4, 4) : p;
}
return _p8() + _p8(true) + _p8(true) + _p8();
},
emptyUuid: function() {
return '00000000-0000-0000-0000-000000000000';
},
getOffset: function(element) {
return element.getBoundingClientRect();
},
outerWidth: function(element) {
return element.offsetWidth;
},
outerHeight: function(element) {
return element.offsetHeight;
},
scrollTop: function() {
return (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
},
scrollLeft: function() {
return (window.pageXOffset !== undefined) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft;
},
/**
* Method returns current page scroll values as array (X and Y axis).
* @return {Array} Returns array with two values [ scrollX, scrollY ].
* @public
* @function
* @name REDIPS.drag#getScrollPosition
*/
getScrollPosition: function() {
// define local scroll position variables
var scrollX, scrollY;
// Netscape compliant
if (typeof(window.pageYOffset) === 'number') {
scrollX = window.pageXOffset;
scrollY = window.pageYOffset;
}
// DOM compliant
else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
scrollX = document.body.scrollLeft;
scrollY = document.body.scrollTop;
}
// IE6 standards compliant mode
else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
scrollX = document.documentElement.scrollLeft;
scrollY = document.documentElement.scrollTop;
}
// needed for IE6 (when vertical scroll bar was on the top)
else {
scrollX = scrollY = 0;
}
// return scroll positions
return [scrollX, scrollY];
}
};
return svc;
}]);
angular.module("easyDraggable").directive('easyDraggable', ['$rootScope', '$parse', 'easyDraggableUtils', '$timeout', function($rootScope, $parse, easyDraggableUtils, $timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.value = attrs.easyDraggable;
var clone = 'true',
revert = true,
initialX = 0,
initialY = 0,
initialWidth = '',
mRegisterX = 0,
mRegisterY = 0;
var offset,
_centerAnchor = false,
_mx, _my,
_tx, _ty,
_cx, _cy,
_mrx, _mry,
scrollOffset,
midScrollY,
maxScrollY;
var _hasTouch = ('ontouchstart' in document.documentElement);
var _pressEvents = 'touchstart mousedown';
var _moveEvents = 'touchmove mousemove';
var _releaseEvents = 'touchend mouseup';
var clonedElement = null;
var scroll = (attrs.scroll == 'true') ? true : false;
clone = (attrs.clone == 'false') ? 'false' : 'true';
var scrollElement = (attrs.scrollElement) ? attrs.scrollElement : undefined;
var ngDocument = angular.element(document);
element = angular.element(element);
//Gets id of the element in the DOM
var id = element.attr("id");
//Sets ID for the element if ID doesn't exists
if (!id) {
id = easyDraggableUtils.newUuid();
element.attr("id", id);
}
var dragElement = null;
var _data = null;
var _dragEnabled = false;
var _pressTimer = null;
var onDragStartCallback = $parse(attrs.whenDragStart) || null;
var onDragStopCallback = $parse(attrs.whenDragStop) || null;
var onDragSuccessCallback = $parse(attrs.ngDragSuccess) || null;
var onDragFailCallback = $parse(attrs.ngDragFail) || null;
element.css({
cursor: 'move'
});
var initialize = function() {
element.attr('draggable', 'false'); // prevent native drag
toggleListeners(true);
};
var toggleListeners = function(enable) {
// remove listeners
if (!enable)
return;
// add listeners.
scope.$on('$destroy', onDestroy);
//attrs.$observe("ngDrag", onEnableChange);
scope.$watch(attrs.easyDraggable, onEnableChange);
//attrs.$observe('ngCenterAnchor', onCenterAnchor);
scope.$watch(attrs.ngCenterAnchor, onCenterAnchor);
scope.$watch(attrs.ngDragData, onDragDataChange);
element.on(_pressEvents, onLongPress);
// if(! _hasTouch){
// element.on('mousedown', function(){ return false;}); // prevent native drag
// }
};
var onDestroy = function(enable) {
toggleListeners(false);
};
var onDragDataChange = function(newVal, oldVal) {
_data = newVal;
};
var onEnableChange = function(newVal, oldVal) {
_dragEnabled = (newVal);
if (typeof _dragEnabled === 'string') {
_dragEnabled = ((_dragEnabled == 'true') ? true : false);
}
};
var onCenterAnchor = function(newVal, oldVal) {
if (angular.isDefined(newVal))
_centerAnchor = (newVal || 'true');
};
/*
* 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;
if (_hasTouch) {
cancelPress();
_pressTimer = setTimeout(function() {
cancelPress();
onLongPress(evt);
}, 100);
ngDocument.on(_moveEvents, cancelPress);
ngDocument.on(_releaseEvents, cancelPress);
} else {
onLongPress(evt);
}
};
var cancelPress = function() {
clearTimeout(_pressTimer);
ngDocument.off(_moveEvents, cancelPress);
ngDocument.off(_releaseEvents, cancelPress);
};
var onLongPress = function(evt) {
if (!_dragEnabled)
return;
evt.preventDefault();
if (evt.touches && evt.touches.length > 1) {
return;
}
_mx = (evt.pageX) ? evt.pageX : (evt.changedTouches) ? evt.changedTouches[0].pageX : evt.originalEvent.changedTouches[0].pageX;
_my = (evt.pageY) ? evt.pageY : (evt.changedTouches) ? evt.changedTouches[0].pageY : evt.originalEvent.changedTouches[0].pageY;
_cx = (evt.clientX) ? evt.clientX : (evt.changedTouches) ? evt.changedTouches[0].clientX : evt.originalEvent.changedTouches[0].pageX;
_cy = (evt.clientY) ? evt.clientY : (evt.changedTouches) ? evt.changedTouches[0].clientY : evt.originalEvent.changedTouches[0].pageY;
if (clone == 'true') {
clonedElement = element.clone();
element.parent().append(clonedElement);
dragElement = clonedElement;
} else {
dragElement = element;
}
initialX = element[0].offsetLeft;
initialY = element[0].offsetTop;
initialWidth = element[0].style.width;
var moveX = _cx - element[0].getBoundingClientRect().left;
var moveY = _cy - element[0].getBoundingClientRect().top;
mRegisterX = moveX;
mRegisterY = moveY;
moveX += element[0].offsetLeft;
moveY += element[0].offsetTop;
moveX -= mRegisterX;
moveY -= mRegisterY;
moveElement(moveX, moveY);
var windowTop = easyDraggableUtils.scrollTop();
var windowLeft = easyDraggableUtils.scrollLeft();
_mx -= windowLeft;
_my -= windowTop;
ngDocument.on(_moveEvents, onMove);
ngDocument.on(_releaseEvents, onRelease);
//Class added to remove highlighting when start dragging
//element.addClass('easy-drag-started');
$rootScope.$broadcast('draggable:start', {
x: _mx,
y: _my,
tx: _tx,
ty: _ty,
element: element,
dragElement: dragElement,
data: _data
});
evt.stopPropagation();
if (onDragStartCallback) {
var data = {
name: 'started',
dragEl: element.attr('id'),
dragElement: element
};
scope.$apply(function() {
onDragStartCallback(scope, {
data: data
});
});
}
};
var onMove = function(evt) {
if (!_dragEnabled)
return;
evt.preventDefault();
_mx = (evt.pageX) ? evt.pageX : (evt.changedTouches) ? evt.changedTouches[0].pageX : evt.originalEvent.changedTouches[0].pageX;
_my = (evt.pageY) ? evt.pageY : (evt.changedTouches) ? evt.changedTouches[0].pageY : evt.originalEvent.changedTouches[0].pageY;
_cx = (evt.clientX) ? evt.clientX : (evt.changedTouches) ? evt.changedTouches[0].clientX : evt.originalEvent.changedTouches[0].pageX;
_cy = (evt.clientY) ? evt.clientY : (evt.changedTouches) ? evt.changedTouches[0].clientY : evt.originalEvent.changedTouches[0].pageY;
var ngScrollElement = angular.element(document.querySelector(scrollElement));
var scrollLimit = ngScrollElement[0].scrollHeight - ngScrollElement[0].clientHeight;
var scrollElementOffset = ngScrollElement[0].getBoundingClientRect();
var scrollElementTop = scrollElementOffset.top;
var scrollElementBottom = scrollElementOffset.bottom;
var scrollElementLeft = scrollElementOffset.left;
var scrollElementRight = scrollElementOffset.right;
_cx = (scrollElementLeft > _cx) ? scrollElementLeft : _cx;
_cx = (scrollElementRight < _cx) ? scrollElementRight : _cx;
_cy = (scrollElementTop > _cy) ? scrollElementTop : _cy;
_cy = (scrollElementBottom < _cy) ? scrollElementBottom : _cy;
if (scroll && scrollElement) {
var dragElementBottom = dragElement[0].getBoundingClientRect().bottom;
var dragElementTop = dragElement[0].getBoundingClientRect().top;
if (dragElementBottom > scrollElementBottom) {
var scrollPosition = dragElementBottom - scrollElementBottom;
scrollPosition = ngScrollElement[0].scrollTop + scrollPosition + 15;
scrollPosition = Math.ceil(scrollPosition);
if (scrollPosition < scrollLimit) {
ngScrollElement[0].scrollTop = scrollPosition;
}
}
if (scrollElementTop > dragElementTop) {
var scrollPosition = scrollElementTop - dragElementTop;
if (ngScrollElement[0].scrollTop) {
scrollPosition = ngScrollElement[0].scrollTop - scrollPosition;
ngScrollElement[0].scrollTop = scrollPosition;
}
}
}
var moveX = _cx - element[0].getBoundingClientRect().left;
var moveY = _cy - element[0].getBoundingClientRect().top;
moveX += element[0].offsetLeft;
moveY += element[0].offsetTop;
moveX -= mRegisterX;
moveY -= mRegisterY;
moveElement(moveX, moveY);
var windowTop = easyDraggableUtils.scrollTop();
var windowLeft = easyDraggableUtils.scrollLeft();
_mx -= windowLeft;
_my -= windowTop;
$rootScope.$broadcast('draggable:move', {
x: _mx,
y: _my,
tx: _tx,
ty: _ty,
element: element,
dragElement: dragElement,
data: _data
});
};
var onDragComplete = function(evt, dragElement) {
if (!onDragSuccessCallback)
return;
scope.$apply(function() {
onDragSuccessCallback(scope, {
$data: _data,
$event: evt
});
});
if (clone == 'true') {
dragElement.remove();
}
dragElement.removeClass('iap-drag-animation easy-dragging');
};
var onDragInComplete = function(evt, dragElement) {
dragElement.addClass('iap-drag-animation');
dragElement.css({
'left': initialX + 'px',
'top': initialY + 'px',
'position': '',
'z-index': '',
'width': initialWidth
});
if (clone == 'true') {
dragElement.remove();
}
dragElement.removeClass('iap-drag-animation easy-dragging');
};
var onRelease = function(evt) {
if (!_dragEnabled)
return;
evt.preventDefault();
//Class added to remove highlighting when start dragging
element.removeClass('easy-drag-started');
$rootScope.$broadcast('draggable:end', {
x: _mx,
y: _my,
tx: _tx,
ty: _ty,
element: element,
dragElement: dragElement,
data: _data,
successCallback: onDragComplete,
failCallback: onDragInComplete
});
ngDocument.off(_moveEvents, onMove);
ngDocument.off(_releaseEvents, onRelease);
evt.stopPropagation();
if (onDragStopCallback) {
var data = {
name: 'stopped',
dragEl: element.attr('id'),
dragElement: element
};
scope.$apply(function() {
onDragStopCallback(scope, {
data: data
});
});
}
};
var resetElement = function() {
dragElement.removeClass('easy-dragging');
if (clone == 'true') {
dragElement.remove();
} else {
dragElement.css({
'position': '',
'z-index': ''
});
}
};
var moveElement = function(x, y) {
dragElement.addClass('easy-dragging');
dragElement.css({
'left': x + 'px',
'top': y + 'px',
'position': 'absolute',
'z-index': 99999,
'width': '100%'
});
};
initialize();
}
};
}]);
angular.module("easyDraggable").directive('easyDroppable', ['$parse', '$timeout', 'easyDraggableUtils', function($parse, $timeout, easyDraggableUtils) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.value = attrs.easyDroppable;
var onDropAction = (typeof attrs.onDropAction !== 'undefined') ? attrs.onDropAction : 'touch';
var _dropEnabled = false;
var onDropSuccess = $parse(attrs.onDropSuccess) || null; // || function(){};
var id = element.attr("id");
if (!id) {
id = easyDraggableUtils.newUuid();
element.attr("id", id);
}
element.addClass('easy-droppable');
var initialize = function() {
toggleListeners(true);
};
var toggleListeners = function(enable) {
// remove listeners
if (!enable)
return;
// add listeners.
scope.$watch(attrs.easyDroppable, 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 = (newVal);
if (typeof _dropEnabled === 'string') {
_dropEnabled = ((_dropEnabled == 'true') ? true : false);
}
};
var onDragStart = function(evt, dragObj) {
if (!_dropEnabled || (element.attr('easy-droppable') === 'false'))
return;
isTouching(dragObj);
};
var onDragMove = function(evt, dragObj) {
if (!_dropEnabled || (element.attr('easy-droppable') === 'false'))
return;
element.isTouching = isTouching(dragObj);
};
var onDragEnd = function(evt, dragObj) {
if (!_dropEnabled || (element.attr('easy-droppable') === 'false'))
return;
if (element.isTouching) {
// call the ngDraggable element callback
if (dragObj.successCallback) {
dragObj.successCallback(evt, dragObj.dragElement);
}
var data = {
name: 'success',
dropEl: element.attr('id'),
dragEl: dragObj.element.attr('id'),
dragElement: dragObj.dragElement,
dropElement: element
};
scope.$apply(function(scope) {
onDropSuccess(scope, {
data: data
});
});
} else {
if (dragObj.failCallback) {
dragObj.failCallback(evt, dragObj.dragElement);
}
}
setTimeout(function(){
updateDragStyles(false, dragObj.element);
});
// Reset isTouching once user stops the dragging.
element.isTouching = false;
};
var isTouching = function(dragObj) {
var touching = hitTest(dragObj);
updateDragStyles(touching, dragObj.element);
return touching;
};
//Updates classes in the DOM
var updateDragStyles = function(touching, dragElement) {
if (touching) {
element.addClass('easy-drag-enter');
dragElement.addClass('easy-drag-over');
} else {
element.removeClass('easy-drag-enter');
dragElement.removeClass('easy-drag-over');
}
};
//Returns true when the dragged element satisfies the drop condition
var hitTest = function(dragObj) {
var isHitted;
var dragOffset = easyDraggableUtils.getOffset(dragObj.dragElement[0]); //Drag position
var dropOffset = easyDraggableUtils.getOffset(element[0]); // Drop position
var drW = easyDraggableUtils.outerWidth(dragObj.element[0]); // Drag width
var drH = easyDraggableUtils.outerHeight(dragObj.element[0]); // Drag Height
var drL = dragOffset.left, // Drag left
drR = dragOffset.right, // Drag right
drT = dragOffset.top, // Drag top
drB = dragOffset.bottom, // Drag bottom
dpL = dropOffset.left, // Drop left
dpR = dropOffset.right, // Drop right
dpT = dropOffset.top, // Drop top
dpB = dropOffset.bottom; // Drop bottom
switch (onDropAction) {
//When the dragged element exactly fits to the droppable element
case 'fit':
isHitted = (dpL <= drL && drR <= dpR && dpT <= drT && drB <= dpB);
break;
//When the dragged element intersects the droppable element half of its width
case 'intersect':
isHitted = dpL < drL + (drW / 2) && // Right Half
drR - (drW / 2) < dpR && // Left Half
dpT < drT + (drH / 2) && // Bottom Half
drB - (drH / 2) < dpB; // Top Half
break;
//When the pointer enters to the droppable element
case 'pointer':
isHitted = dragObj.x >= dpL &&
dragObj.x <= dpR &&
dragObj.y <= dpB &&
dragObj.y >= dpT;
break;
//When the dragged element touches the droppable element
case 'touch':
isHitted = (
(drT >= dpT && drT <= dpB) || // Top edge touching
(drB >= dpT && drB <= dpB) || // Bottom edge touching
(drT < dpT && drB > dpB) // Surrounded vertically
) && (
(drL >= dpL && drL <= dpR) || // Left edge touching
(drR >= dpL && drR <= dpR) || // Right edge touching
(drL < dpL && drR > dpR) // Surrounded horizontally
);
break;
default:
isHitted = false;
}
return isHitted;
};
initialize();
}
};
}]);