forked from schartier/angular-sortable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-sortable.js
More file actions
312 lines (261 loc) · 9.82 KB
/
angular-sortable.js
File metadata and controls
312 lines (261 loc) · 9.82 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
'use strict';
(function ($) {
var events = {
drag: 'mousemove touchmove',
dragstart: 'mousedown touchstart',
dragend: 'mouseup touchend',
selectstart: 'selectstart'
};
var classes = {
sorting: 'sortable-sorting',
item: 'sortable-item',
handle: 'sortable-handle',
clone: 'sortable-clone',
active: 'sortable-activeitem',
}
var $body = $(document.body);
var debounceMs = 2;
function debounce(fn, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) {
fn.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
fn.apply(context, args);
}
};
};
function Sortable(element, options) {
this.options = $.extend({
items: '.sortable',
handles: null,
zindex: '9000',
dragX: true,
dragY: true,
onChange: null,
onDragstart: null,
onDrag: null,
onDragend: null
}, options);
this.enabled = null;
this.state = null;
this.$element = $(element);
this.$activeItem = null;
this.refresh();
}
Sortable.prototype.enable = function (enabled) {
var self = this;
this.enabled = enabled;
};
Sortable.prototype.refresh = function () {
var self = this;
var $items = $(this.options.items, this.$element);
// fixed class used to mark the elements, makes sure the event target is not set to a child node
// not using `this.options.items` because that one is a selector (can have any form) controlled by client code
$items.addClass(classes.item);
// adding unselectable to draggable items
$items.attr('unselectable', 'on');
$body.off(events.dragend).off(events.drag);
$items.off(events.dragstart);
if (this.enabled) {
if (!this.$activeItem) {
$items.on(events.dragstart, function (e) {
self.dragstart(e);
});
}
}
};
var detect = debounce(function (context, event) {
var $items = $('.' + classes.item, context.$element);
var dragElement = context.$dragElement[0];
// caching before loop
var ix;
var item;
var length = $items.length;
var offsetX = event.offsetX + dragElement.offsetLeft;
var offsetY = event.offsetY + dragElement.offsetTop;
for (ix = 0; ix < length; ix++) {
item = $items[ix];
if (ix === context.draggingIdx) {
continue;
}
if (offsetY > item.offsetTop
&& (offsetY < item.offsetTop + item.offsetHeight)
&& offsetX > item.offsetLeft
&& offsetX < item.offsetLeft + item.offsetWidth
) {
context.$activeItem.removeClass(classes.active);
context.$activeItem = $($items[ix]);
context.$activeItem.addClass(classes.active);
context.options.onChange(context.draggingIdx, ix);
context.draggingIdx = ix;
break;
}
}
}, debounceMs);
Sortable.prototype.drag = function (event) {
this.options.onDrag(event);
if (event.isPropagationStopped()) {
return;
}
this.$dragElement.css('top', '+=' + (event.clientY - this.state.clientY));
this.$dragElement.css('left', '+=' + (event.clientX - this.state.clientX));
detect(this, event);
this.state = event;
}
Sortable.prototype.dragstart = function (event) {
if (event.which !== 1) {
// Make sure it is a left mouse click
return;
}
var self = this;
var $items = $('.' + classes.item, self.$element);
var $target = $(event.target);
// make sure event.target is a handle
if (this.options.handles) {
// marking all handles with a css class in order to be able to detect them on drag start using `$.closest()`
// regardless of what selector the client used
// doing it here and not on refresh because when referesh runs, the child nodes of the ng-repeat are not fully rendered
if (this.options.handles) {
$items.find(this.options.handles).addClass(classes.handle);
}
if (!$target.closest('.' + classes.handle).length) {
return;
}
}
this.options.onDragstart(event);
if (event.isPropagationStopped()) {
return;
}
// makes sure event target is the sortable element, not some child
event.target = (function () {
if ($target.hasClass(classes.item)) {
return event.target;
}
else {
return $target.closest('.' + classes.item)[0];
}
})();
self.bodyUnselectable = $body.attr('unselectable');
$body.attr('unselectable', 'on');
// clones the css classes before cloning the element
var className = $(event.target).attr('class');
this.$activeItem = $(event.target).addClass(classes.active);
var position = this.$activeItem.position();
self.draggingIdx = Array.prototype.indexOf.call($items, self.$activeItem[0]);
// Todo: The following will eventually cause problems related to styling,
// this should be a clone of the activeElement without all the angular bindings...
this.$dragElement = $('<' + event.target.tagName + '/>').html(event.target.innerHTML)
.css({
'z-index': this.options.zindex,
width: this.$activeItem[0].offsetWidth,
height: this.$activeItem[0].offsetHeight,
top: position.top,
left: position.left
})
.addClass(className + ' ' + classes.clone)
.removeClass(classes.item)
.appendTo(event.target.parentNode);
this.$element.addClass(classes.sorting);
$(this.options.items, this.$element).off(events.dragstart);
$body.on(events.drag, function (e) {
self.drag(e);
})
.on(events.dragend, function (e) {
self.dragend(e);
})
.on(events.selectstart, function (e) {
e.preventDefault();
return false;
});
this.state = event;
};
Sortable.prototype.dragend = function (event) {
var self = this;
self.draggingIdx = null;
this.options.onDragend(event);
if (event.isPropagationStopped()) {
return;
}
$body.attr('unselectable', self.bodyUnselectable)
.off(events.drag)
.off(events.dragend)
.off(events.selectstart);
$(this.options.items, this.$element)
.on(events.dragstart, function (e) {
return self.dragstart(e);
});
this.$activeItem.removeClass(classes.active);
this.$activeItem = null;
this.$element.removeClass(classes.sorting);
this.$dragElement.remove();
};
var safeApply = function ($scope, fn) {
var phase = $scope.$root.$$phase;
if (phase === '$apply' || phase === '$digest') {
if (fn && (typeof (fn) === 'function')) {
fn();
}
} else {
$scope.$root.$apply(fn);
}
};
angular.module('sortable', []).directive('ngSortable', function () {
return {
restrict: 'A',
scope: {
ngSortable: '=',
ngSortableItems: '@',
ngSortableHandles: '@',
ngSortableZindex: '@',
ngSortableDisable: '=',
ngSortableOnChange: '=',
ngSortableOnDrag: '=',
ngSortableOnDragstart: '=',
ngSortableOnDragend: '='
},
link: function ($scope, $element, $attrs) {
var items = $scope.ngSortable;
if (!items) {
items = [];
}
function onChange(fromIdx, toIdx) {
safeApply($scope, function () {
var temp = items.splice(fromIdx, 1);
items.splice(toIdx, 0, temp[0]);
});
}
var options = {
items: $scope.ngSortableItems,
handles: $scope.ngSortableHandles,
zindex: $scope.ngSortableZindex,
onChange: onChange,
onDrag: $scope.ngSortableOnDrag || $.noop,
onDragstart: $scope.ngSortableOnDragstart || $.noop,
onDragend: $scope.ngSortableOnDragend || $.noop
};
if ($scope.ngSortableOnChange) {
options.onChange = function (fromIdx, toIdx) {
onChange(fromIdx, toIdx);
$scope.ngSortableOnChange(fromIdx, toIdx);
};
}
var sortable = new Sortable($element, options);
$scope.$watch('ngSortableDisable', function () {
sortable.enable(!$scope.ngSortableDisable);
});
$scope.$watch('ngSortable.length', function () {
sortable.refresh();
});
}
};
});
}(jQuery));