FTree Example
Flash MX, ActionScript 1.0
The following is an example of how to bind Xml data to the Flash
FTree component. It allows n levels of folders, stores Xml node
data with the FTree nodes and makes it available on selection.
It also assigns double-click open/close action to tree nodes.
It saves the tree state in a Shared Object on the client and restores
it whenever the tree is loaded, e.g. at a later visit to the same
page.
Note : There is also an example
available of how to bind XML to the Flash MX 2004 Tree component
using the BGX Serialization Classes
Links
Download sample files
View the Xml used
for this example
The Code
var nodeDescription = "";
var dTitle = "Loading...";
loadButton._visible = false;
var fileInfo = "";
with (iconClip) {
gotoAndStop(1);
}
var lastClick = 0;
var lastSelected;
var dClick = false;
bg_so = SharedObject.getLocal("bgTree");
var t = _level0.bgTree;
var rootNode;
t.setNodeExpansionHandler("saveBranches", _root);
t.setChangeHandler("bgTreeCall", _root);
var cD;
treeXML = new XML();
treeXML.onLoad = buildTree;
treeXML.load("explorer.xml");
function buildTree() {
getRoot();
parseTreeNode(rootNode,"0");
setBranches();
dTitle = "Description";
nodeDescription = descDefault;
}
function parseTreeNode(node,level) {
var parentTreeNode;
thisObject = new NodeData(node);
if(t.getTreeLength() == 0 ) {
t.setRootNode(new FTreeNode(thisObject.label, thisObject));
} else {
parentTreeNode = t.getRootNode();
var levelArray = level.split(".");
if (levelArray.length > 2) {
for (var i=1; i < levelArray.length -1; i++) {
var pChildren = parentTreeNode.getChildNodes();
parentTreeNode = pChildren[levelArray[i]];
}
}
t.addNode(parentTreeNode, new FTreeNode(thisObject.label, thisObject));
}
if(node.hasChildNodes()) {
var childArray = node.childNodes;
var childNode = node.firstChild;
for (var z = 0; z < childArray.length; z++) {
var childLevel = level + "." + z;
parseTreeNode(childNode,childLevel);
childNode = childNode.nextSibling;
}
}
}
function getRoot() {
rootNode = treeXML.firstChild;
rootName = rootNode.nodeName;
var x = 0;
var y = treeXML.childNodes;
while (x < y.length) {
if (rootName == "myRoot") {
break;
}
rootNode = rootNode.nextSibling;
rootName = rootNode.nodeName;
x++;
}
}
function NodeData(sNode) {
this.type = sNode.nodeName;
this.label = sNode.attributes["label"];
this.description = sNode.attributes["description"];
if (this.type == "leaf") {
this.sampleUrl = sNode.attributes["URL"];
this.fileinfo = sNode.attributes["fileInf"];
this.icon = sNode.attributes["icon"];
this.target = sNode.attributes["target"];
}
this.hasUrl = ((this.type == "leaf") && (this.sampleUrl.length > 0));
}
NodeData.prototype.loadSample = function() {
getURL(this.sampleUrl,this.target);
}
NodeData.prototype.update = function() {
if (dClick) {
if (this.hasURL) this.loadSample();
return;
}
loadButton._visible = this.hasURL;
nodeDescription = this.description;
fileInfo = this.fileinfo;
with (iconClip) {
gotoAndStop((this.icon.length>0) ? this.icon : 1);
}
}
function bgTreeCall(component) {
var s = component.getSelectedNode();
checkDblClick(s);
if (s.isBranch() && dClick) {
toggleOpen = ((s.isOpen()) ? false : true);
s.setIsOpen(toggleOpen);
t.refresh();
saveBranches();
} else {
if (!dClick) cD = s.getData();
cD.update();
}
}
function setBranches() {
fL = this.bg_so.data.folderList;
rO = this.bg_so.data.rootOpen;
if (fL == undefined) {
t.getRootNode().setIsOpen(true);
} else {
t.getRootNode().setIsOpen(rO);
var bArray = t.getRootNode().getChildNodes();
var i = 0;
setBranchesLoop(bArray,fL,i);
}
t.refresh();
}
function setBranchesLoop(bArray,fL,i) {
for (var j=0; j < bArray.length; j++) {
if (bArray[j].isBranch()) {
bArray[j].setIsOpen(fl[i]);
i++;
var thisChildren = bArray[j].getChildNodes();
i = setBranchesLoop(thisChildren,fL,i);
}
}
return i;
}
function saveBranches() {
rOpen = t.getRootNode().isOpen();
var bOpen = new Array();
var bArray = t.getRootNode().getChildNodes();
saveBranchesLoop(bOpen,bArray);
this.bg_so.data.folderList = bOpen;
this.bg_so.data.rootOpen = rOpen;
}
function saveBranchesLoop(bOpen,bArray){
for (var j=0; j < bArray.length; j++) {
if (bArray[j].isBranch()) {
bOpen.push(bArray[j].isOpen());
var thisChildren = bArray[j].getChildNodes();
saveBranchesLoop(bOpen,thisChildren);
}
}
}
function checkDblClick(s) {
var clickTime = getTimer();
dClick = ((clickTime-lastClick<300) && (lastSelected == s));
lastClick = clickTime;
lastSelected = s;
}
function loadButtonCall() {
cD.loadSample();
}