Skip to content

Commit a68f5b0

Browse files
committed
many fixes and improvements
1 parent f4bb51e commit a68f5b0

1 file changed

Lines changed: 45 additions & 17 deletions

File tree

jquery.mjs.nestedSortable.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
isTree: false,
3030
branchClass: 'mjs-nestedSortable-branch',
3131
leafClass: 'mjs-nestedSortable-leaf',
32-
collapsedClass: 'mjs-nestedSortable-collapse',
33-
expandedClass: 'mjs-nestedSortable-expand',
34-
expandOnHover: 200,
32+
collapsedClass: 'mjs-nestedSortable-collapsed',
33+
expandedClass: 'mjs-nestedSortable-expanded',
34+
expandOnHover: 500,
3535
startCollapsed: true
3636
},
3737

@@ -41,19 +41,22 @@
4141
if (!this.element.is(this.options.listType))
4242
throw new Error('nestedSortable: Please check that the listType option is set to your actual list type');
4343

44+
var self = this;
4445
// this goes through any any any list item, but what if a list is present but is not part of the sortable?
4546
this.element.find('li').each(function() {
4647
var $li = $(this);
47-
if ($li.children('ul').length) {
48-
$li.addClass(this.options.branchClass);
48+
if ($li.children(self.options.listType).length) {
49+
$li.addClass(self.options.branchClass);
4950
// expand/collapse class only if they have children
50-
if (this.options.startCollapsed) $li.addClass(this.options.collapsedClass);
51-
else $li.addClass(this.options.expandedClass);
51+
if (self.options.startCollapsed) $li.addClass(self.options.collapsedClass);
52+
else $li.addClass(self.options.expandedClass);
5253
} else {
53-
$li.addClass(this.options.leafClass);
54+
$li.addClass(self.options.leafClass);
5455
}
5556
})
5657

58+
if (this.options.isTree) this.options.tolerance = 'intersect';
59+
5760
return $.ui.sortable.prototype._create.apply(this, arguments);
5861
},
5962

@@ -111,13 +114,15 @@
111114
//Regenerate the absolute position used for position checks
112115
this.positionAbs = this._convertPositionTo("absolute");
113116

114-
// Find the top offset before rearrangement,
115-
var previousTopOffset = this.placeholder.offset().top;
117+
// Find the top offset before rearrangement,
118+
var previousTopOffset = this.placeholder.offset().top;
116119

117120
//Set the helper position
118121
if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
119122
if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';
120123

124+
this.hovering = this.hovering ? this.hovering : null;
125+
121126
//Rearrange
122127
for (var i = this.items.length - 1; i >= 0; i--) {
123128

@@ -136,14 +141,18 @@
136141

137142
// if the element has children and they are hidden, show them after some time
138143
if ($(itemElement).hasClass(o.collapsedClass)) {
139-
TOID = window.setTimeout(function() { $(itemElement).removeClass(o.collapsedClass).addClass(o.expandedClass) }, o.expandOnHover);
144+
if (!this.hovering) {
145+
this.hovering = window.setTimeout(function() { $(itemElement).removeClass(o.collapsedClass).addClass(o.expandedClass) }, o.expandOnHover);
146+
console.log('started '+this.hovering);
147+
}
140148
}
141149

142150
this.direction = intersection == 1 ? "down" : "up";
143151

144-
if ( (this.options.tolerance == "pointer" && !o.isTree) || this._intersectsWithSides(item)) {
152+
if (this.options.tolerance == "pointer" || this._intersectsWithSides(item)) {
145153
$(itemElement).mouseleave();
146-
window.clearTimeout(TOID);
154+
this.hovering && window.clearTimeout(this.hovering);console.log('cleared '+this.hovering);
155+
this.hovering = null;
147156
this._rearrange(event, item);
148157
} else {
149158
break;
@@ -198,21 +207,27 @@
198207
if (parentItem != null && nextItem == null &&
199208
(o.rtl && (this.positionAbs.left + this.helper.outerWidth() > parentItem.offset().left + parentItem.outerWidth()) ||
200209
!o.rtl && (this.positionAbs.left < parentItem.offset().left))) {
210+
201211
parentItem.after(this.placeholder[0]);
212+
if (parentItem.children(o.listItem).children('li:visible:not(.ui-sortable-helper)').length < 1) {
213+
parentItem.removeClass(this.options.branchClass + ' ' + this.options.expandedClass)
214+
.addClass(this.options.leafClass);
215+
}
202216
this._clearEmpty(parentItem[0]);
203217
this._trigger("change", event, this._uiHash());
204218
}
205219
// If the item is below a sibling and is moved to the right, make it a child of that sibling.
206220
else if (previousItem != null &&
221+
(previousItem.children(o.listType).length && previousItem.children(o.listType).is(':visible') || !previousItem.children(o.listType).length) &&
207222
(o.rtl && (this.positionAbs.left + this.helper.outerWidth() < previousItem.offset().left + previousItem.outerWidth() - o.tabSize) ||
208223
!o.rtl && (this.positionAbs.left > previousItem.offset().left + o.tabSize))) {
209224

210225
this._isAllowed(previousItem, level, level+childLevels+1);
211226

212227
if (!previousItem.children(o.listType).length) {
213-
previousItem[0].removeClass(o.leafClass)
228+
previousItem.removeClass(o.leafClass)
214229
.addClass(o.branchClass + ' ' + o.expandedClass)
215-
.appendChild(newList);
230+
.append(newList);
216231
}
217232

218233
// If this item is being moved from the top, add it to the top of the list.
@@ -261,14 +276,20 @@
261276

262277
}
263278

279+
$.ui.sortable.prototype._mouseStop.apply(this, arguments);
280+
281+
},
282+
283+
_clear: function(event, noPropagation) {
284+
285+
$.ui.sortable.prototype._clear.apply(this, arguments);
286+
264287
// Clean last empty ul/ol
265288
for (var i = this.items.length - 1; i >= 0; i--) {
266289
var item = this.items[i].item[0];
267290
this._clearEmpty(item);
268291
}
269292

270-
$.ui.sortable.prototype._mouseStop.apply(this, arguments);
271-
272293
},
273294

274295
serialize: function(options) {
@@ -391,10 +412,17 @@
391412
_clearEmpty: function(item) {
392413

393414
var emptyList = $(item).children(this.options.listType);
415+
394416
if (emptyList.length && !emptyList.children().length) {
395417
$(item).removeClass(this.options.branchClass + ' ' + this.options.expandedClass)
396418
.addClass(this.options.leafClass);
397419
emptyList.remove();
420+
} else if (emptyList.length && emptyList.children().length && emptyList.is(':visible')) {
421+
$(item).removeClass(this.options.leafClass)
422+
.addClass(this.options.branchClass + ' ' + this.options.expandedClass);
423+
} else if (emptyList.length && emptyList.children().length && !emptyList.is(':visible')) {
424+
$(item).removeClass(this.options.leafClass)
425+
.addClass(this.options.branchClass + ' ' + this.options.collapsedClass);
398426
}
399427

400428
},

0 commit comments

Comments
 (0)