javascript中tree的实现

实现tree的代码如下:function showTree(treeid) { if(treeid == 0 ) return false; top.frameColumn.location.href = ""+ treeid +".html"; } function init() { t = new Bs_Tree(); t.imageDir = './images/icon/Help/'; t.draggable = true; t.initByArray(a); t.drawInto('treeDiv1'); t.getElement(1).open();//打开结点 t.setActiveElement(t.getElement(1)); setTimeout("showTree(t.getElement(1).treeID);", 100); }function Bs_TreeElement(){ this.id;

 // bluemaple add this.treeID; //记录数据库中的id

 this.parent; this._tree; this.caption; this.title;// Created by yangxl 2004-6-15 this.url; this.target; this.linkStyle; this.divStyle; this.onClick;

 this.isOncontextmenu = true; //是否打开右键控制,默认为true

 this.isOpen = false; this.visible = true; this.isChecked = 0; this.checkboxName; this.radioButtonSelected; this._checkboxObject; this._level = 0; this._children = new Array; this._undoneChildren; this.imageDir; this.imageHeight; this.icon; this.tag;// Created by yangxl 2004-6-15 this.beforeIconSpan; this.beforeCaptionSpan; this.afterCaptionSpan; this.dataContainer; this._attachedEvents; this._isOutrendered = false; this._errorArray;

 this.getThis = function() {  return this; }

 this.addChild = function(treeElement) {  treeElement.parent = this;  if (typeof(this._children) != 'object')    this._children = new Array;

  if (this._children.push)  {   this._children.push(treeElement);  }  else  {   this._children[this._children.length] = treeElement;  }

  treeElement._level = this._level +1;  this._updateLevelAndParent(treeElement);  this._tree._clearingHouse[treeElement.id] = treeElement;  if (this._isOutrendered)  {   this.render(true, true);  } }

 this.addChildByArray = function(elementData) {  var treeElement = this._tree._createTreeElement(elementData);  this.addChild(treeElement);  return treeElement; }

 this.isChild = function(elementId, bubble) {  for (var i=0; i<this._children.length; i++)  {   if (this._children[i].id == elementId)    return true;   if (bubble)   {    if (this._children[i].isChild(elementId, true))     return true;   }  }  return false; }

 this.setCaption = function(caption) {  this.caption = caption;  if (this._isOutrendered)  {   var span = document.getElementById(this._tree._objectId + '_e_' + this.id + '_caption2');   if (span) span.innerHTML = caption;  } }

 this.render = function(omitDivTags, putIntoPage, lookAhead) {  if (typeof(this._tree.stopWatch) == 'object')   this._tree.stopWatch.takeTime('Bs_TreeElement.render() for id: ' + this.id + ' in level: ' + this._level);

  if (typeof(lookAhead) == 'undefined')  {   lookAhead = this._tree.lookAhead;  }

  if ((this._tree._pseudoElement == this) && !this._tree.showPseudoElement && (lookAhead != -1))  {   lookAhead++;  }

  var imageDir    = this._getVar('imageDir');  var imageHeight = this._getVar('imageHeight');  var out      = new Array();  var outI     = 0;  var evalStr  = '';  if (!omitDivTags)  {   out[outI++] = '<span >';   var level = this._level;   if (!this._tree.showPseudoElement)    --level;   var obj = null;   var outTemp = '';   for (var i=0; i<level; i++)   {    if (!obj)    {     obj = this.parent;    }    else    {     obj = obj.parent;    }

    if (obj.hasSiblingsDown())    {     var img = 'line1';    }    else    {     var img = 'empty';    }    // 2004-6-28 yangxl outTemp = '<img src="' + imageDir + img + '.gif" height="' + imageHeight + '" border="0" align="top">' + outTemp;    outTemp = '<img src="' + imageDir + img + '.gif" border="0" align="top">' + outTemp;   }   out[outI++] = outTemp;   if (this.hasSiblingsDown())   {    var imgNumber = 3;   }   else   {    var imgNumber = 2;   }   if (this.hasVisibleChildren())   {    if ((this._level == 0) || (!this._tree.showPseudoElement && (this._level == 1) && ((this._tree.useAutoSequence && (this.id == 1)) || (!this._tree.useAutoSequence && true))))    {     if (this.hasSiblingsDown())     {      imgNumber++;     }     else     {      imgNumber--;     }    }    if (this.isOpen)    {     var plusImg = 'minus' + imgNumber;     var onClick = 'Close';    }    else    {     var plusImg = 'plus' + imgNumber;var onClick = 'Open';    }   }   else   {    var plusImg = 'line' + imgNumber;    var onClick = false;   }

   if (onClick)   {    var onClickStr = 'onClick="Bs_Objects['+this._tree._id+'].elementToggleOpenClose(\'' + this.id + '\');"';   }   else   {    var onClickStr = '';   }

   if (this.onClick)   {    var onClick = this.onClick;    onClick = onClick.replace(/__this\.id__/g, this.id); //replace the string __this.id__ with the actual id (int).    out[outI++] = '<span style="cursor:pointer; cursor:hand;" onClick="' + onClick + '">';   }   // 2004-6-28 yangxl  out[outI++] = '<img ';    }

    if (true)    {     out[outI++] = ' style="height:16px;"';    }

    if (this.radioButtonSelected)    {     out[outI++] = ' checked';    }    out[outI++] = '>';   }   if (this._tree.useCheckboxSystem)   {    var checkboxSpan = this.checkboxName + 'Span';    var checkboxObj  = this.checkboxName + 'Obj';    out[outI++] = '&nbsp;<span ';   }

   if(this.isOncontextmenu && !this.hasEventAttached('oncontextmenuCaption') )   {    out[outI++] = ' oncontextmenu="event.returnValue=false; Bs_Objects['+this._tree._id+'].getElement('+ this.id +').setActive();"';   }   else if(this.hasEventAttached('oncontextmenuCaption') )   {    out[outI++] = ' oncontextmenu="event.returnValue=false; Bs_Objects['+this._tree._id+'].getElement('+ this.id +').setActive();Bs_Objects['+this._tree._id+'].executeOnElement(\'' + this.id + '\', \'fireEvent\', Array(\'oncontextmenuCaption\'));"';   }

   out[outI++] = ' onClick="Bs_Objects['+this._tree._id+'].executeOnElement(\'' + this.id + '\', \'fireEvent\', Array(\'onClickCaption\'));">';

   if (hRef) out[outI++] = hRef;   out[outI++] = '<span ;  this._isOutrendered = true;  var content = new Array(out.join(''), evalStr);  if (putIntoPage)  {   var doc = document.getElementById(this._tree._objectId + '_e_' + this.id);if (doc != null)   {    doc.innerHTML = content[0];    if (content[1] != '')    {     eval(content[1]);    }    return true;   }   else    {    return false;   }  }  else  {   return content;  } }    this.reset = function() {  this.caption           = null;  this.url               = null;  this.target            = null;  this.onClick           = null;  this.isOpen            = false;  this.isChecked         = 0;  this.checkboxName      = null;  this.beforeIconSpan    = null;  this.beforeCaptionSpan = null;  this.afterCaptionSpan  = null; }

 this.initByArray = function(a, tree, level) {  this._tree   = tree;  this._level  = level;  if (typeof(this._tree.stopWatch) == 'object')   this._tree.stopWatch.takeTime('Bs_TreeElement.initByArray()');  if (this._tree.useAutoSequence && (level > 0))  {   this.id      = ++this._tree._elementSequence;  }  else  {   if (typeof(a['id']) == 'undefined')   {    this._addError('tree error: useAutoSequence is set to false, but for an array element there is no id defined.');    return false;   }   this.id = a['id'];  }

  if (typeof(a['caption'])             != 'undefined')   this.caption             = a['caption'];  if (typeof(a['title'])             != 'undefined') // Created by yangxl 2004-6-15   this.title             = a['title'];  if (typeof(a['url'])                 != 'undefined')   this.url                 = a['url'];  if (typeof(a['target'])              != 'undefined')   this.target              = a['target'];  if (typeof(a['onClick'])             != 'undefined')   this.onClick             = a['onClick'];  if (typeof(a['isOpen'])              != 'undefined')   this.isOpen              = a['isOpen'];  if (typeof(a['isChecked'])           != 'undefined')   this.isChecked           = parseInt(a['isChecked']);  if (typeof(a['visible'])             != 'undefined')   this.visible             = a['visible'];  if (typeof(a['icon'])                != 'undefined')   this.icon                = a['icon'];  if (typeof(a['imageDir'])            != 'undefined')   this.imageDir            = a['imageDir'];  if (typeof(a['beforeIconSpan'])      != 'undefined')   this.beforeIconSpan      = a['beforeIconSpan'];  if (typeof(a['beforeCaptionSpan'])   != 'undefined')   this.beforeCaptionSpan   = a['beforeCaptionSpan'];  if (typeof(a['afterCaptionSpan'])    != 'undefined')   this.afterCaptionSpan    = a['afterCaptionSpan'];  if (typeof(a['radioButtonSelected']) != 'undefined')   this.radioButtonSelected = a['radioButtonSelected'];  if (typeof(a['dataContainer'])       != 'undefined')   this.dataContainer       = a['dataContainer'];  if (typeof(a['tag'])       != 'undefined')// Created by yangxl 2004-6-15   this.tag       = a['tag'];  if (typeof(a['treeID'])       != 'undefined') // bluemaple add   this.treeID       = a['treeID'];

  if (typeof(a['checkboxName']) != 'undefined')  {   this.checkboxName  = a['checkboxName'];  }  else  {   if (this._tree.useCheckboxSystem)   {    this.checkboxName = 'bsTreeChk_' + this._tree._objectId + '_' + this.id;   }  }

  if (typeof(a['oncontextmenuCaption'])    != 'undefined')  {   this.attachEvent('oncontextmenuCaption', a['oncontextmenuCaption']);  }

  if (typeof(a['onClickCaption'])    != 'undefined')  {   this.attachEvent('onClickCaption', a['onClickCaption']);  }

  if (typeof(a['onChangeCheckbox'])    != 'undefined')  {   this.attachEvent('onChangeCheckbox', a['onChangeCheckbox']);  }

  if (typeof(a['events']) != 'undefined')  {   for (ev in a['events'])   {    this.attachEvent(ev, a['events'][ev]);   }  }

  return true; }

 this.exportAsArray = function(withChildren) {  var ret = new Array();  if (typeof(this.id)                       != 'undefined')   ret['id']                       = this.id;  if (typeof(this.caption)                  != 'undefined')   ret['caption']                  = this.caption;  if (typeof(this.title)                  != 'undefined')// Created by yangxl 2004-6-15   ret['title']                  = this.title;  if (typeof(this.url)                      != 'undefined')   ret['url']                      = this.url;  if (typeof(this.target)                   != 'undefined')   ret['target']                   = this.target;  if (typeof(this.onClick)                  != 'undefined')   ret['onClick']                  = this.onClick;  if (typeof(this.isOpen)                   != 'undefined')   ret['isOpen']                   = this.isOpen;  if (typeof(this.isChecked)                != 'undefined')   ret['isChecked']                = this.isChecked;  if (typeof(this.visible)                  != 'undefined')   ret['visible']                  = this.visible;  if (typeof(this.icon)                     != 'undefined')   ret['icon']                     = this.icon;  if (typeof(this.imageDir)                 != 'undefined')   ret['imageDir']                 = this.imageDir;  if (typeof(this.beforeIconSpan)           != 'undefined')   ret['beforeIconSpan']           = this.beforeIconSpan;  if (typeof(this.afterCaptionSpan)         != 'undefined')   ret['afterCaptionSpan']         = this.afterCaptionSpan;  if (typeof(this.radioButtonSelected)      != 'undefined')   ret['radioButtonSelected']      = this.radioButtonSelected;  if (typeof(this.dataContainer)            != 'undefined')   ret['dataContainer']            = this.dataContainer;  if (typeof(this.checkboxName)             != 'undefined')   ret['checkboxName']             = this.checkboxName;  if (typeof(this.beforeCaptionSpan)        != 'undefined')   ret['beforeCaptionSpan']        = this.beforeCaptionSpan;  if (typeof(this.tag)        != 'undefined')// Created by yangxl 2004-6-15   ret['tag']        = this.tag;  if (typeof(this.treeID)        != 'undefined')// Created by fangj   ret['treeID']        = this.treeID;

  /* start modi by fangj */

  if (typeof(this._attachedEvents['onClickCaption'])        != 'undefined')// Created by fangj   ret['onClickCaption']        = this._attachedEvents['onClickCaption'];

  if (typeof(this._attachedEvents['oncontextmenuCaption'])        != 'undefined')// Created by fangj   ret['oncontextmenuCaption']        = this._attachedEvents['oncontextmenuCaption'];

  /* end modi by fangj */

  if (withChildren)   {   ret['children'] = new Array();   for (var i=0; i<this._children.length; i++)   {    ret['children'][ret['children'].length] = this._children[i].exportAsArray(true);   }  }  return ret; }

 this.updateObjectByArray = function(a) {  this.reset();  if (a['caption'])            this.caption            = a['caption'];  if (a['title'])            this.title            = a['title'];// Created by yangxl 2004-6-15  if (a['url'])                this.url                = a['url'];  if (a['target'])             this.target             = a['target'];  if (a['onClick'])            this.onClick            = a['onClick'];  if (a['isOpen'])             this.isOpen             = a['isOpen'];  if (a['isChecked'])          this.isChecked          = a['isChecked'];  if (a['imageDir'])           this.imageDir           = a['imageDir'];  if (a['checkboxName'])  {   this.checkboxName  = a['checkboxName'];  }  else   {   if (this._tree.useCheckboxSystem)   {    this.checkboxName = 'bsTreeCheckbox' + this.id;   }  }  if (a['beforeIconSpan'])     this.beforeIconSpan     = a['beforeIconSpan'];  if (a['beforeCaptionSpan'])  this.beforeCaptionSpan  = a['beforeCaptionSpan'];  if (a['afterCaptionSpan'])   this.afterCaptionSpan   = a['afterCaptionSpan'];  if (a['tag'])   this.afterCaptionSpan   = a['tag'];// Created by yangxl 2004-6-15 }

 this.getJavascriptCode = function(varName, recursive) {  var ret = "";  if(   (this._tree.useAutoSequence && (this.id > 1))   || (!this._tree.useAutoSequence && !this.parent)   )  {}  else  {   ret += varName + " = new Array();\n";   if (!this._tree.useAutoSequence)   {    ret += varName + "['id'] = \"" + this.id + "\";\n";   }   if (this.caption)           ret += varName + "['caption']            = \"" + this.caption            + "\";\n";if (this.url)               ret += varName + "['url']                = \"" + this.url                + "\";\n";if (this.target)            ret += varName + "['target']             = \"" + this.target             + "\";\n";if (this.onClick) {   //var onClick = this.onClick.replace(/'/g,  "\'");   var onClick = this.onClick.replace(/"/g,  '\"');   ret += varName + "['onClick']            = \"" + onClick            + "\";\n";  }  if (this.imageDir)          ret += varName + "['imageDir']           = \"" + this.imageDir          + "\";\n";if (this.isOpen)            ret += varName + "['isOpen']             = '" + this.isOpen             + "';\n";if (this.isChecked)         ret += varName + "['isChecked']          = '" + this.isChecked          + "';\n";if (this.checkboxName)      ret += varName + "['checkboxName']       = '" + this.checkboxName       + "';\n";if (this.icon)              ret += varName + "['icon']               = \"" + this.icon              + "\";\n";if (this.beforeIconSpan)    ret += varName + "['beforeIconSpan']     = \"" + this.beforeIconSpan     + "\";\n";if (this.beforeCaptionSpan) ret += varName + "['beforeCaptionSpan']  = \"" + this.beforeCaptionSpan  + "\";\n";if (this.afterCaptionSpan)  ret += varName + "['afterCaptionSpan']   = \"" + this.afterCaptionSpan   + "\";\n";varName += "['children']";}  if (recursive)  {   if (this._children.length > 0)    {    ret += varName + " = new Array();\n";    for (var i=0; i<this._children.length; i++)    {     ret += this._children[i].getJavascriptCode(varName + "[" + i + "]", recursive);    }   }  }  return ret; }

 this.setActive = function() {  var activeElement = this._tree.getActiveElement();  if (activeElement != false)  {   activeElement.unsetActive();  }  this._tree.setActiveElement(this);  this._highlight(); }

 this._highlight = function() {  var elmSetActive = document.getElementById(this._tree._objectId + '_e_' + this.id + '_caption');  var elmSetActive2 = document.getElementById(this._tree._objectId + '_e_' + this.id + '_caption2');  var elmSetActive3 = document.getElementById(this._tree._objectId + '_e_' + this.id + '_folder');//bluemaple modi  if (elmSetActive != null)  {   elmSetActive.style.backgroundColor = this._getVar('captionBgColor');   elmSetActive2.style.color = this._getVar('captionForeColor');

   if(this._getVar('icon') != "false")    if( elmSetActive3.src.indexOf(this._getVar('imageDir').substring(this._getVar('imageDir').indexOf("/"), this._getVar('imageDir').length )+"leaf.gif") >=0 )     elmSetActive3.src = this._getVar('imageDir') + "leaf1.gif";  }  else  {   setTimeout("Bs_Objects["+this._tree._id+"].executeOnElement('" + this.id + "', '_highlight');", 100);  } }

 // bluemaple add this.highlight = function() {  var elmSetActive = document.getElementById(this._tree._objectId + '_e_' + this.id + '_caption');  var elmSetActive2 = document.getElementById(this._tree._objectId + '_e_' + this.id + '_caption2');  var elmSetActive3 = document.getElementById(this._tree._objectId + '_e_' + this.id + '_folder');//bluemaple modi  if (elmSetActive != null)  {   elmSetActive.style.backgroundColor = this._getVar('captionBgColor');   elmSetActive2.style.color = this._getVar('captionForeColor');

   if(this._getVar('icon') != "false")    if( elmSetActive3.src.indexOf(this._getVar('imageDir').substring(this._getVar('imageDir').indexOf("/"), this._getVar('imageDir').length )+"leaf.gif") >=0 )     elmSetActive3.src = this._getVar('imageDir') + "leaf1.gif";  }  else  {   setTimeout("Bs_Objects["+this._tree._id+"].executeOnElement('" + this.id + "', '_highlight');", 100);  } }

 this.unsetActive = function() {  var e = document.getElementById(this._tree._objectId + '_e_' + this.id + '_caption');  var e2 = document.getElementById(this._tree._objectId + '_e_' + this.id + '_caption2');

  var e3 = document.getElementById(this._tree._objectId + '_e_' + this.id + '_folder');//bluemaple modi

  if (e != null) e.style.backgroundColor = 'transparent';  if (e2 != null) e2.style.color = 'black';

  if(e3 != null)  {   if( e3.src.indexOf(this._getVar('imageDir').substring( this._getVar('imageDir').indexOf("/"), this._getVar('imageDir').length ) + "leaf1.gif") >=0 )    e3.src = this._getVar('imageDir') + "leaf.gif";  } }

 this.toggleOpenClose = function() {  if (this.isOpen)  {   if (this.hasEventAttached('onBeforeClose'))   {    var status = this.fireEvent('onBeforeClose');    if (status != true) return;   }   this.close();   if (this.hasEventAttached('onAfterClose')) this.fireEvent('onAfterClose');  }  else  {   if (this.hasEventAttached('onBeforeOpen'))   {    var status = this.fireEvent('onBeforeOpen');    if (status != true) return;   }   this.open();   if (this.hasEventAttached('onAfterOpen')) this.fireEvent('onAfterOpen');  } }

 this.open = function(checkParents) {  if (this.isOpen) return;  this.isOpen = true;  if (true || !doRender)  {   if (this._isOutrendered)   {    var d = document.getElementById(this._tree._objectId + '_e_' + this.id + '_children');    d.style.display = 'block';    this._switchIconsOnToggleOpenClose();   }   else   {    if (checkParents)    {     this._renderParentsUp();    }    this.render(true, true);   }  }  if (this._tree.autoCollapse)  {   var sib = this.getSiblings();   for (var i=0; i<sib.length; i++)   {    if (sib[i].id != this.id)    {     sib[i].close();    }   }  }  if (this.hasVisibleChildren())  {   var lookAhead = this._tree.lookAhead;   var treeElm     = this;   for (var j=0; j<treeElm._children.length; j++)   {    if (typeof(treeElm._children[j]._undoneChildren) == 'object')    {     for (var k=0; k<treeElm._children[j]._undoneChildren.length; k++)     {      var newE = this._tree._createTreeElement(treeElm._children[j]._undoneChildren[k], treeElm._children[j]._level +1);      treeElm._children[j].addChild(newE);     }     treeElm._children[j]._undoneChildren = false;    }    if (treeElm._children[j].hasVisibleChildren())    {     var doRender = false;     for (var k=0; k<treeElm._children[j]._children.length; k++)     {      if (!treeElm._children[j]._children[k]._isOutrendered)      {       var doRender = true;break;      }     }     if (doRender)     {      treeElm._children[j].render(true, true, lookAhead);     }    }   }  } }

 this._renderParentsUp = function() {  if (typeof(this.parent) == 'undefined') this.parent._renderParentsUp();  if (this._isOutrendered) return;  this.render(true, true); }

 this.close = function() {  if (!this.isOpen) return;   this.isOpen = false;  if (this._isOutrendered)  {   var d = document.getElementById(this._tree._objectId + '_e_' + this.id + '_children');   d.style.display = 'none';   this._switchIconsOnToggleOpenClose();  }  else  {   this.render(true, true);  } }

 this._switchIconsOnToggleOpenClose = function() {  var openClose = document.getElementById(this._tree._objectId + '_e_' + this.id + '_openClose');  openClose.src = this._getSourceOpenCloseIcon();  if (this._getVar('useFolderIcon'))  {   var folderIconId = this._tree._objectId + '_e_' + this.id + '_folder';   var fIcon = document.getElementById(folderIconId);   if (fIcon)   {    fIcon.src = this._getSourceFolderIcon();   }  } }

 this._getSourceOpenCloseIcon = function() {  if (this.hasSiblingsDown())  {   var imgNumber = 3;  }  else  {   var imgNumber = 2;  }  if (this.hasVisibleChildren())  {   if ((this._level == 0) || (!this._tree.showPseudoElement && (this._level == 1) && ((this._tree.useAutoSequence && (this.id == 1)) || (!this._tree.useAutoSequence && true))))   {    if (this.hasSiblingsDown())    {     imgNumber++;    }    else    {     imgNumber--;    }   }   if (this.isOpen)   {    var plusImg = 'minus' + imgNumber;    var onClick = 'Close';   }   else   {    var plusImg = 'plus' + imgNumber;    var onClick = 'Open';   }  }  else  {   var plusImg = 'line' + imgNumber;  }  var imageDir = this._getVar('imageDir');  return imageDir + plusImg + '.gif'; }

 this._getSourceFolderIcon = function() {  var imageDir = this._getVar('imageDir');  switch (typeof(this.icon))  {   case 'undefined':    if (this._tree.useLeaf && !this.hasChildren())    {     var folderImg = 'leaf';    }    else    {     var folderImg = 'folder';     folderImg += (this.isOpen) ? 'Open' : 'Closed';    }    return imageDir + folderImg + '.gif';   break;   case 'bool':   case 'boolean':   break;   case 'string':   if (this.icon != 'false')   {    var ret = '';    if (!this._iconHasPath(this.icon))     ret += imageDir;    ret += this.icon;    if (!this._iconHasExtension(this.icon))     ret += '.gif';    return ret;   }  }  return ''; }

 this.hasChildren = function() {  return (this._children.length > 0); }

 this.hasVisibleChildren = function() {  if (!this._children || !(this._children.length > 0)) return false;  for (var i=0; i<this._children.length; i++)  {   if (this._children[i].visible) return true;  }  return false; }

 this.numChildren = function() {  return this._children.length; }

 this.childPos = function(id) {  for (var i=0; i<this._children.length; i++)   {   if (this._children[i].id == id)    return ++i;  }  return false; }

 this.hasSiblings = function() { }

 this.hasSiblingsDown = function() {  try  {   var tot = this.parent.numChildren();   var pos = this.parent.childPos(this.id);   return (pos < tot);  }  catch (e)  {   return false;  } }

 this.hasSiblingsAbove = function() {}

 this.getSiblings = function() {  try  {   return this.parent.getChildren();  }  catch(e)  {   return new Array;  } }

 this.getChildren = function() {  return this._children; }

 this.getParentId = function() {  try  {   return this.parent.id;  }  catch (e)  {   return false;  } }

 this.hasParent = function() {  return (this.parent); }

 this.attachEvent = function(trigger, yourEvent) {  if (typeof(this._attachedEvents) == 'undefined')  {   this._attachedEvents = new Array();  }

  if (typeof(this._attachedEvents[trigger]) == 'undefined')  {   this._attachedEvents[trigger] = new Array(yourEvent);  }  else  {   this._attachedEvents[trigger][this._attachedEvents[trigger].length] = yourEvent;  } }

 this.hasEventAttached = function(trigger) {  return ((typeof(this._attachedEvents) != 'undefined') && (typeof(this._attachedEvents[trigger]) != 'undefined')); }

 this.fireEvent = function(trigger) {  var ret = true;  if (trigger == 'onClickCaption')  {   this.setActive();

   if ((typeof(this._attachedEvents) != 'undefined') && (typeof(this._attachedEvents[trigger]) != 'undefined'))    eval(''+ this._attachedEvents['onClickCaption'] +'');//add by fangj  }  else if ((typeof(this._attachedEvents) != 'undefined') && (typeof(this._attachedEvents[trigger]) != 'undefined'))  {   var e = this._attachedEvents[trigger];   if ((typeof(e) == 'string') || (typeof(e) == 'function'))   {    e = new Array(e);   }   for (var i=0; i<e.length; i++)   {    if (typeof(e[i]) == 'function')    {     var status = e[i](this);     if (status == false) ret = false;    }    else if (typeof(e[i]) == 'string')    {     var ev = e[i].replace(/__this\.id__/g, this.id); //replace the string __this.id__ with the actual id.     //ev = ev.replace(/__this__/g, 'this'); //replace the string __this__ with 'this'.     eval(ev);    }   }  }  return ret; }

 this._addError = function(str) {  if (typeof(this._errorArray) == 'undefined')  {   this._errorArray = new Array(str);  }  else  {   this._errorArray[this._errorArray.length] = str;  } }

 this.getLastError = function() {  if (typeof(this._errorArray) != 'undefined')  {   if (this._errorArray.length > 0)   {    return this._errorArray[this._errorArray.length -1];   }  }  return false; }

 this._getVar = function(varName) {  if (typeof(this[varName]) != 'undefined')  {   return this[varName];  }  else  {   if (this._tree.walkTree && (typeof(this.parent) != 'undefined'))   {    return this.parent._getVar(varName);   }   else if (typeof(this._tree[varName]) != 'undefined')   {    return this._tree[varName];   }   else   {    return null;   }  } }

 this.onMouseOver = function() {  var img = document.getElementById(this._spanId + 'icon');  if (!img.swapOver0)  {   img.swapOver0 = new Image();   img.swapOver0.src = this.imgDir + 'enabled_0_over.gif';   img.swapOver1 = new Image();   img.swapOver1.src = this.imgDir + 'enabled_1_over.gif';   img.swapOver2 = new Image();   img.swapOver2.src = this.imgDir + 'enabled_2_over.gif';   img.swapOut0 = new Image();   img.swapOut0.src = this.imgDir + 'enabled_0.gif';   img.swapOut1 = new Image();   img.swapOut1.src = this.imgDir + 'enabled_1.gif';   img.swapOut2 = new Image();   img.swapOut2.src = this.imgDir + 'enabled_2.gif';  }  img.src = img['swapOver' + this.value].src; }

 this.onMouseOut = function() {  var img = document.getElementById(this._spanId + 'icon');  img.src = img['swapOut' + this.value].src; }

 this.setCheckboxValue = function(value, fireEvents, doWalk) {  if (typeof(fireEvents) == 'undefined') fireEvents = true;  if (typeof(doWalk)     == 'undefined') doWalk     = true;  if (!this.hasChildren())  {   value = (value) ? 2 : 0;  }  else  {   if (this.isChecked == 0)   {    if (this._tree.checkboxSystemWalkTree && (this._tree.checkboxSystemWalkTree != 2) && (this._tree.checkboxSystemWalkTree != 3) && this.hasChildren())    {     value = 1;    }   }  }  this.isChecked = value;  this._checkboxObject.setTo(value, true);  if (fireEvents)  {   if (this.hasEventAttached('onChangeCheckbox')) this.fireEvent('onChangeCheckbox');  }  if (doWalk)  {   if ((this._tree.checkboxSystemWalkTree == 3) || (this._tree.checkboxSystemWalkTree == 1) || (this._tree.checkboxSystemWalkTree == 4))   {    this.parent.updateCheckboxFromChild();   }   if ((this._tree.checkboxSystemWalkTree == 3) || (this._tree.checkboxSystemWalkTree == 2) || ((this._tree.checkboxSystemWalkTree == 4) && (value == 0)))    {    this.checkboxUpdateDown(value);   }  } }

 this.checkboxEvent = function(value) {  if (!this.hasChildren())  {   value = (value) ? 2 : 0;  }  else  {   if (this.isChecked == 1)   {    if ((!this._tree.checkboxSystemIfPartlyThenFull) || ((this._tree.checkboxSystemWalkTree) && (this._tree.checkboxSystemWalkTree != 2) && (this._tree.checkboxSystemWalkTree != 3)))    {     value = 0;    }    else    {     value = 2;    }   }   else if (this.isChecked == 0)   {    if (this._tree.checkboxSystemWalkTree && (this._tree.checkboxSystemWalkTree != 2) && (this._tree.checkboxSystemWalkTree != 3) && this.hasChildren())    {     value = 1;    }   }  }  this.isChecked = value;  this._checkboxObject.setTo(value, true);  if (this.hasEventAttached('onChangeCheckbox'))   this.fireEvent('onChangeCheckbox');  if ((this._tree.checkboxSystemWalkTree == 3) || (this._tree.checkboxSystemWalkTree == 1) || (this._tree.checkboxSystemWalkTree == 4))  {   this.parent.updateCheckboxFromChild();  }  if ((this._tree.checkboxSystemWalkTree == 3) || (this._tree.checkboxSystemWalkTree == 2) || ((this._tree.checkboxSystemWalkTree == 4) && (value == 0)))  {   this.checkboxUpdateDown(value);  } }

 this.checkboxUpdateDown = function(value) {  for (var i=0; i<this._children.length; i++)  {   this._children[i]._updateCheckboxFromParent(value, true);  } }

 this.updateCheckboxVisually = function() {  if (typeof(this._checkboxObject) == 'object')  {   try   {    this._checkboxObject.setTo(this.isChecked);   }   catch (e)   {   }  } }

 this._updateCheckboxFromParent = function(newValue, recursiveDown) {  var backupValue = this.isChecked;  this.isChecked = (newValue) ? 2 : 0;  var hasChanged = (this.isChecked != backupValue);  if (hasChanged)  {   this.updateCheckboxVisually();   if (this.hasEventAttached('onChangeCheckbox'))    this.fireEvent('onChangeCheckbox');  }  if (recursiveDown) this.checkboxUpdateDown(newValue, true); }

 this.updateCheckboxFromChild = function() {  var backupIsChecked = this.isChecked;var numYes   = 0;var numNo    = 0;var isPartly = false;for (var i=0; i<this._children.length; i++)  {   if (this._children[i].isChecked == 1)   {    isPartly = true;    this.isChecked = 1;    break;   }   else if (this._children[i].isChecked)   {    numYes++;   }   else   {    numNo++;   }   if ((numYes > 0) && (numNo > 0))   {    break;   }  }

  if (!isPartly)  {   if ((numYes > 0) && (numNo > 0))   {    this.isChecked = 1;   }   else if (numYes > 0)   {    this.isChecked = 2;   }   else   {    this.isChecked = 0;   }  }  if (backupIsChecked != this.isChecked)  {   this.updateCheckboxVisually();   if (this.hasEventAttached('onChangeCheckbox'))    this.fireEvent('onChangeCheckbox');  }  if (typeof(this.parent) == 'object')  {   this.parent.updateCheckboxFromChild();  } }

 this._updateLevelAndParent = function(treeElement) {  if ((typeof(treeElement._children) == 'object') && (treeElement._children.length > 0))  {   for (var i=0; i<treeElement._children.length; i++)   {    treeElement._children[i].parent = treeElement;    treeElement._children[i]._level = treeElement._level +1;    this._updateLevelAndParent(treeElement._children[i]);   }  } }

 this._getLinkStyle = function() {  if (typeof(this.linkStyle)       != 'undefined') return this.linkStyle;  if (typeof(this._tree.linkStyle) != 'undefined') return this._tree.linkStyle;  return ''; }

 this._iconHasExtension = function(iconStr) {  var iconLower = iconStr.toLowerCase();  var iconPos   = iconLower.lastIndexOf('.');  if (iconPos > -1)  {   var iconExt = iconLower.substr(iconPos +1);   if ((iconExt != 'gif') && (iconExt != 'png') && (iconExt != 'jpg') && (iconExt != 'jpeg'))   {    return false;   }  }  else  {   return false;  }  return true; }

 this._iconHasPath = function(iconStr) {  if (iconStr.indexOf('://') > -1) return true;  if (iconStr.substr(0, 1) == '/') return true;  return false; }}if (!Bs_Objects){ var Bs_Objects = [];};

function Bs_Tree(){ this._id; this._objectId; this.autoCollapse      = false; this.lookAhead = 2; this.captionBgColor    = "highlight"; this.captionForeColor    = "highlighttext"; this.linkStyle; this.divStyle = 'font-family: Arial, Helvetica, sans-serif; font-size: 11px;'; this.showPseudoElement = false; this.useCheckboxSystem = false; this.checkboxSystemWalkTree = 3; this.checkboxSystemIfPartlyThenFull = true; this.checkboxSystemImgDir; this.checkboxSystemGuiNochange; this.useRadioButton = false; this.radioButtonName; this.imageDir = './images/icon/'; this.imageHeight = 16; this.useFolderIcon = true; this.useLeaf = true; this.walkTree = true; this.useAutoSequence = true; this.draggable = false; this._clearingHouse = new Array; this._pseudoElement; this._currentActiveElement; this._elementSequence = 0; this._errorArray; this.stopWatch; this.totalElements = 0;//add 2004-6-15

 this._constructor = function() {  this._id = Bs_Objects.length;  Bs_Objects[this._id] = this;  this._objectId = "Bs_Tree_"+this._id;  var a = [];  a['id']               = 'pseudoElement001';  a['caption']          = "root";  a['url']              = "";  a['target']           = "";  a['isOpen']           = true;  this._pseudoElement = this._createTreeElement(a, 0); }

 this.initByArray = function(arr) {  for (var i=0; i<arr.length; i++)  {   var e = this._createTreeElement(arr[i], 1);   if (e == false)   {    return false;   }   this._pseudoElement.addChild(e);  }  return true; }  this.getActiveElement = function() {  if (typeof(this._currentActiveElement) != 'undefined')   return this._currentActiveElement;

  return false; }

 this.setActiveElement = function(treeElement) {  this._currentActiveElement = treeElement;  this._currentActiveElement.highlight(); }

 this._createTreeElement = function(arr, level) {  ++this.totalElements; //add 2004-6-15  if (typeof(level) == 'undefined') level = 1;  var e = new Bs_TreeElement();  var status = e.initByArray(arr, this, level);  if (!status)  {   this._addError(e.getLastError());   return false;  }  this._clearingHouse[e.id] = e;

  if (arr['children'])  {   if ((this.useCheckboxSystem && (this.checkboxSystemWalkTree >= 2)) || e.isOpen || ((this.lookAhead +2) > level) || (this.lookAhead == -1) || ((typeof(e.parent) == 'object') && (e.parent.isOpen)))   {    for (var i=0; i<arr['children'].length; i++)    {     var newE = this._createTreeElement(arr['children'][i], level +1);     if (!newE) return false;     e.addChild(newE);    }   }   else   {    e._undoneChildren = arr['children'];   }  }  return e; }

 this.getElement = function(elementId) {  if (elementId == 0) return this._pseudoElement;  if (typeof(this._clearingHouse[elementId]) == 'object')  {   return this._clearingHouse[elementId];  }  else  {   return false;  } }

 this.removeElement = function(elementId) {  if (typeof(this._clearingHouse[elementId]) == 'undefined') return false;  var elm = this._clearingHouse[elementId];  if ((typeof(elm.parent) == 'object') && (typeof(elm.parent._children) == 'object'))  {   for (var i=0; i<elm.parent._children.length; i++)   {    if (elm.parent._children[i].id == elementId)    {     elm.parent._children.deleteItem(i);     break;    }   }  }  this._clearingHouse.deleteItemHash(elementId);  for (var i=0; i<elm._children.length; i++)  {   this._clearingHouse.deleteItemHash(elm._children[i].id);  }  if ((typeof(elm.parent) == 'object') && (elm.parent._isOutrendered))  {   elm.parent.render(true, true);  }  return true; }

 this.draw = function() {  var content = this._pseudoElement.render();  document.writeln(content[0]);eval(content[1]); }

 this.toHtml = function() {  return this._pseudoElement.render(); }

 this.executeOnElement = function(id, func, params) {  if (this._clearingHouse[id])  {   if (this._clearingHouse[id][func])   {    if (params)    {     switch (params.length)     {      case 1:       return this._clearingHouse[id][func](params[0]);      break;      case 2:       return this._clearingHouse[id][func](params[0], params[1]);      break;      case 3:       return this._clearingHouse[id][func](params[0], params[1], params[2]);      break;      case 4:       return this._clearingHouse[id][func](params[0], params[1], params[2], params[3]);      break;     }    }    else    {     return this._clearingHouse[id][func]();    }   }  }  return; }

 this.getJavascriptCode = function() {  return this._pseudoElement.getJavascriptCode('a', true); }

 this.elementToggleOpenClose = function(id) {  this._clearingHouse[id].toggleOpenClose(); }

 this.elementOpenWalkUp = function(id) {  if (typeof(this._clearingHouse[id]) != 'undefined')  {   var elm = this._clearingHouse[id];   elm.open(true);   if (typeof(elm.parent) != 'undefined')    this.elementOpenWalkUp(elm.parent.id);  }  else  {   return false;  }  return true; }

 this.elementCloseWalkUp = function(id) {  if (typeof(this._clearingHouse[id]) != 'undefined')  {   var elm = this._clearingHouse[id];   elm.close(true);   if (typeof(elm.parent) != 'undefined') this.elementCloseWalkUp(elm.parent.id);  }  else  {   return false;  }  return true; }

 this.elementCloseWalkDown = function(id) {  if (typeof(id) == 'undefined')  {   var elm = this._pseudoElement;  }  else if (typeof(this._clearingHouse[id]) != 'undefined')  {   var elm = this._clearingHouse[id];elm.close(true);  }  else  {   return false;  }  if (typeof(elm._children) != 'undefined')  {   for (var i=0; i<elm._children.length; i++)   {    this.elementCloseWalkDown(elm._children[i].id);   }  }  return true; }

 this.elementOpen = function(id) {  if (typeof(this._clearingHouse[id]) != 'undefined')  {   this._clearingHouse[id].open();  } }

 this.elementClose = function(id) {  this._clearingHouse[id].close(); }

 this.openPath = function(data, valueType) {  var elm = this.getElementByCaptionPath(data);  if (elm == false) return false;  this.elementOpenWalkUp(elm.id);  return true; }

 this.getElementByCaptionPath = function(data) {  var elm = this._pseudoElement;  for (var i=0; i<data.length; i++)  {   var newElm = null;   for (var j=0; j<elm._children.length; j++)   {    if (elm._children[j].caption == data[i])    {     newElm = elm._children[j];     elm = newElm;     if (typeof(elm._undoneChildren) == 'object')     {      for (var k=0; k<elm._undoneChildren.length; k++)      {       var newE = this._createTreeElement(elm._undoneChildren[k], elm._level +1);       elm.addChild(newE);      }      elm._undoneChildren = false;     }     break;    }   }   if (newElm == null) return false;  }  return newElm; }

 this.elementCheckboxEvent = function(id, value) {  this._clearingHouse[id].checkboxEvent(value); }

 this.debugDumpTree = function(elm, indent) {  if (typeof(elm) == 'undefined')  {   elm    = this._pseudoElement;indent = '';   var firstCall = true;  }  var ret = '';  if (typeof(elm._children) == 'object')  {   for (var i=0; i<elm._children.length; i++)   {    ret += indent + i + ': ' + elm._children[i].id + ': ' + elm._children[i].caption + "\n";    ret += this.debugDumpTree(elm._children[i], indent + '  ');   }  }  if (firstCall)  {   alert(ret);  }  else  {   return ret;  } }

 this._addError = function(str) {  if (typeof(this._errorArray) == 'undefined')  {   this._errorArray = new Array(str);  }  else  {   this._errorArray[this._errorArray.length] = str;  } }

 this.getLastError = function() {  if (typeof(this._errorArray) != 'undefined')  {   if (this._errorArray.length > 0)   {    return this._errorArray[this._errorArray.length -1];   }  }  return false; }

 this.old_drawInto = function(id) {  var content = this._pseudoElement.render();  var e       = document.getElementById(id);  if (e)  {   e.innerHTML = content[0];   if ('' != content[1]) eval(content[1]);  } }

 this._imgPreload = function() {  var id = this.globalId;  var e  = document.getElementById(id);  var ii = 0;  var outTemp = new Array();  outTemp[ii++] = '<img src="' + this.imageDir + 'line1.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'line2.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'line3.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'minus1.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'minus2.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'minus3.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'plus1.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'plus2.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'plus3.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'line3.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'empty.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'leaf.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'folderClosed.gif" border="0" style="display:none;">';  outTemp[ii++] = '<img src="' + this.imageDir + 'folderOpen.gif" border="0" style="display:none;">';  if (e) e.innerHTML = outTemp.join(''); }

 this._afterImgPreload = function() {  var id = this.globalId;  var content = this._pseudoElement.render();  var e       = document.getElementById(id);  if (e)   {   e.innerHTML = content[0];   if ('' != content[1])   {    eval(content[1]);   }  } }

 this.drawInto = function(id) {  this.globalId = id;  setTimeout('Bs_Objects['+this._id+']._imgPreload()', 0);  setTimeout('Bs_Objects['+this._id+']._afterImgPreload()',50); }

 this.getTotalElements = function() {//add 2004-6-15  return (this.totalElements - 1); }

 this._constructor();}Array.prototype.moveUp = function(key){ if (key == 0) return this; if (key >= (this.length)) return this;if (key > 1) {  var newArr = this.slice(0, key -1); } else {  var newArr = new Array; }

 newArr[newArr.length] = this[key];newArr[newArr.length] = this[key -1];var endArr = this.slice(key +1, this.length);return newArr.concat(endArr);}

Array.prototype.moveDown = function(key){ if (key >= (this.length -1)) return this;if (key > 0)  {  var newArr = this.slice(0, key); } else {  var newArr = new Array; } newArr[newArr.length] = this[key +1];newArr[newArr.length] = this[key];

 if (this.length > (key +2)) {  var endArr = this.slice(key +2, this.length);return newArr.concat(endArr); } return newArr;}

Array.prototype.moveToTop = function(key){ if (key == 0) return this; if (key >= (this.length)) return this; var startArr  = new Array(this[key]); var middleArr = this.slice(0, key); var endArr    = this.slice(key +1, this.length); return startArr.concat(middleArr, endArr);}

Array.prototype.moveToBottom = function(key){ if (key >= (this.length -1)) return this; if (key > 0) {  var startArr = this.slice(0, key); } else {  var startArr = new Array; } var middleArr = this.slice(key +1, this.length);var endArr    = new Array(this[key]);return startArr.concat(middleArr, endArr);}

Array.prototype.indexOf = function(str){ for(var i=0; i<this.length; i++) {  if (this[i] == str) return i; } return -1;};

Array.prototype.has = function(str){return (this.indexOf(str) >= 0);}

Array.prototype.deleteItem = function(i){ if (i<0 || i>(this.length-1)) return false;if (i == (this.length-1)) {  this.length--;return true; } for (var i=(i+1); i<this.length; i++) {  this[i-1] = this[i]; } this.length--; return true;};

Array.prototype.deleteItemHash = function(key){ var ret = new Array; for (var k in this) {  if (k != key) ret[k] = this[k]; } return ret;}

function bs_array_maxSizeOfLevel(array, level){ if (!array) return 0; if (array.length == 0) return 0; if (level == 1) return array.length; var ret = 0; for (var i=0; i<array.length; i++) {  if (array[i].length > ret) ret = array[i].length; } return ret;}

function bs_array_toCsv(array, separator){ if (typeof(separator) != 'string') separator = ';'; var ret = ''; for (var i=0; i<array.length; i++) {  var lineA = new Array();  for (var j=0; j<array[i].length; j++)  {   if ((array[i][j]) && (array[i][j]['value']))   {    lineA[j] = array[i][j]['value'];   }  }  ret += lineA.join(separator) + "\n"; } return ret;}

21人参与, 0条评论 登录后显示评论回复

你需要登录后才能评论 登录/ 注册