/* DynAPI Distribution List Class The DynAPI Distribution is distributed under the terms of the GNU LGPL license. Requirements: dynapi.api [dynlayer, dyndocument, nodeitem, events] dynapi.gui [NodeItem] */ function Tree(showLines,defaultOpen){ this.DynLayer=DynLayer; this.DynLayer(); this.multiMode = false; this._level=0; this.lastItem = false; this.items = []; this.root_name="root"; this.listener={ onselect : function(e){ var o=e.getSource().parent;o.select(e.getSource()); }, ondeselect : function(e){ var o=e.getSource().parent;o.deselect(e.getSource()); } }; this.onCreate(this.PreCreate); this.defaultOpen=defaultOpen; /*default style*/ this.treeStyle = {}; this.treeStyle.icons = {}; this.treeStyle.icons.expand=dynapi.library.path+'gui/images/tvw_expand.gif'; this.treeStyle.icons.collapse=dynapi.library.path+'gui/images/tvw_collapse.gif'; this.treeStyle.icons.open=dynapi.library.path+'gui/images/tvw_open.gif'; this.treeStyle.icons.openLast=dynapi.library.path+'gui/images/tvw_openlast.gif'; this.treeStyle.icons.close=dynapi.library.path+'gui/images/tvw_close.gif'; this.treeStyle.icons.closeLast=dynapi.library.path+'gui/images/tvw_closelast.gif'; this.treeStyle.icons.noChildren=dynapi.library.path+'gui/images/tvw_nochildren.gif'; this.treeStyle.icons.noChildrenLast=dynapi.library.path+'gui/images/tvw_nochildrenlast.gif'; this.treeStyle.icons.line=dynapi.library.path+'gui/images/tvw_line.gif'; this.treeStyle.icons.empty=dynapi.library.path+'gui/images/tvw_white.gif'; this.icons = {}; this.icons.normal_item=dynapi.library.path+'gui/images/tvw_foldclose.gif'; this.icons.selected_item=dynapi.library.path+'gui/images/tvw_foldopen.gif'; this.icons.dir_closed=dynapi.library.path+'gui/images/tvw_foldclose.gif'; this.icons.dir_opened=dynapi.library.path+'gui/images/tvw_foldopen.gif'; this.icons.root=dynapi.library.path+='gui/images/tvw_drive.gif'; this.items[this.root_name]=[]; this.items[this.root_name].haveChild=false; this.items[this.root_name].lastItem=false; this.items[this.root_name].id=0; this.level_icons=[]; this.showLines = showLines; this.treeStyle.indent = 16; this.treeStyle.borders = 1; this.treeStyle.spacing = 1; this.treeStyle.padding = 4; this.treeStyle.bos = false; this.treeStyle.ios = false; this.treeStyle.bg = 'white'; this.treeStyle.bgRoll = 'silver'; this.treeStyle.bgSelect = 'gray'; this.totalHeight = this.treeStyle.borders; this.itemListener = false; this.deleteTree=[]; this.itemsAutoWidth=true; this.itemsAutoHeight=true; this.autoWidth=false; this.autoHeight=true; this.pWidth=true; this.pHeight=true; } var p = dynapi.setPrototype('Tree','DynLayer'); p.PreCreate=function() { if(this._created) { this.arrangeItems(); } } p.addItemEventListener = function (listener) { this.itemListener=listener; for(i in this.items) { if(i!=this.root_name) { this.items[i]["node"].addEventListener(this.itemListener); } } } p.add = function(parent,id,content,value){ //parent,id,content,value,[icon],[selected icon],[selected content] var ts = this.treeStyle; var i = new NodeItem(content,value) if(arguments.length>6) { selectedContent=arguments[6]; }else { selectedContent=content; if (ts.bos) selectedContent=""+selectedContent+""; if (ts.ios) selectedContent=""+selectedContent+""; } i.setSelectedContent(selectedContent); i.setColors(ts.bg,ts.bgRoll,ts.bgSelect); i.addEventListener(this.listener); if(this.itemListener!=false) i.addEventListener(this.itemListener); i.setAutoResize(this.itemsAutoWidth,this.itemsAutoHeight); var treeObj=new Object(); treeObj.opened = this.defaultOpen; treeObj.parent = parent; treeObj.node = i; treeObj.id = id; icon = new DynLayer(); treeObj.icon = icon; selecticon = new DynLayer(); treeObj.selecticon = selecticon; treeObj.normal_icon=this.icons.normal_item; treeObj.selected_icon=this.icons.selected_item; if(arguments.length>4) if(arguments[4]!=false){ treeObj.normal_icon=arguments[4]; treeObj.selected_icon=arguments[4]; } if(arguments.length>5) if(arguments[5]!=false) treeObj.selected_icon=arguments[5]; treeObj.haveChild = false; treeObj.lastItem = false; this.items[id] = treeObj; this.items[parent].haveChild=true; this.items[parent].lastItem = id; this.addChild(i); this.addChild(icon); this.addChild(selecticon); if(dynapi.loaded) this.arrangeItems(); }; p.arrangeItems = function() { for(i in this.items) { if(i!=this.root_name) { this.items[i].node.setVisible(false); this.items[i].icon.setVisible(false); this.items[i].selecticon.setVisible(false); } } this.totalHeight = this.treeStyle.borders; this._recursiveItems(this.root_name); this.setHeight(this.totalHeight-this.treeStyle.spacing+this.treeStyle.borders); //this.pack(); } p._setSelectIcon = function(id,icon) { if(id!=this.root_name) { this.items[id].selecticon.setHTML(''); } } p._recursiveItems = function(parent) { this._level+=1; for(i in this.items) { if(this.items[i].parent==parent) { var item=this.items[i]; var IconContent = ''; var PreIconContent=''; var is_last=(this.items[parent].lastItem==i)? true:false; if(item.haveChild) { if(this.showLines) { open_icon=(is_last)?this.treeStyle.icons.openLast:this.treeStyle.icons.open; close_icon=(is_last)?this.treeStyle.icons.closeLast:this.treeStyle.icons.close; if(item.opened) IconContent+=''; else IconContent+=''; }else { if(item.opened) IconContent+=''; else IconContent+=''; } }else { nochildren_icon=(is_last)?this.treeStyle.icons.noChildrenLast:this.treeStyle.icons.noChildren; if(this.showLines) IconContent+=''; else IconContent+=''; } if(item.haveChild) { if(item.opened) SIconContent=this.icons.dir_opened; else SIconContent=this.icons.dir_closed; }else { SIconContent=item.normal_icon; } for(var k=0;k'; else this.level_icons[this.level_icons.length]=''; // this.level_icons[this._level]=; this._recursiveItems(i); this.level_icons.pop(); } } } this._level-=1; } p.collapse = function(id) { this.items[id].opened=false; this.arrangeItems(); } p.expand = function(id) { this.items[id].opened=true; this.arrangeItems(); } p._removeTree = function(parent) { this.deleteTree[this.deleteTree.length]=parent; if(this.items[parent].haveChild) { for(i in this.items) { if((i!=this.root_name) && (this.items[i].parent==parent)) this._removeTree(i); } } //alert(this.items[this.items[parent].parent].lastItem); /* alert(this.items[parent].parent+"=>"+this.items[this.items[parent].parent].lastItem); */ } p.removeSelected=function() { del=this.getSelectedIndexes(); if(del.length>0) { for(i in del) { this._removeItem(del[i]); } } } p.remove = function(item){ var i = this.getIndexOf(item); if ((i==-1) || (i==this.root_name)) return; this._removeItem(i); } p._removeItem = function(i){ this.deleteTree=[]; this._removeTree(i); parent=this.items[i].parent; for(var j=0;j