-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsb-drag-and-drop.js
165 lines (138 loc) · 4.7 KB
/
sb-drag-and-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
angular.module('sbDragAndDrop', [])
.config(function () {
if (window.jQuery) jQuery.event.props.push('dataTransfer');
})
.provider('sbDragController', [function () {
var Ctrl = function () {
this._counter = 0;
this._elements = {};
this._data = {};
this._completed = {};
};
Ctrl.prototype.dragStart = function (event, element, data) {
var id = String(++this._counter);
event.dataTransfer.setData('sbDragId', id);
this._data[id] = data;
this._elements[id] = element;
};
Ctrl.prototype.getId = function (event) {
var id = event.dataTransfer.getData('sbDragId');
// Work around some browsers not allowing getData on dragend
if (!id) {
for (id in this._elements) {
if (this._elements[id] === event.target) break;
}
}
return id;
};
Ctrl.prototype.getContext = function (event) {
var id = this.getId(event);
return {
$event: event,
$data: this._data[id],
$element: this._elements[id],
$completed: this._completed[id]
};
};
Ctrl.prototype.setCompleted = function (event) {
this._completed[this.getId(event)] = true;
};
Ctrl.prototype.dragEnd = function (event) {
var id = this.getId(event);
if (id) {
delete this._data[id];
delete this._elements[id];
delete this._completed[id];
}
};
this.$get = function () {
return new Ctrl();
};
}])
.directive('sbDraggable', ['$parse', 'sbDragController', function($parse, ctrl) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
attrs.$set('draggable', true);
var handlers = {};
['Dragstart', 'Drag', 'Dragend'].forEach(function (name) {
name = 'sb'+name;
if (name in attrs) {
var fn = $parse(attrs[name]);
handlers[name] = function (event) {
return scope.$apply(function () {
return fn(scope, ctrl.getContext(event));
});
};
}
else handlers[name] = angular.noop;
});
function callHandler(name, event) {
if (name in handlers) handlers[name](event);
}
elem.bind('dragstart', function(event) {
ctrl.dragStart(event, elem, scope.$eval(attrs.sbDragdata));
var res = handlers.sbDragstart(event);
if (res === false) {
ctrl.dragEnd(event);
return false;
}
attrs.$addClass('sb-dragging');
});
elem.bind('dragend', function(event) {
handlers.sbDragend(event);
ctrl.dragEnd(event);
attrs.$removeClass('sb-dragging');
});
}
};
}])
.directive('sbDropTarget', ['$parse', 'sbDragController', function($parse, ctrl) {
return {
restrict: 'A',
link: function(scope, elem, attrs, controller) {
var dropEffect = attrs.sbDropEffect || 'move';
var handlers = {};
['Dragenter', 'Dragover', 'Dragleave', 'Drop'].forEach(function (name) {
name = 'sb'+name;
if (name in attrs) {
var fn = $parse(attrs[name]);
handlers[name] = function (event) {
return scope.$apply(function () {
return fn(scope, ctrl.getContext(event));
});
};
}
else handlers[name] = angular.noop;
});
// With HTML's drag and drop event handling calling preventDefault enables
// the element as a drop zone. The opposite behaviour (returning false to disable
// the dropzone) Seems more appropriate here...
elem.bind('dragenter', function(event) {
var res = handlers.sbDragenter(event);
if (res === false) return;
event.preventDefault();
event.dataTransfer.dropEffect = dropEffect;
attrs.$addClass('sb-dragover');
});
elem.bind('dragover', function(event) {
var res = handlers.sbDragover(event);
if (res === false) return;
event.preventDefault();
event.dataTransfer.dropEffect = dropEffect;
});
elem.bind('dragleave', function(event) {
handlers.sbDragleave(event);
attrs.$removeClass('sb-dragover');
});
elem.bind('drop', function(event) {
// calling preventDefault stops the browser's native drop handling
if (event.preventDefault) event.preventDefault();
if (event.stopPropagation) event.stopPropagation();
var res = handlers.sbDrop(event);
if (res !== false) ctrl.setCompleted(event);
attrs.$removeClass('sb-dragover');
});
}
};
}]);