Maintain node hierarchy using unique keys

rel-1.0
Vitaliy Filippov 2016-06-05 01:37:11 +03:00
parent ede717a8f1
commit 9b4d846e16
1 changed files with 47 additions and 10 deletions

View File

@ -14,6 +14,7 @@
* renderer = function(node) { return [ { innerHTML, style, className, title }, ... ] }
*
* items: [ node={
* key: <unique key of this node across siblings>,
* children: [ node... ],
* leaf: true/false,
* collapsed: true/false,
@ -351,6 +352,7 @@ function TreeGridNode(node, grid, level, insertBefore, startHidden)
{
this.grid = grid;
this.level = level;
this.key = node.key;
this.data = node.data;
if (this.level === undefined)
this.level = -1;
@ -369,9 +371,23 @@ function TreeGridNode(node, grid, level, insertBefore, startHidden)
insertBefore ? grid.tbody.insertBefore(this.tr, insertBefore) : grid.tbody.appendChild(this.tr);
}
this.children = [];
this.childrenByKey = {};
if (node.children)
{
for (var i = 0; i < node.children.length; i++)
this.children.push(new TreeGridNode(node.children[i], grid, this.level+1, insertBefore, this.collapsed));
{
var child = new TreeGridNode(node.children[i], grid, this.level+1, insertBefore, this.collapsed);
child._index = i;
this.children.push(child);
if (child.key)
this.childrenByKey[child.key] = child;
}
}
}
TreeGridNode.prototype.getChildByKey = function(key)
{
return this.childrenByKey[key];
}
TreeGridNode.prototype._addCollapser = function()
@ -507,11 +523,12 @@ TreeGridNode.prototype.setChildren = function(isLeaf, newChildren)
}
this.leaf = isLeaf;
this.children = [];
this.childrenByKey = {};
this.addChildren(newChildren);
}
// experimental
TreeGridNode.prototype.syncChildren = function()
// experimental & broken
TreeGridNode.prototype._syncChildren = function()
{
var i, j;
for (var i = 0; i < this.children.length; i++)
@ -574,13 +591,24 @@ TreeGridNode.prototype.syncChildren = function()
}
}
TreeGridNode.prototype.removeChild = function(i)
TreeGridNode.prototype.removeChild = function(nodeOrIndex)
{
if (!this.children[i])
return null;
var e = this.children[i].tr;
var l = this.children[i].level;
this.children.splice(i, 1);
if (nodeOrIndex instanceof TreeGridNode)
{
if (this.children[nodeOrIndex._index] == nodeOrIndex)
nodeOrIndex = nodeOrIndex._index;
else
return false;
}
if (!this.children[nodeOrIndex])
return false;
var e = this.children[nodeOrIndex].tr;
var l = this.children[nodeOrIndex].level;
if (this.children[nodeOrIndex].key)
delete this.childrenByKey[this.children[nodeOrIndex].key];
this.children.splice(nodeOrIndex, 1);
for (var i = nodeOrIndex; i < this.children.length; i++)
this.children[i]._index--;
var k;
do
{
@ -588,6 +616,7 @@ TreeGridNode.prototype.removeChild = function(i)
e = e.nextSibling;
k.parentNode.removeChild(k);
} while (e && e._node.level > l);
return true;
}
TreeGridNode.prototype.addChildren = function(nodes, insertBefore)
@ -607,6 +636,14 @@ TreeGridNode.prototype.addChildren = function(nodes, insertBefore)
else
e = this.children[insertBefore].tr;
for (var i = 0; i < nodes.length; i++)
this.children.splice(insertBefore+i, 0, new TreeGridNode(nodes[i], this.grid, this.level+1, e, this.collapsed));
{
var child = new TreeGridNode(nodes[i], this.grid, this.level+1, e, this.collapsed);
child._index = insertBefore+i;
this.children.splice(insertBefore+i, 0, child);
if (child.key)
this.childrenByKey[child.key] = child;
}
for (var i = insertBefore+nodes.length; i < this.children.length; i++)
this.children[i]._index += nodes.length;
return this.children.slice(insertBefore, nodes.length);
}