-
Notifications
You must be signed in to change notification settings - Fork 4
/
sortable.js
271 lines (214 loc) · 6.86 KB
/
sortable.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
'use strict';
var START_EVENT = 'ontouchstart' in document.documentElement ? 'touchstart' : 'mousedown';
var MOVE_EVENT = 'ontouchmove' in document.documentElement ? 'touchmove' : 'mousemove';
var END_EVENT = 'ontouchend' in document.documentElement ? 'touchend' : 'mouseup';
function Sortable($attrs, $element, $scope) {
var self = this;
/*
* dropzone [string] - By default, an empty element will be created
* with the same tag name as the element being
* dragged.
*
* sameSize [bool] - Make the dropzone the same size as the element
* being dragged.
*
* threshold [float] - The minimum distance the cursor must travel to
* initiate a drag event.
*/
var options = {
dropzone: undefined,
sameSize: true,
threshold: 2
};
var dropzone;
var selected;
var position = {};
if(self.options) {
angular.extend(options, self.options);
}
if($attrs.sortable === '') {
self.sortable = true;
}
function childCount() {
return $element.children().length;
}
function createDropZone(element) {
if(angular.isString(options.dropzone)) {
dropzone = angular.element(options.dropzone);
} else {
dropzone = angular.element('<' + element.prop('localName') + '>');
}
dropzone.addClass('drop-zone');
if(options.sameSize) {
dropzone.css({
height: element.prop('clientHeight') + 'px',
width: element.prop('clientWidth') + 'px'
});
}
element.after(dropzone);
}
function floatElement(element, children) {
for(var i = 0; i < children.length; i++) {
var child = children.eq(i);
var childComputedStyle = child.data('sortable.computedStyle');
child.css({
height: childComputedStyle.height,
width: childComputedStyle.width
});
}
var bounds = element[0].getBoundingClientRect();
var computedStyle = element.data('sortable.computedStyle');
element.addClass('dragging').css({
height: computedStyle.height,
left: bounds.left - computedStyle.marginLeft.slice(0, -2) + 'px',
pointerEvents: 'none',
position: 'fixed',
top: bounds.top - computedStyle.marginTop.slice(0, -2) + 'px',
width: computedStyle.width,
zIndex: 100
});
}
function getChildFromPoint(point) {
var children = $element.children();
var hovering = document.elementFromPoint(point.x, point.y);
for(var i = 0; i < children.length; i++) {
if(children[i] === hovering || children[i].contains(hovering)) {
return children[i];
}
}
}
function indexOf(child) {
return Array.prototype.indexOf.call($element.children(), child[0]);
}
function magnitude(vector) {
return Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2));
}
function mouseDown(event) {
event.preventDefault();
event.stopPropagation();
selected = angular.element(event.currentTarget);
position.x = event.pageX;
position.y = event.pageY;
document.addEventListener(MOVE_EVENT, mouseMove);
document.addEventListener(END_EVENT, mouseUp);
}
function mouseMove(event) {
event.preventDefault();
var delta = {
x: event.pageX - position.x,
y: event.pageY - position.y
};
if(magnitude(delta) < options.threshold) {
return;
}
if(!selected.hasClass('dragging')) {
onDragStart(selected, selected.children());
}
onDragMove(event, selected, delta);
}
function mouseUp(event) {
document.removeEventListener(MOVE_EVENT, mouseMove);
document.removeEventListener(END_EVENT, mouseUp);
if(selected.hasClass('dragging')) {
onDragEnd(event, selected, selected.children());
}
}
function onDragEnd(event, child, grandChildren) {
var oldIndex = child.data('sortable.index');
var newIndex = indexOf(dropzone);
// because the element being dragged is never removed
if(newIndex > oldIndex) {
newIndex--;
}
restoreStyle(child.removeClass('dragging'));
for(var i = 0; i < grandChildren.length; i++) {
restoreStyle(grandChildren.eq(i));
}
if(angular.isArray(self.sequence)) {
dropzone.remove();
$scope.$apply(function () {
self.sequence.splice(newIndex, 0, self.sequence.splice(oldIndex, 1)[0]);
});
} else {
dropzone.replaceWith(child);
}
if(angular.isFunction(self.dragEnd)) {
self.dragEnd(child, newIndex, oldIndex, self.sequence);
}
}
function onDragMove(event, child, delta) {
child.css('transform', 'translate(' + delta.x + 'px, ' + delta.y+ 'px)');
var point = {x: event.pageX, y: event.pageY};
var sibling = getChildFromPoint(point);
if(sibling && sibling !== dropzone[0]) {
updateDropZone(point, sibling);
}
if(angular.isFunction(self.dragMove)) {
self.dragMove(child);
}
}
function onDragStart(child, grandChildren) {
remeberStyle(child);
for(var i = 0; i < grandChildren.length; i++) {
remeberStyle(grandChildren.eq(i));
}
floatElement(child, grandChildren);
createDropZone(child.data('sortable.index', indexOf(child)));
if(angular.isFunction(self.dragStart)) {
self.dragStart(child);
}
}
function remeberStyle(element) {
element.data('sortable.style', element.attr('style') || '');
if(!element.data('sortable.computedStyle')) {
element.data('sortable.computedStyle', window.getComputedStyle(element[0]));
}
}
function restoreStyle(element) {
element.attr('style', element.data('sortable.style'));
}
function updateDropZone(cursor, sibling) {
var offsetY = cursor.y - sibling.getBoundingClientRect().top;
var midPoint = sibling.clientHeight >> 1;
// if the cursor is past the midpoint of the element, insert
// the dropzone after the element
if(offsetY > midPoint) {
if(sibling.nextElementSibling !== dropzone[0]) {
$element[0].insertBefore(dropzone[0], sibling.nextElementSibling);
}
} else if(sibling.previousElementSibling !== dropzone[0]) {
$element[0].insertBefore(dropzone[0], sibling);
}
}
$scope.$watch(childCount, function (childCount) {
if(childCount) {
var children = $element.children();
if(self.sortable) {
children.on(START_EVENT, mouseDown);
}
}
});
$scope.$watch('$sortable.sortable', function (sortable) {
var children = $element.children();
if(sortable) {
children.on(START_EVENT, mouseDown);
} else {
children.off(START_EVENT, mouseDown);
}
});
}
Sortable.$inject = ['$attrs', '$element', '$scope'];
angular.module('sortable', []).directive('sortable', function () {
return {
controller: Sortable,
controllerAs: '$sortable',
bindToController: {
options: '=?',
dragEnd: '=?',
dragMove: '=?',
dragStart: '=?',
sequence: '=?ngModel',
sortable: '=?'
}
};
});