-
Notifications
You must be signed in to change notification settings - Fork 578
/
Copy pathand-drag-drop.js
211 lines (171 loc) · 7.71 KB
/
and-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
var module = angular.module("lvl.directives.dragdrop", ['lvl.services']);
module.directive('lvlDraggable', ['$rootScope', 'uuid','$compile', function ($rootScope, uuid) {
return {
restrict: 'A',
link: function (scope, el, attrs, controller) {
var isEnabled = scope.$eval(attrs.lvlDraggable);
if(isEnabled) {
angular.element(el).attr("draggable", "true");
var id = angular.element(el).attr("id");
if (!id) {
id = uuid.new()
angular.element(el).attr("id", id);
}
el.bind("dragstart", function (e) {
e.originalEvent.dataTransfer.setData('text', id);
e.originalEvent.dataTransfer.setDragImage(document.getElementById('file-drag-target'),40,30);
console.log('drag');
$rootScope.$emit("LVL-DRAG-START");
});
el.bind("dragend", function (e) {
$rootScope.$emit("LVL-DRAG-END");
});
}
}
};
}]);
module.directive('lvlDropTarget', ['$rootScope', 'uuid', function ($rootScope, uuid) {
return {
restrict: 'A',
scope: {
onDrop: '&'
},
link: function (scope, el, attrs, controller) {
var id = angular.element(el).attr("id");
if (!id) {
id = uuid.new();
angular.element(el).attr("id", id);
}
var isEnabled = scope.$eval(attrs.lvlDropTarget);
if(isEnabled){
el.bind("dragover", function (e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
e.originalEvent.dataTransfer.dropEffect = 'move'; // See the section on the originalEvent.dataTransfer object.
return false;
});
el.bind("dragenter", function (e) {
// this / e.target is the current hover target.
//check if el exists
if(e.target == null)
return;
if(e.target.localName !== 'td'){
angular.element(e.target).addClass('lvl-over');
}else{
angular.element(e.target.parentElement).addClass('lvl-over');
}
});
el.bind("dragleave", function (e) {
//check if el exists
if(e.target == null)
return;
if(e.target.localName !== 'td'){
angular.element(e.target).removeClass('lvl-over'); // this / e.target is previous target element.
}else{
angular.element(e.target.parentElement).removeClass('lvl-over'); // this / e.target is previous target element.
}
});
el.bind("drop", function (e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
if (e.stopPropagation) {
e.stopPropagation(); // Necessary. Allows us to drop.
}
var data = e.originalEvent.dataTransfer.getData("text");
var dest = document.getElementById(id);
var src = document.getElementById(data);
scope.onDrop({dragEl: data, dropEl: id});
});
$rootScope.$on("LVL-DRAG-START", function () {
var el = document.getElementById(id);
angular.element(el).addClass("lvl-target");
});
$rootScope.$on("LVL-DRAG-END", function () {
var el = document.getElementById(id);
angular.element(el).removeClass("lvl-target");
//check if el exists
if(el == null)
return;
if(el.localName !== 'td'){
angular.element(el).removeClass('lvl-over'); // this / e.target is previous target element.
}else{
angular.element(el.parentElement).removeClass('lvl-over'); // this / e.target is previous target element.
}
//angular.element(el).removeClass("lvl-over");
});
}
}
};
}]);
module.directive('lvlDropZone', ['$rootScope', 'uuid', function ($rootScope, uuid) {
return {
restrict: 'A',
scope: {
onDrop: '&'
},
link: function (scope, el, attrs, controller) {
var id = angular.element(el).attr("id");
if (!id) {
id = uuid.new();
angular.element(el).attr("id", id);
}
var isEnabled = scope.$eval(attrs.lvlDropZone);
if(isEnabled){
el.bind("dragover", function (e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
e.originalEvent.dataTransfer.dropEffect = 'move'; // See the section on the originalEvent.dataTransfer object.
return false;
});
el.bind("dragenter", function (e) {
// this / e.target is the current hover target.
//check if el exists
if(e.target == null)
return;
//check if we really drag files from desktop
if(e.originalEvent.dataTransfer.types[0] != 'Files')
return;
//remove style
angular.element(e.target).addClass('lvl-zone-over');
});
el.bind("dragleave", function (e) {
//check if el exists
if(e.target == null)
return;
angular.element(e.target).removeClass('lvl-zone-over'); // this / e.target is previous target element.
});
el.bind("drop", function (e) {
console.log('files dropped');
angular.element(e.target).removeClass('lvl-zone-over');
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
if (e.stopPropagation) {
e.stopPropagation(); // Necessary. Allows us to drop.
}
//check if we really drag files from desktop
if(e.originalEvent.dataTransfer.types[0] != 'Files')
return;
var files = e.originalEvent.dataTransfer.files;
var data = e.originalEvent.dataTransfer.getData("text");
scope.onDrop({dragEl: data, dropEl: id, dragFiles: files});
});
$rootScope.$on("LVL-DRAG-START", function () {
var el = document.getElementById(id);
angular.element(el).addClass("lvl-target");
});
$rootScope.$on("LVL-DRAG-END", function () {
var el = document.getElementById(id);
angular.element(el).removeClass("lvl-target");
//check if el exists
if(el == null)
return;
angular.element(el).removeClass("lvl-zone-over");
});
}
}
};
}]);