Skip to content

Commit 2ed8c04

Browse files
author
Angeline Chew
authored
Fix Pull Request dbushell#170 (Restrict Level Change)
Following Pull Request dbushell#170 and added fix to allow an item to be dragged onto a different parent which doesn't already have children. Also fixed syntax error on line 143.
1 parent 4f93032 commit 2ed8c04

1 file changed

Lines changed: 69 additions & 26 deletions

File tree

jquery.nestable.js

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,25 @@
2727
})();
2828

2929
var defaults = {
30-
listNodeName : 'ol',
31-
itemNodeName : 'li',
32-
rootClass : 'dd',
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>',
43-
group : 0,
44-
maxDepth : 5,
45-
threshold : 20
46-
};
30+
listNodeName : 'ol',
31+
itemNodeName : 'li',
32+
rootClass : 'dd',
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>',
43+
group : 0,
44+
maxDepth : 5,
45+
threshold : 20,
46+
allowDecrease : true,
47+
allowIncrease : true
48+
};
4749

4850
function Plugin(element, options)
4951
{
@@ -140,7 +142,7 @@
140142
{
141143
var data,
142144
depth = 0,
143-
list = this;
145+
list = this,
144146
step = function(level, depth)
145147
{
146148
var array = [ ],
@@ -231,13 +233,20 @@
231233
});
232234
},
233235

236+
isParent: function(li)
237+
{
238+
return (li.find('button[data-action]').length != 0);
239+
},
240+
234241
setParent: function(li)
235242
{
236-
if (li.children(this.options.listNodeName).length) {
237-
li.prepend($(this.options.expandBtnHTML));
238-
li.prepend($(this.options.collapseBtnHTML));
243+
if (!this.isParent(li)) {
244+
if (li.children(this.options.listNodeName).length) {
245+
li.prepend($(this.options.expandBtnHTML));
246+
li.prepend($(this.options.collapseBtnHTML));
247+
}
248+
li.children('[data-action="expand"]').hide();
239249
}
240-
li.children('[data-action="expand"]').hide();
241250
},
242251

243252
unsetParent: function(li)
@@ -358,8 +367,9 @@
358367
// reset move distance on x-axis for new phase
359368
mouse.distAxX = 0;
360369
prev = this.placeEl.prev(opt.itemNodeName);
370+
361371
// increase horizontal level if previous sibling exists and is not collapsed
362-
if (mouse.distX > 0 && prev.length && !prev.hasClass(opt.collapsedClass)) {
372+
if (opt.allowIncrease && mouse.distX > 0 && prev.length && !prev.hasClass(opt.collapsedClass)) {
363373
// cannot increase level when item above is collapsed
364374
list = prev.find(opt.listNodeName).last();
365375
// check if depth limit has reached
@@ -378,8 +388,9 @@
378388
}
379389
}
380390
}
391+
381392
// decrease horizontal level
382-
if (mouse.distX < 0) {
393+
if (opt.allowDecrease && mouse.distX < 0) {
383394
// we can't decrease a level if an item preceeds the current one
384395
next = this.placeEl.next(opt.itemNodeName);
385396
if (!next.length) {
@@ -420,17 +431,49 @@
420431
* move vertical
421432
*/
422433
if (!mouse.dirAx || isNewRoot || isEmpty) {
434+
// get previously hovered element based on direction of mouse movement
435+
if (mouse.dirY == 1) {
436+
prev = this.pointEl.prev(opt.itemNodeName);
437+
} else if (mouse.dirY == -1) {
438+
prev = this.pointEl.next(opt.itemNodeName);
439+
}
440+
423441
// check if groups match if dragging over new root
424442
if (isNewRoot && opt.group !== pointElRoot.data('nestable-group')) {
425443
return;
426444
}
427445
// check depth limit
446+
var currentDepth = this.placeEl.parents(opt.listNodeName).length;
428447
depth = this.dragDepth - 1 + this.pointEl.parents(opt.listNodeName).length;
448+
429449
if (depth > opt.maxDepth) {
430450
return;
431451
}
452+
453+
if (!opt.allowIncrease && currentDepth < depth) {
454+
return;
455+
}
456+
457+
if (!opt.allowDecrease && currentDepth > depth) {
458+
// set parent for currently hovered element
459+
list = this.pointEl.find(opt.listNodeName).last();
460+
if (!list.length) {
461+
list = $(document.createElement(opt.listNodeName)).addClass(opt.listClass);
462+
}
463+
list.append(this.placeEl);
464+
this.pointEl.append(list);
465+
this.setParent(this.pointEl);
466+
467+
// unset parent for previously hovered element
468+
if (prev != undefined && this.isParent(prev) && prev.find(opt.listNodeName).children().length == 0) {
469+
this.unsetParent(prev);
470+
}
471+
472+
return;
473+
}
474+
432475
var before = e.pageY < (this.pointEl.offset().top + this.pointEl.height() / 2);
433-
parent = this.placeEl.parent();
476+
parent = this.placeEl.parent();
434477
// if empty create new list to replace empty placeholder
435478
if (isEmpty) {
436479
list = $(document.createElement(opt.listNodeName)).addClass(opt.listClass);
@@ -481,4 +524,4 @@
481524
return retval || lists;
482525
};
483526

484-
})(window.jQuery || window.Zepto, window, document);
527+
})(window.jQuery || window.Zepto, window, document);

0 commit comments

Comments
 (0)