-
Notifications
You must be signed in to change notification settings - Fork 4
/
multiple-drag.js
83 lines (76 loc) · 3.14 KB
/
multiple-drag.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
/**
* Angular JS multiple-drag module
* @author Maksym Pomazan
* @version 0.0.1
*/
angular.module('multipleDrag', ['multipleSelection'])
.directive('multipleDragItem', ['$document', function($document) {
return {
scope: true,
restrict: 'A',
link: function(scope, element, iAttrs, controller) {
scope.isDraggable = true;
scope.isDragging = false;
var startX = 0,
startY = 0;
element.css({
position: 'absolute',
cursor: 'pointer'
});
if (!element.css('top')) {
element.css({
'top': element.prop('offsetTop') + "px"
});
}
if (!element.css('left')) {
element.css({
'left': element.prop('offsetLeft') + "px"
});
}
element.on('mousedown', function(event) {
// Prevent default dragging of selected content
event.preventDefault();
startX = event.pageX;
startY = event.pageY;
$document.on('mousemove', mousemove);
$document.on('mouseup', mouseup);
});
function mousemove(event) {
event.preventDefault();
event.stopPropagation();
var deltaX = event.pageX - startX;
var deltaY = event.pageY - startY;
var childs = element.parent().children();
for (var i = 0; i < childs.length; i++) {
child = angular.element(childs[i]);
if (child.scope().isDraggable && child.scope().isSelected) {
if (!child.scope().isDragging) {
child.scope().isDragging = true;
child.scope().$apply();
}
var x = parseInt(child.css('left').replace('px', ''));
var y = parseInt(child.css('top').replace('px', ''));
child.css({
left: (x + deltaX) + 'px',
top: (y + deltaY) + 'px'
});
}
}
startX = event.pageX;
startY = event.pageY;
}
function mouseup() {
var childs = element.parent().children();
for (var i = 0; i < childs.length; i++) {
child = angular.element(childs[i]);
if (child.scope().isDragging) {
child.scope().isDragging = false;
child.scope().$apply();
}
}
$document.off('mousemove', mousemove);
$document.off('mouseup', mouseup);
}
}
};
}]);