-
Notifications
You must be signed in to change notification settings - Fork 3
/
gm.dragDrop.js
145 lines (115 loc) · 3.24 KB
/
gm.dragDrop.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
(function() {
'use strict';
// Based on myDraggable - https://docs.angularjs.org/guide/directive
angular
.module('gm.dragDrop', [])
.directive('gmDraggable', ['$window', '$document', gmDraggable])
.directive('gmOnHover', gmOnHover)
.directive('gmOnDrop', gmOnDrop);
var dragOb = null;
function apply(scope, attr, event) {
scope.$apply(function() {
scope.$eval(attr)(dragOb, event);
});
}
function gmDraggable($window, $document) {
return function(scope, element, attrs) {
var startX = 0,
startY = 0,
clone = null,
absParent = angular.element('<div></div>');
var cancelWatch = null;
// Handle will become a regular element, so if anything is returned,
// we need to wrap that in angular.element(...) to bind events to it
var handle = element[0].querySelectorAll('gm-drag-handle, [gm-drag-handle], .gm-drag-handle');
(handle.length ? angular.element(handle) : element).on('mousedown', function(event) {
// Prevent default dragging of selected content
event.preventDefault();
cancelWatch = scope.$watch(function() {
return element.prop('innerHTML');
}, function(val) {
if(clone)
clone.prop('innerHTML', val);
});
dragOb = scope.$eval(attrs.gmDraggable);
dragOb.$$gmDropZone = attrs.gmDropZone;
clone = element.clone();
element.addClass('gm-drag-element').after(clone);
clone.css({
position: 'relative',
pointerEvents: 'none'
}).addClass('gm-dragging');
var elBoundingRect = element[0].getBoundingClientRect();
absParent.css({
position: 'absolute',
zIndex: '2000',
top: elBoundingRect.top + $window.scrollY + 'px',
left: elBoundingRect.left + 'px',
width: elBoundingRect.width + 'px'
});
$document.find('body').append(absParent);
absParent.append(clone);
startX = event.pageX;
startY = event.pageY;
$document.on('mousemove', mousemove);
$document.on('mouseup', mouseup);
});
function mousemove(event) {
clone.css({
top: event.pageY - startY + 'px',
left: event.pageX - startX + 'px'
});
}
function mouseup() {
if(dragOb) {
if(attrs.gmOnInvalidDrop) {
apply(scope, attrs.gmOnInvalidDrop);
}
dragOb = null;
}
cancelWatch();
element.removeClass('gm-drag-element');
clone.remove();
absParent.remove();
$document.off('mousemove', mousemove);
$document.off('mouseup', mouseup);
}
};
}
function gmOnHover() {
return function(scope, element, attr) {
element.on('mouseover', function(e) {
if(!dragOb)
return;
if(dragOb.$$gmDropZone == attr.gmDropZone) {
apply(scope, attr.gmOnHover, e);
}
});
}
}
function gmOnDrop() {
return function(scope, element, attr) {
element.on('mouseover', function() {
if(!dragOb)
return;
if(dragOb.$$gmDropZone == attr.gmDropZone) {
element.addClass('gm-dropping');
}
});
element.on('mouseout', function() {
if(!dragOb)
return;
element.removeClass('gm-dropping');
});
element.on('mouseup', function() {
if(!dragOb)
return;
if(dragOb.$$gmDropZone == attr.gmDropZone) {
apply(scope, attr.gmOnDrop);
dragOb = null;
}
element.removeClass('gm-dropping');
});
}
}
})();