PDA

View Full Version : [3.x] Ext.ux.strictMultiSelectionModel plugin



Drayton
14 Jan 2010, 7:14 AM
Here's a small plugin i wrote to make the behavior of Ext.tree.MultiSelectionModel fit a group selection scenario a little better.

When a node is selected all other nodes in the direct path (both ways) of this node is unselected automatically. Parallel nodes can be selected as normal.

You might ask why I don't select all childs to visually indicate "this node and all its descendants", but doing this on a async tree is kinda counterproductive as you then need to load all child nodes when the only thing you wanted to do was to select the "Staff" node to imply "everyone in Staff" and move on.

Also I haven't found an event where I could visually select nodes as they are loaded.


Ext.ux.strictMultiSelectionModel = function(){

return {
init: function(tp){

tp.on('render',function(tp){

var sm = tp.getSelectionModel();

if(!sm instanceof Ext.tree.MultiSelectionModel){
return;
}

sm.unSelAllChildNodes = function(sm,node){
sm.suspendEvents();
node.eachChild( function(cn){
cn.unselect();
// Recursion!
if(cn.hasChildNodes()){
sm.unSelAllChildNodes(sm,cn);
}
});
sm.resumeEvents();
};

sm.unSelAllParentNodes = function(sm,node){
if(!node.parentNode){
return;
}
sm.suspendEvents();
node.parentNode.unselect();
// Recursion!
sm.unSelAllParentNodes(sm,node.parentNode);
sm.resumeEvents();
}

sm.on('selectionchange',function(sm,nodes){
if(nodes.length == 0){
return;
}
var ln = nodes[nodes.length-1];
sm.unSelAllParentNodes(sm,ln);
if(ln.hasChildNodes()){
sm.unSelAllChildNodes(sm,ln);
}
})

});

}
}

};