Inserting a node at a specific position

27 views
Skip to first unread message

fm

unread,
Feb 1, 2011, 2:14:38 AM2/1/11
to mootree
I am trying to extend the code to insert a node at a specific position/
after a specific node in the existing tree instead of appending at the
end.

Example:
Consider a tree like:
Root
->ABC
->XYZ

On clicking ABC, I am removing ABC and adding its children. So, the
tree becomes something like:
Root
->XYZ
->ABC1 (child of ABC)
->ABC2 (child of ABC)
->ABC3 (child of ABC)


Instead of this what I want the user to see is:
Root
->ABC1 (child of ABC)
->ABC2 (child of ABC)
->ABC3 (child of ABC)
->XYZ

So I made a few changes as below:
1. Added position to MooTreeNode options
this.position = $defined(options.position) ? options.position : null;

2.In MooTreeNode: initialize() added
if(this.position != null){
var prevNode = this.parent.nodes[this.position];
var prevNodeDiv = prevNode.div;
$(prevNodeDiv.main).inject(options.div , 'after');
$(prevNodeDiv.sub).inject(options.div, 'after');
}

3. In MooTreeNode: insert() added
if(this.position != undefined){
for(var i = n.length-1; i > this.position ; i--){
n[i+1] = n[i];
}
n[this.position +1] = node;
}else{
n.push(node);
}


Now the new nodes are getting added at the specified position but this
seems to have disturbed the parent-child div relationship. So expand/
collapse etc are not working.

Could you please point me in the right direction for extending the
code for this purpose.

Thanks

mindplay.dk

unread,
Feb 1, 2011, 1:20:29 PM2/1/11
to mootree
I quit using MooTools about 3 years ago and switched to jQuery, so I'm
no longer maintaining this library.

Perhaps someone else can help...

fm

unread,
Feb 2, 2011, 3:45:40 AM2/2/11
to mootree
Not a problem, thanks for replying though.

I figured out where I was going wrong.
The order in which I was injecting the nodes was incorrect. Also I had
accidentally used the 'options' node object instead of 'this'
So with the following changes it is working fine.

In MooTreeNode: initialize(options) added
if(this.position != undefined && this.position != null){
var prevNode = this.parent.nodes[this.position-1];
var prevNodeDiv = prevNode.div;
//Insert the current main div after previous sub div
$(this.div.main).inject(prevNodeDiv.sub, 'after');
//Insert the current sub div after the current main div
$(this.div.sub).inject(this.div.main, 'after');
}

Thanks,
Reply all
Reply to author
Forward
0 new messages