📄 dhtmlxtree.js
字号:
/*
Copyright Scand LLC http://www.scbr.com
This version of Software is free for using in non-commercial applications. For commercial use please contact info@scbr.com to obtain license
*/
/*_TOPICS_
@0:Initialization
@1:Selection control
@2:Add/delete
@3:Private
@4:Node/level control
@5:Checkboxes/user data manipulation
@6:Appearence control
*/
/**
* @desc: tree constructor
* @param: htmlObject - parent html object or id of parent html object
* @param: width - tree width
* @param: height - tree height
* @param: rootId - id of virtual root node
* @type: public
* @topic: 0
*/
function dhtmlXTreeObject(htmlObject, width, height, rootId){
this._isOpera=(navigator.userAgent.indexOf('Opera')!= -1);
if (typeof(htmlObject)!="object")
this.parentObject=document.getElementById(htmlObject);
else
this.parentObject=htmlObject;
this.xmlstate=0;
this.mytype="tree";
this.smcheck=true; //smart checkboxes
this.width=width;
this.height=height;
this.rootId=rootId;
this.childCalc=null;
this.def_img_x="18px";
this.def_img_y="18px";
this.style_pointer="pointer";
if (navigator.appName == 'Microsoft Internet Explorer') this.style_pointer="hand";
this._aimgs=true;
this.htmlcA=" [";
this.htmlcB="]";
this.lWin=window;
this.cMenu=0;
this.mlitems=0;
this.dadmode=0;
this.slowParse=false;
this.autoScroll=true;
this.hfMode=0;
this.nodeCut=0;
this.XMLsource=0;
this.XMLloadingWarning=0;
this._globalIdStorage=new Array();
this.globalNodeStorage=new Array();
this._globalIdStorageSize=0;
this.treeLinesOn=true;
this.checkFuncHandler=0;
this.openFuncHandler=0;
this.dblclickFuncHandler=0;
this.tscheck=false;
this.timgen=true;
this.dpcpy=false;
this.imPath="treeGfx/";
this.checkArray=new Array("iconUnCheckAll.gif","iconCheckAll.gif","iconCheckGray.gif","iconUncheckDis.gif");
this.lineArray=new Array("line2.gif","line3.gif","line4.gif","blank.gif","blank.gif");
this.minusArray=new Array("minus2.gif","minus3.gif","minus4.gif","minus.gif","minus5.gif");
this.plusArray=new Array("plus2.gif","plus3.gif","plus4.gif","plus.gif","plus5.gif");
this.imageArray=new Array("leaf.gif","folderOpen.gif","folderClosed.gif");
this.cutImg= new Array(0,0,0);
this.cutImage="but_cut.gif";
this.dragger= new dhtmlDragAndDropObject();
//create root
this.htmlNode=new dhtmlXTreeItemObject(this.rootId,"",0,this);
this.htmlNode.htmlNode.childNodes[0].childNodes[0].style.display="none";
this.htmlNode.htmlNode.childNodes[0].childNodes[0].childNodes[0].className="hiddenRow";
//init tree structures
this.allTree=this._createSelf();
this.allTree.appendChild(this.htmlNode.htmlNode);
this.allTree.onselectstart=new Function("return false;");
this.XMLLoader=new dtmlXMLLoaderObject(this._parseXMLTree,this);
this.selectionBar=document.createElement("DIV");
this.selectionBar.className="selectionBar";
this.selectionBar.innerHTML=" ";
//this.selectionBar.style.left=getAbsoluteLeft(this.allTree);
if (this.allTree.offsetWidth>20) this.selectionBar.style.width=this.allTree.offsetWidth-20;
this.selectionBar.style.display="none";
this.allTree.appendChild(this.selectionBar);
/*
this.selectionBox=document.createElement("DIV");
this.selectionBox.className="selectionBox";
this.selectionBox.innerHTML=" ";
//this.selectionBar.style.left=getAbsoluteLeft(this.allTree);
this.selectionBox.style.width=this.allTree.offsetWidth;
this.selectionBox.style.height=this.allTree.offsetHeight;
this.selectionBox.style.display="none";
this.allTree.appendChild(this.selectionBox);*/
return this;
};
/**
* @desc: tree node constructor
* @param: itemId - node id
* @param: itemText - node label
* @param: parentObject - parent item object
* @param: treeObject - tree object
* @param: actionHandler - onclick event handler(optional)
* @param: mode - do not show images
* @type: private
* @topic: 0
*/
function dhtmlXTreeItemObject(itemId,itemText,parentObject,treeObject,actionHandler,mode){
this.htmlNode="";
this.acolor="";
this.scolor="";
this.tr=0;
this.childsCount=0;
this.tempDOMM=0;
this.tempDOMU=0;
this.dragSpan=0;
this.dragMove=0;
this.span=0;
this.closeble=1;
this.childNodes=new Array();
this.userData=new Object();
this.checkstate=0;
this.treeNod=treeObject;
this.label=itemText;
this.parentObject=parentObject;
this.actionHandler=actionHandler;
this.images=new Array(treeObject.imageArray[0],treeObject.imageArray[1],treeObject.imageArray[2]);
this.id=treeObject._globalIdStorageAdd(itemId,this);
if (this.treeNod.checkBoxOff ) this.htmlNode=this.treeNod._createItem(1,this,mode);
else this.htmlNode=this.treeNod._createItem(0,this,mode);
this.htmlNode.objBelong=this;
return this;
};
/**
* @desc: register node
* @type: private
* @param: itemId - node identificator
* @param: itemObject - node object
* @topic: 3
*/
dhtmlXTreeObject.prototype._globalIdStorageAdd=function(itemId,itemObject){
if (this._globalIdStorageFind(itemId,1,1)) { d=new Date(); itemId=d.valueOf()+"_"+itemId; return this._globalIdStorageAdd(itemId,itemObject); }
this._globalIdStorage[this._globalIdStorageSize]=itemId;
this.globalNodeStorage[this._globalIdStorageSize]=itemObject;
this._globalIdStorageSize++;
return itemId;
};
/**
* @desc: unregister node
* @type: private
* @param: itemId - node identificator
* @topic: 3
*/
dhtmlXTreeObject.prototype._globalIdStorageSub=function(itemId){
for (var i=0; i<this._globalIdStorageSize; i++)
if (this._globalIdStorage[i]==itemId)
{
this._globalIdStorage[i]=this._globalIdStorage[this._globalIdStorageSize-1];
this.globalNodeStorage[i]=this.globalNodeStorage[this._globalIdStorageSize-1];
this._globalIdStorageSize--;
this._globalIdStorage[this._globalIdStorageSize]=0;
this.globalNodeStorage[this._globalIdStorageSize]=0;
}
};
/**
* @desc: return node object
* @param: itemId - node identificator
* @type: private
* @topic: 3
*/
dhtmlXTreeObject.prototype._globalIdStorageFind=function(itemId,skipXMLSearch,skipParsing){
// if (confirm(itemId)) { window.asdasd.asdasd(); }
for (var i=0; i<this._globalIdStorageSize; i++)
if (this._globalIdStorage[i]==itemId)
{
return this.globalNodeStorage[i];
}
return null;
};
/**
* @desc: create and return new line in tree
* @type: private
* @param: htmlObject - parent Node object
* @param: node - item object
* @topic: 2
*/
dhtmlXTreeObject.prototype._drawNewTr=function(htmlObject,node)
{
var tr =document.createElement('tr');
var td1=document.createElement('td');
var td2=document.createElement('td');
td1.appendChild(document.createTextNode(" "));
td2.colSpan=3;
td2.appendChild(htmlObject);
tr.appendChild(td1); tr.appendChild(td2);
return tr;
};
/**
* @desc: load tree from xml string
* @type: public
* @param: xmlString - XML string
* @param: afterCall - function which will be called after xml loading
* @topic: 0
*/
dhtmlXTreeObject.prototype.loadXMLString=function(xmlString,afterCall){
this.xmlstate=1;
this.XMLLoader.loadXMLString(xmlString); this.waitCall=afterCall||0; };
/**
* @desc: load tree from xml file
* @type: public
* @param: file - link too XML file
* @param: afterCall - function which will be called after xml loading
* @topic: 0
*/
dhtmlXTreeObject.prototype.loadXML=function(file,afterCall){
this.xmlstate=1;
this.XMLLoader.loadXML(file); this.waitCall=afterCall||0; };
/**
* @desc: create new child node
* @type: private
* @param: parentObject - parent node object
* @param: itemId - new node id
* @param: itemText - new node text
* @param: itemActionHandler - function fired on node select event
* @param: image1 - image for node without childrens;
* @param: image2 - image for closed node;
* @param: image3 - image for opened node
* @param: optionStr - string of otions
* @param: childs - node childs flag (for dynamical trees) (optional)
* @param: beforeNode - node, after which new node will be inserted (optional)
* @topic: 2
*/
dhtmlXTreeObject.prototype._attachChildNode=function(parentObject,itemId,itemText,itemActionHandler,image1,image2,image3,optionStr,childs,beforeNode){
if (beforeNode) parentObject=beforeNode.parentObject;
if (((parentObject.XMLload==0)&&(this.XMLsource))&&(!this.XMLloadingWarning))
{
parentObject.XMLload=1; this.loadXML(this.XMLsource+getUrlSymbol(this.XMLsource)+"itemId="+escape(parentObject.id));
}
var Count=parentObject.childsCount;
var Nodes=parentObject.childNodes;
if (beforeNode)
{
var ik,jk;
for (ik=0; ik<Count; ik++)
if (Nodes[ik]==beforeNode)
{
for (jk=Count; jk!=ik; jk--)
Nodes[1+jk]=Nodes[jk];
break;
}
ik++;
Count=ik;
}
if ((!itemActionHandler)&&(this.aFunc)) itemActionHandler=this.aFunc;
if (optionStr) {
var tempStr=optionStr.split(",");
for (var i=0; i<tempStr.length; i++)
{
switch(tempStr[i])
{
case "TOP": if (parentObject.childsCount>0) { beforeNode=new Object; beforeNode.tr=parentObject.childNodes[0].tr.previousSibling; }
for (ik=0; ik<Count; ik++)
Nodes[ik+Count]=Nodes[ik+Count-1];
Count=0;
break;
}
};
};
Nodes[Count]=new dhtmlXTreeItemObject(itemId,itemText,parentObject,this,itemActionHandler,1);
if(image1) Nodes[Count].images[0]=image1;
if(image2) Nodes[Count].images[1]=image2;
if(image3) Nodes[Count].images[2]=image3;
parentObject.childsCount++;
var tr=this._drawNewTr(Nodes[Count].htmlNode);
if (this.XMLloadingWarning)
Nodes[Count].htmlNode.parentNode.parentNode.style.display="none";
if ((beforeNode)&&(beforeNode.tr.nextSibling))
parentObject.htmlNode.childNodes[0].insertBefore(tr,beforeNode.tr.nextSibling);
else
if ((this.parsingOn)&&(this.parsingOn==parentObject.id))
{
this.parsedArray[this.parsedArray.length]=tr;
}
else
parentObject.htmlNode.childNodes[0].appendChild(tr);
if ((beforeNode)&&(!beforeNode.span)) beforeNode=null;
if (this.XMLsource) if ((childs)&&(childs!=0)) Nodes[Count].XMLload=0; else Nodes[Count].XMLload=1;
Nodes[Count].tr=tr;
tr.nodem=Nodes[Count];
if (parentObject.itemId==0)
tr.childNodes[0].className="hitemIddenRow";
if (optionStr) {
var tempStr=optionStr.split(",");
for (var i=0; i<tempStr.length; i++)
{
switch(tempStr[i])
{
case "SELECT": this.selectItem(itemId,false); break;
case "CALL": this.selectItem(itemId,true); break;
case "CHILD": Nodes[Count].XMLload=0; break;
case "CHECKED":
if (this.XMLloadingWarning)
this.setCheckList+=","+itemId;
else
this.setCheck(itemId,1);
break;
case "HCHECKED":
this._setCheck(Nodes[Count],"notsure");
break;
case "OPEN": Nodes[Count].openMe=1; break;
}
};
};
if (!this.XMLloadingWarning)
{
if (this._getOpenState(parentObject)<0)
this.openItem(parentObject.id);
if (beforeNode)
{
this._correctPlus(beforeNode);
this._correctLine(beforeNode);
}
this._correctPlus(parentObject);
this._correctLine(parentObject);
this._correctPlus(Nodes[Count]);
if (parentObject.childsCount>=2)
{
this._correctPlus(Nodes[parentObject.childsCount-2]);
this._correctLine(Nodes[parentObject.childsCount-2]);
}
if (parentObject.childsCount!=2) this._correctPlus(Nodes[0]);
if (this.tscheck) this._correctCheckStates(parentObject);
}
if (this.cMenu) this.cMenu.setContextZone(Nodes[Count].span,Nodes[Count].id);
return Nodes[Count];
};
/**
* @desc: create new node, next to existing
* @type: public
* @param: parentId - parent node id
* @param: itemId - new node id
* @param: itemText - new node text
* @param: itemActionHandler - function fired on node select event (optional)
* @param: image1 - image for node without childrens; (optional)
* @param: image2 - image for closed node; (optional)
* @param: image3 - image for opened node (optional)
* @param: optionStr - options string (optional)
* @param: childs - node childs flag (for dynamical trees) (optional)
* @topic: 2
*/
dhtmlXTreeObject.prototype.insertNewItem=function(parentId,itemId,itemText,itemActionHandler,image1,image2,image3,optionStr,childs){
var parentObject=this._globalIdStorageFind(parentId);
if (!parentObject) return (-1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -