/* * Class - TTreeNode */ var TTreeNode = Class.extend({}, { /* * Method - initialize */ initialize: function() { this.id = null; this.attributes = new Array(); this.nodePointer = null; this.subNodes = new Array(); this.subNodesCount = 0; this.parentId = null; this.isLastLeaf = false; }, /* * Method - addNode */ addNode: function( nodeObj ) { if ( this.nodePointer ) this.nodePointer.isLastLeaf = false; this.subNodes[ nodeObj.id ] = nodeObj; nodeObj.zindex = ++this.subNodesCount; nodeObj.isLastLeaf = true; this.nodePointer = nodeObj; }, /* * Method - findNode */ findNode: function( id ) { if ( this.id == id ) { return this; } if ( this.subNodes[id] ) { return this.subNodes[id]; } for ( i in this.subNodes ) { temp = this.subNodes[i].findNode( id ); if ( temp != null ) return temp; } return null; } }); /* * Class - TTree */ var TTree = Class.extend(TTreeNode, { /* * Method - initialize */ initialize: function( name ) { $C(TTreeNode).initialize.call( this ); } }); /* * Class - TAbstractTreeMenu */ var TAbstractTreeMenu = Class.extend({}, { /* * Method - initialize */ initialize: function( name ) { this.name = name; this.tree = new TTree(); this.value = null; this.pendingNodes = new Array(); this.pendingNodesOpen = true; }, /* * Method - setRoot */ setRoot: function( id, text ) { this.tree = new TTree(); this.tree.id = id; this.tree.attributes["text"] = text; this.tree.attributes["level"] = 0; }, /* * Method - addMenuItem */ addMenuItem: function( parentId, attributes ) { parentNode = this.tree.findNode( parentId ); if ( parentNode ) { node = new TTreeNode(); node.id = attributes["id"]; node.parentId = parentId; for ( key in attributes ) node.attributes[key] = attributes[key]; node.attributes["level"] = parentNode.attributes["level"] + 1; parentNode.addNode( node ); return true; } else if ( this.pendingNodesOpen ) { this.pendingNodes[this.pendingNodes.length] = {0:parentId, 1:attributes}; } return false; }, /* * Method - processPendingNodes */ processPendingNodes: function() { this.pendingNodesOpen = false; if (this.pendingNodes.length == 0) return; var temp = new Array(); var counter = 0; for ( i in this.pendingNodes ) { if ( this.addMenuItem(this.pendingNodes[i][0],this.pendingNodes[i][1]) ) { counter++; } else { temp[temp.length] = this.pendingNodes[i]; } } this.pendingNodes = temp; if ( counter > 0 ) { return counter + this.processPendingNodes(); } return counter; }, /* * Method - showPendingNodes */ showPendingNodes: function() { var str = ""; for ( i in this.pendingNodes ) { str += this.pendingNodes[i][0] + "-" + this.pendingNodes[i][1]["id"] + "-" + this.pendingNodes[i][1]["text"] + "\n"; } alert(str); }, /* * Method - clearItems */ clearItems: function() { this.tree = new TTree(); this.pendingNodes = new Array(); }, /* * Method - setValue */ setValue: function( value ) { this.value = value; } });