5
;(function($, window, document, undefined)
7
var hasTouch = 'ontouchstart' in document;
14
var hasPointerEvents = (function()
16
var el = document.createElement('div'),
17
docEl = document.documentElement;
18
if (!('pointerEvents' in el.style)) {
21
el.style.pointerEvents = 'auto';
22
el.style.pointerEvents = 'x';
23
docEl.appendChild(el);
24
var supports = window.getComputedStyle && window.getComputedStyle(el, '').pointerEvents === 'auto';
25
docEl.removeChild(el);
33
listClass : 'dd-list',
34
itemClass : 'dd-item',
35
dragClass : 'dd-dragel',
36
handleClass : 'dd-handle',
37
collapsedClass : 'dd-collapsed',
38
placeClass : 'dd-placeholder',
39
noDragClass : 'dd-nodrag',
40
emptyClass : 'dd-empty',
41
expandBtnHTML : '<button data-action="expand" type="button">Expand</button>',
42
collapseBtnHTML : '<button data-action="collapse" type="button">Collapse</button>',
48
function Plugin(element, options)
52
this.options = $.extend({}, defaults, options);
64
list.el.data('nestable-group', this.options.group);
66
list.placeEl = $('<div class="' + list.options.placeClass + '"/>');
68
$.each(this.el.find(list.options.itemNodeName), function(k, el) {
69
list.setParent($(el));
72
list.el.on('click', 'button', function(e) {
76
var target = $(e.currentTarget),
77
action = target.data('action'),
78
item = target.parent(list.options.itemNodeName);
79
if (action === 'collapse') {
80
list.collapseItem(item);
82
if (action === 'expand') {
83
list.expandItem(item);
87
var onStartEvent = function(e)
89
var handle = $(e.target);
90
if (!handle.hasClass(list.options.handleClass)) {
91
if (handle.closest('.' + list.options.noDragClass).length) {
94
handle = handle.closest('.' + list.options.handleClass);
97
if (!handle.length || list.dragEl) {
101
list.isTouch = /^touch/.test(e.type);
102
if (list.isTouch && e.touches.length !== 1) {
107
list.dragStart(e.touches ? e.touches[0] : e);
110
var onMoveEvent = function(e)
114
list.dragMove(e.touches ? e.touches[0] : e);
118
var onEndEvent = function(e)
122
list.dragStop(e.touches ? e.touches[0] : e);
127
list.el[0].addEventListener('touchstart', onStartEvent, false);
128
window.addEventListener('touchmove', onMoveEvent, false);
129
window.addEventListener('touchend', onEndEvent, false);
130
window.addEventListener('touchcancel', onEndEvent, false);
133
list.el.on('mousedown', onStartEvent);
134
list.w.on('mousemove', onMoveEvent);
135
list.w.on('mouseup', onEndEvent);
139
serialize: function()
144
step = function(level, depth)
147
items = level.children(list.options.itemNodeName);
148
items.each(function()
151
item = $.extend({}, li.data()),
152
sub = li.children(list.options.listNodeName);
154
item.children = step(sub, depth + 1);
160
data = step(list.el.find(list.options.listNodeName).first(), depth);
164
serialise: function()
166
return this.serialize();
190
this.isTouch = false;
193
this.dragRootEl = null;
195
this.hasNewRoot = false;
199
expandItem: function(li)
201
li.removeClass(this.options.collapsedClass);
202
li.children('[data-action="expand"]').hide();
203
li.children('[data-action="collapse"]').show();
204
li.children(this.options.listNodeName).show();
207
collapseItem: function(li)
209
var lists = li.children(this.options.listNodeName);
211
li.addClass(this.options.collapsedClass);
212
li.children('[data-action="collapse"]').hide();
213
li.children('[data-action="expand"]').show();
214
li.children(this.options.listNodeName).hide();
218
expandAll: function()
221
list.el.find(list.options.itemNodeName).each(function() {
222
list.expandItem($(this));
226
collapseAll: function()
229
list.el.find(list.options.itemNodeName).each(function() {
230
list.collapseItem($(this));
234
setParent: function(li)
236
if (li.children(this.options.listNodeName).length) {
237
li.prepend($(this.options.expandBtnHTML));
238
li.prepend($(this.options.collapseBtnHTML));
240
li.children('[data-action="expand"]').hide();
243
unsetParent: function(li)
245
li.removeClass(this.options.collapsedClass);
246
li.children('[data-action]').remove();
247
li.children(this.options.listNodeName).remove();
250
dragStart: function(e)
252
var mouse = this.mouse,
253
target = $(e.target),
254
dragItem = target.closest(this.options.itemNodeName);
256
this.placeEl.css('height', dragItem.height());
258
mouse.offsetX = e.offsetX !== undefined ? e.offsetX : e.pageX - target.offset().left;
259
mouse.offsetY = e.offsetY !== undefined ? e.offsetY : e.pageY - target.offset().top;
260
mouse.startX = mouse.lastX = e.pageX;
261
mouse.startY = mouse.lastY = e.pageY;
263
this.dragRootEl = this.el;
265
this.dragEl = $(document.createElement(this.options.listNodeName)).addClass(this.options.listClass + ' ' + this.options.dragClass);
266
this.dragEl.css('width', dragItem.width());
268
dragItem.after(this.placeEl);
269
dragItem[0].parentNode.removeChild(dragItem[0]);
270
dragItem.appendTo(this.dragEl);
272
$(document.body).append(this.dragEl);
274
'left' : e.pageX - mouse.offsetX,
275
'top' : e.pageY - mouse.offsetY
279
items = this.dragEl.find(this.options.itemNodeName);
280
for (i = 0; i < items.length; i++) {
281
depth = $(items[i]).parents(this.options.listNodeName).length;
282
if (depth > this.dragDepth) {
283
this.dragDepth = depth;
288
dragStop: function(e)
290
var el = this.dragEl.children(this.options.itemNodeName).first();
291
el[0].parentNode.removeChild(el[0]);
292
this.placeEl.replaceWith(el);
294
this.dragEl.remove();
295
this.el.trigger('change');
296
if (this.hasNewRoot) {
297
this.dragRootEl.trigger('change');
302
dragMove: function(e)
304
var list, parent, prev, next, depth,
309
'left' : e.pageX - mouse.offsetX,
310
'top' : e.pageY - mouse.offsetY
314
mouse.lastX = mouse.nowX;
315
mouse.lastY = mouse.nowY;
317
mouse.nowX = e.pageX;
318
mouse.nowY = e.pageY;
320
mouse.distX = mouse.nowX - mouse.lastX;
321
mouse.distY = mouse.nowY - mouse.lastY;
323
mouse.lastDirX = mouse.dirX;
324
mouse.lastDirY = mouse.dirY;
326
mouse.dirX = mouse.distX === 0 ? 0 : mouse.distX > 0 ? 1 : -1;
327
mouse.dirY = mouse.distY === 0 ? 0 : mouse.distY > 0 ? 1 : -1;
329
var newAx = Math.abs(mouse.distX) > Math.abs(mouse.distY) ? 1 : 0;
339
if (mouse.dirAx !== newAx) {
343
mouse.distAxX += Math.abs(mouse.distX);
344
if (mouse.dirX !== 0 && mouse.dirX !== mouse.lastDirX) {
347
mouse.distAxY += Math.abs(mouse.distY);
348
if (mouse.dirY !== 0 && mouse.dirY !== mouse.lastDirY) {
357
if (mouse.dirAx && mouse.distAxX >= opt.threshold) {
360
prev = this.placeEl.prev(opt.itemNodeName);
362
if (mouse.distX > 0 && prev.length && !prev.hasClass(opt.collapsedClass)) {
364
list = prev.find(opt.listNodeName).last();
366
depth = this.placeEl.parents(opt.listNodeName).length;
367
if (depth + this.dragDepth <= opt.maxDepth) {
370
list = $('<' + opt.listNodeName + '/>').addClass(opt.listClass);
371
list.append(this.placeEl);
373
this.setParent(prev);
376
list = prev.children(opt.listNodeName).last();
377
list.append(this.placeEl);
382
if (mouse.distX < 0) {
384
next = this.placeEl.next(opt.itemNodeName);
386
parent = this.placeEl.parent();
387
this.placeEl.closest(opt.itemNodeName).after(this.placeEl);
388
if (!parent.children().length) {
389
this.unsetParent(parent.parent());
398
if (!hasPointerEvents) {
399
this.dragEl[0].style.visibility = 'hidden';
401
this.pointEl = $(document.elementFromPoint(e.pageX - document.body.scrollLeft, e.pageY - (window.pageYOffset || document.documentElement.scrollTop)));
402
if (!hasPointerEvents) {
403
this.dragEl[0].style.visibility = 'visible';
405
if (this.pointEl.hasClass(opt.handleClass)) {
406
this.pointEl = this.pointEl.parent(opt.itemNodeName);
408
if (this.pointEl.hasClass(opt.emptyClass)) {
411
else if (!this.pointEl.length || !this.pointEl.hasClass(opt.itemClass)) {
416
var pointElRoot = this.pointEl.closest('.' + opt.rootClass),
417
isNewRoot = this.dragRootEl.data('nestable-id') !== pointElRoot.data('nestable-id');
422
if (!mouse.dirAx || isNewRoot || isEmpty) {
424
if (isNewRoot && opt.group !== pointElRoot.data('nestable-group')) {
428
depth = this.dragDepth - 1 + this.pointEl.parents(opt.listNodeName).length;
429
if (depth > opt.maxDepth) {
432
var before = e.pageY < (this.pointEl.offset().top + this.pointEl.height() / 2);
433
parent = this.placeEl.parent();
436
list = $(document.createElement(opt.listNodeName)).addClass(opt.listClass);
437
list.append(this.placeEl);
438
this.pointEl.replaceWith(list);
441
this.pointEl.before(this.placeEl);
444
this.pointEl.after(this.placeEl);
446
if (!parent.children().length) {
447
this.unsetParent(parent.parent());
449
if (!this.dragRootEl.find(opt.itemNodeName).length) {
450
this.dragRootEl.append('<div class="' + opt.emptyClass + '"/>');
454
this.dragRootEl = pointElRoot;
455
this.hasNewRoot = this.el[0] !== this.dragRootEl[0];
462
$.fn.nestable = function(params)
467
lists.each(function()
469
var plugin = $(this).data("nestable");
472
$(this).data("nestable", new Plugin(this, params));
473
$(this).data("nestable-id", new Date().getTime());
475
if (typeof params === 'string' && typeof plugin[params] === 'function') {
476
retval = plugin[params]();
481
return retval || lists;
484
})(window.jQuery || window.Zepto, window, document);