PDA

View Full Version : Reload treePanel with different rootnode, but uncheck all layers first?



Planet_x
15 Nov 2010, 7:08 AM
I am having a slight issue with using a TreePanel and GeoExt.LayerContainer (AsyncTreeNode) to have a legend related to my current layers shown and then switch to a different "theme" that removes the current map layers and adds new ones. Except I need to be able to uncheck all the layers of my rootnode and then rebuild the treepanel with new nodes based on the new layers. Is there any easy way to refresh my root node with new AsyncNodes or should I just create one from scratch each time? My problem is the unchecking of the nodes. Below is how I declare everything and I tried to loop through it to uncheck each node if it was checked except when I switch to a different them I am getting errors like enableDD is null or not an object. Appreciate any assistance on what is wrong with my code or if there is a completely easier way to do this I just don't know about, thanks.



var layerRoot_Legend = new Ext.tree.TreeNode({
enableDD: true,
text: "All Legend Items",
expanded: true
});
var layerTree_Legend = new Ext.tree.TreePanel({
title: '',
root: layerRoot_Legend,
enableDD: true,
applyLoader: true,
rootVisible: false
});

//Labels layerList
layerList_Labels = new GeoExt.tree.LayerContainer({
text: 'Labels',
layerStore: mapPanel.layers,
leaf: false,
cls: 'rootnode',
enableDD: true,
singleClickExpand: true,
expanded: false,
checked: false,
listeners: {
'checkchange' : function(node, checked)
{
// If a parent node is unchecked, uncheck all the children
if (node.getUI().isChecked()) {
node.expand();
node.eachChild(function(child){
child.ui.toggleCheck(true);
});
}
if (!node.getUI().isChecked())
{
node.expand();
node.eachChild(function(child) {
child.ui.toggleCheck(false);
});
}
}
},
loader:
{
filter: function(record)
{
var myarr = new Array();
myarr[0] = record.get("layer").name.indexOf("Neighbourhood Label");
myarr[1] = record.get("layer").name.indexOf("Overview Streets");
myarr[2] = record.get("layer").name.indexOf("Quadrant Streets");
myarr[3] = record.get("layer").name.indexOf("Neighbourhood Streets");
myarr[4] = record.get("layer").name.indexOf("Block Streets");
myarr[5] = record.get("layer").name.indexOf("Proposed Streets");
myarr[6] = record.get("layer").name.indexOf("Address Label (Overview)");
myarr[7] = record.get("layer").name.indexOf("Address Label (Detail)");
if(myarr[0]==-1 && myarr[1]==-1 && myarr[2]==-1 && myarr[3]==-1 && myarr[4]==-1 && myarr[5]==-1 && myarr[6]==-1 && myarr[7]==-1)
{
return false;
}
else
{
return true;
}
}
}
});


So I have created my treepanel / treenode for the legend and I have created many layerlists like the one above. Then I have my procedure to switch themes like so -



//Remove all layers
for (var k = map.layers.length - 1; k >= 0; k--) {
map.removeLayer(map.layers[k]);
}

//add Base Layers to map
map.addLayers([all layers would go here]);

//Create Legend based on theme name
function createLegend(themeName)
{
switch(themeName)
{
case 'Base Map':
//Reload Treepanel Legend Items
layerRoot_Legend.beginUpdate();
//Uncheck all checked items except some layers that default to checked

//Loop through each Group of Layers
for (var i=0; i<=(layerRoot_Legend.childNodes.length -1); i++)
{
//Check for base layer, if so do this differently
if (layerRoot_Legend.childNodes[i].text == 'Base')
{
//Reset Base Map group layers

layerRoot_Legend.childNodes[i].ui.toggleCheck(false);
layerRoot_Legend.childNodes[i].childNodes[0].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[1].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[2].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[3].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[4].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[5].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[6].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[8].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[9].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[10].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[11].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[12].ui.toggleCheck(true);
layerRoot_Legend.childNodes[i].childNodes[13].ui.toggleCheck(true);
}
// not base legend group - check if entire group is checked, uncheck all
else if (layerRoot_Legend.childNodes[i].attributes.checked == true)
{
//uncheck all child nodes in the group
layerRoot_Legend.childNodes[i].ui.toggleCheck(false);
}
else
{
//expand Layer Group (like all Labels)
layerRoot_Legend.childNodes[i].expand();

var counter, counter2;
counter = layerRoot_Legend.childNodes[i].childNodes.length;
counter2 = (counter -1)

while (counter > 0)
{
if (layerRoot_Legend.childNodes[i].childNodes[counter2].attributes.checked == true)
{
layerRoot_Legend.childNodes[i].childNodes[counter2].ui.toggleCheck(false);
}
counter = (counter -1);
counter2 = (counter2 -1);
}

}
} // end for loop through each layer group

//Recreate layerlists
layerList_Labels.collapse();
layerList_AOI.collapse();
layerList_Trails.collapse();
layerList_Footprints.collapse();
layerList_LegalFabric.collapse();
layerList_ParcelFabric.collapse();
layerList_Base.collapse();
layerList_Orthos.collapse();
layerList_Google.collapse();
layerRoot_Legend.removeAll();
layerRoot_Legend.appendChild(layerList_Labels);
layerRoot_Legend.appendChild(layerList_AOI);
layerRoot_Legend.appendChild(layerList_Trails);
layerRoot_Legend.appendChild(layerList_Footprints);
layerRoot_Legend.appendChild(layerList_LegalFabric);
layerRoot_Legend.appendChild(layerList_ParcelFabric);
layerRoot_Legend.appendChild(layerList_Base);
layerRoot_Legend.appendChild(layerList_Orthos);
layerRoot_Legend.appendChild(layerList_Google);

layerRoot_Legend.childNodes[4].childNodes[0].ui.toggleCheck(true);
layerRoot_Legend.childNodes[5].childNodes[0].ui.toggleCheck(true);
layerRoot_Legend.childNodes[5].childNodes[1].ui.toggleCheck(true);
layerRoot_Legend.childNodes[5].childNodes[2].ui.toggleCheck(true);

layerRoot_Legend.endUpdate();
break;

Planet_x
15 Nov 2010, 9:54 AM
So I have experimented with this and got it somewhat working but now I realize there is a huge speed issue when recreating my legends if there is a lot of layers on my map.. obviously going through each treenode and expanding/unchecking each child is a slow method, isn't there any other method of reloading a treepanel that would be faster I can use?