From e304ca336dd5ec3a320bd1388ca0ad38e52df3d1 Mon Sep 17 00:00:00 2001 From: dangoodman Date: Mon, 9 Mar 2015 21:40:29 +0300 Subject: [PATCH] Keep branchClass on root item if protectRoot is true This also make _clearEmpty() code a bit more readable --- jquery.mjs.nestedSortable.js | 57 +++++++++++++++++------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/jquery.mjs.nestedSortable.js b/jquery.mjs.nestedSortable.js index 7672dbe..47ebbc7 100644 --- a/jquery.mjs.nestedSortable.js +++ b/jquery.mjs.nestedSortable.js @@ -794,40 +794,37 @@ }, - _clearEmpty: function(item) { + _clearEmpty: function (item) { + function replaceClass(elem, search, replace, swap) { + if (swap) { + search = [replace, replace = search][0]; + } + + $(elem).removeClass(search).addClass(replace); + } + var o = this.options, - emptyList = $(item).children(o.listType); - - if (emptyList.length && !emptyList.children().length && !o.doNotClear) { - if (o.isTree) { - $(item) - .removeClass(o.branchClass + " " + o.expandedClass) - .addClass(o.leafClass); + childrenList = $(item).children(o.listType), + hasChildren = childrenList.is(':not(:empty)'); + + var doNotClear = + o.doNotClear || + hasChildren || + o.protectRoot && $(item)[0] === this.element[0]; + + if (o.isTree) { + replaceClass(item, o.branchClass, o.leafClass, doNotClear); + + if (doNotClear && hasChildren) { + replaceClass(item, o.collapsedClass, o.expandedClass); } - - emptyList.remove(); - } else if ( - o.isTree && - emptyList.length && - emptyList.children().length && - emptyList.is(":visible") - ) { - $(item) - .removeClass(o.leafClass) - .addClass(o.branchClass + " " + o.expandedClass); - } else if ( - o.isTree && - emptyList.length && - emptyList.children().length && - !emptyList.is(":visible") - ) { - $(item) - .removeClass(o.leafClass) - .addClass(o.branchClass + " " + o.collapsedClass); } - + + if (!doNotClear) { + childrenList.remove(); + } }, - + _getLevel: function(item) { var level = 1,