Skip to content

Commit

Permalink
[ui:tree] performance tuning of dir tree making
Browse files Browse the repository at this point in the history
ref. #984 - Lazy loading on tree list
  • Loading branch information
nao-pon committed Jul 6, 2015
1 parent 6b9d146 commit 760a415
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions js/ui/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,14 @@ $.fn.elfindertree = function(fm, opts) {
*/
findSibling = function(subtree, dir) {
var node = subtree.children(':first'),
info;
info, compare;

compare = fm.naturalCompare;
while (node.length) {
info = fm.file(fm.navId2Hash(node.children('[id]').attr('id')));

if ((info = fm.file(fm.navId2Hash(node.children('[id]').attr('id'))))
&& dir.name.toLowerCase().localeCompare(info.name.toLowerCase()) < 0) {
&& compare(dir.name, info.name) < 0) {
return node;
}
node = node.next();
Expand All @@ -254,7 +255,7 @@ $.fn.elfindertree = function(fm, opts) {
var length = dirs.length,
orphans = [],
i = dirs.length,
dir, html, parent, sibling;
dir, html, parent, sibling, init, atonce = {};

var firstVol = true; // check for netmount volume
while (i--) {
Expand All @@ -265,18 +266,37 @@ $.fn.elfindertree = function(fm, opts) {
}

if ((parent = findSubtree(dir.phash)).length) {
html = itemhtml(dir);
if (dir.phash && (sibling = findSibling(parent, dir)).length) {
sibling.before(html);
if (dir.phash && ((init = !parent.children().length) || (sibling = findSibling(parent, dir)).length)) {
if (init) {
if (!atonce[dir.phash]) {
atonce[dir.phash] = [];
}
atonce[dir.phash].push(dir);
} else {
sibling.before(itemhtml(dir));
}
} else {
parent[firstVol || dir.phash ? 'append' : 'prepend'](html);
parent[firstVol || dir.phash ? 'append' : 'prepend'](itemhtml(dir));
firstVol = false;
}
} else {
orphans.push(dir);
}
}

// When init, html append at once
if (Object.keys(atonce).length){
$.each(atonce, function(p, dirs){
var parent = findSubtree(p),
html = [];
dirs.sort(compare);
$.each(dirs, function(i, d){
html.push(itemhtml(d));
});
parent.append(html.join(''));
});
}

if (orphans.length && orphans.length < length) {
return updateTree(orphans);
}
Expand All @@ -289,6 +309,14 @@ $.fn.elfindertree = function(fm, opts) {

},

/**
* sort function by dir.name
*
*/
compare = function(dir1, dir2) {
return fm.naturalCompare(dir1.name, dir2.name);
},

/**
* Auto scroll to cwd
*
Expand Down

0 comments on commit 760a415

Please sign in to comment.