xtree.js

来自「JS-异步加载树」· JavaScript 代码 · 共 1,427 行 · 第 1/3 页

JS
1,427
字号
		}
		if(i==this.parent.Nodes.length-1){			
			return this.parent.getNextSibling(true);
		}
		else{
			return this.parent.Nodes[i+1];
		}
	}
	else{
		return this;
	}
}

TreeItem.prototype.contextmenu=function(e){
	var root=this._root();
	if(root.onContextmenu){
		if(typeof root.contextmenu=="function"){
			root.onContextmenu(this,e);
		}
		else{
			eval(root.onContextmenu);
		}
	}
	return false;
}
TreeItem.prototype.keycode=function(key){
	window.status =key;
	if(key==39 && this.Nodes.length >0){
		if(!this.open){
			this.expand();
		}
		this.getFirst().select();
		return false;
	}
	else if(key==37){
		if(this.open) {
			this.collapse();	
		}
		else{
			if(this.parent){
				this.parent.select();
			}
			return false;
		}
	}
	else if(key==40){
		this.getNextSibling().select();
		return false;
	}
	else if(key==38){
		this.getPreviousSibling().select();
		return false;
	}else{
		if(key==13){
			this.toggle();
		}
	}
	window.event.returnValue=false;
	window.event.cancelBubble=true;
}
TreeItem.prototype._showParent=function(){
	
	if ( this.parent ){
		this.parent._showParent();		
		this.parent.expand();
	}
}
TreeItem.prototype.hover=function(){
	if(treeHandler.selected!=this){
		document.getElementById(this.id +"-text-anchor").className=treeConfig.css_ItemTextHover
	}
}

TreeItem.prototype.restore=function(){
	if(treeHandler.selected!=this){		
		document.getElementById(this.id +"-text-anchor").className=treeConfig.css_ItemText;
	}

}


////////////////////////////////////////////////////////////////////
// TreeObject
////////////////////////////////////////////////////////////////////
function TreeObject(sCaption){
	this.base=TreeItem;
	this.base(sCaption);
	treeHandler.selected=null;
	this.onSelected=null;
	this.onUnSelected=null;
	this.onExpand=null;
	this.onCollapse=null;
}
TreeObject.prototype=new TreeItem;
TreeObject.prototype.write=function(){
	var s=this.toString();
	
	document.write(s);
	this.expand();
}
TreeObject.prototype.selected=function(){
	return treeHandler.selected;
}

//////////////////////
//Check Box Item
/////////////////////

//定义选择的hook
treeHandler.check=function(oItem){
	var objChk=this.all[oItem.id.replace("-checkbox","")];
	objChk.check(!objChk.checked);
}

function TreeCheckItem(sCaption,vChecked,vData){
	this.base=TreeItem;
	this.base(sCaption);
	this.checked=vChecked || false;
	this.NodeData=vData;
}
TreeCheckItem.prototype=new TreeItem;

TreeCheckItem.prototype._TreeItem_toString=TreeItem.prototype.toString; //保留原有的toString方法


TreeCheckItem.prototype.toString=function(){
//因为只是修改其中的字符串,为了方便,我就不重新拷贝代码,也没有使用regexp进行操作
	var tmp=document.createElement("div");
	tmp.innerHTML=this._TreeItem_toString();	
	var objChk=document.createElement("span");
	objChk.className=treeConfig.css_ItemCheckBox;
	objChk.id=this.id +"-checkbox";
	objChk.onclick="treeHandler.check(this)";
	if (this.checked){
		//objChk.innerText="R";	
		objChk.innerText="r";
	}
	else{
		//objChk.innerText="T";
		objChk.innerText=" ";
	}
	
	/*
	var objChk=document.createElement("input");
	objChk.id=this.id +"-checkbox";
	objChk.type="checkbox";
	objChk.className=treeConfig.css_ItemCheckBox;
	objChk.onclick="treeHandler.check(this)";
	objChk.checked=true;
	*/
	/*
	if(this.checked ==true){
		objChk.checked=true;
	}
	else{
		objChk.checked=false;
	}	
	*/

	var txt=tmp.children[0];
	txt.children[txt.children.length-1].insertAdjacentElement("beforeBegin",objChk);
	return tmp.innerHTML;	
}

TreeCheckItem.prototype.check=function(b){
	
	var obj=document.getElementById(this.id +"-checkbox");
	this.checked=b;
	
	if(obj){		
		if(this.checked){
			//obj.innerHTML="R";
			obj.innerHTML="r";
		}
		else{
			//obj.innerHTML="T";
			obj.innerHTML=" ";
		}
	}	
	
	if(this.Nodes.length!=0){
		for(var i=0;i<this.Nodes.length;i++){
			
			if(this.Nodes[i] instanceof TreeCheckItem){
				this.Nodes[i].check(b);
			}
		}
	}
	
}
/////////////////////////////////
//设置上级节点的状态

////////////////////////////////
TreeCheckItem.prototype._checkParent=function(b){

}

TreeCheckItem.prototype._getSubState=function(){

}
TreeItem.prototype._getSubs=function(arr){
	var l=arr.length;
	if(this instanceof TreeCheckItem){
		if(this.checked){
			arr[l]=this;
		}
	}
	for(var i=0;i<this.Nodes.length;i++){
		this.Nodes[i]._getSubs(arr);
	}
}
TreeObject.prototype.getSelectedNodes=function(){
	var allNodes=new Array();
	this._getSubs(allNodes);
	
	return allNodes;
}
//////////////////////////////////////////
//注释的代码实现自定义选择的样式

/////////////////////////////////////////
/*
TreeCheckItem.prototype.select=function(){
	if(treeHandler.selected ==this) return;
	if(treeHandler.selected){
		treeHandler.selected.unselect();
	}	
	var div=document.getElementById(this.id);	
	treeHandler.selected=this;	
	//div.className="selectedItem";	
	div.style.backgroundColor="#FEE7D4";
	//div.style.color="window";
	
	var txt=document.getElementById(this.id +"-text-anchor");
	txt.focus();
	var root=this._root();
	if(root.onSelected){
		if(typeof root.onSelected=="function"){
			root.onSelected(this);
		}
		else{
			eval(root.onSelected);
		}
	}
	//div.focus();
}
TreeCheckItem.prototype.unselect=function(){	
	document.getElementById(this.id).style.backgroundColor="";
	document.getElementById(this.id).style.color="";
	var root=this._root();
	if(root.onUnSelected){ //定义了处理函数

		if(typeof root.onUnSelected=="function"){
			root.onUnSelected(treeHandler.selected);
		}
		else{
			eval(root.onUnSelected);
		}
	}
}
TreeCheckItem.prototype.hover=function(){
	document.getElementById(this.id +"-text-anchor").className="hoverItem";	
}

TreeCheckItem.prototype.restore=function(){		
	document.getElementById(this.id +"-text-anchor").className="labelItem";	
}
*/


//////////////////////////////////////////////
// XLoadTreeItem
/////////////////////////////////////////////
function TreeXLoadItem(sText,sSrc,sAction,sTarget,sOpenImage){
	this.base=TreeItem;
	this.base(sText,sAction,sTarget,sOpenImage);
	this.loaded=false;
	this.loading=false;
	this.xmlSrc=sSrc;
	this._loadingItem=new TreeItem("Loading");
	this.add(this._loadingItem);
	
}

TreeXLoadItem.prototype=new TreeItem;
TreeXLoadItem.prototype._TreeItem_expand=TreeItem.prototype.expand; //rem treeitem's method "expand"

TreeXLoadItem.prototype.expand=function(){
	if(!this.loaded){
		this._TreeItem_expand();		
		_startLoadXmlTree(this.xmlSrc, this);
	}
	else{
		this._TreeItem_expand();
	}
}
//开始下载数据

function _startLoadXmlTree(sSrc, jsNode) {
		var xmlDoc=new ActiveXObject("MSXML2.FreeThreadedDOMDocument")
		xmlDoc.async =true;		
		//xmlDoc.load(sSrc);
		//_xmlFileLoaded(xmlDoc,jsNode);	
		
		xmlDoc.onreadystatechange=function(){
			
			_xmlFileLoaded(xmlDoc,jsNode);
		}
		window.setTimeout(function(){xmlDoc.load(sSrc);},10);
		
		
}
/*
记录节点需要的扩展信息的处理对象

*/
function externalData(){
	var iCount=0;
	this.AddProperty=function(sName,sValue){
		this[sName]=sValue;	
		iCount++;
	}
	this.RemoveProperty=function(sName){
		if(this[sName]==null){
			throw "No Property";			
		}
		this[sName]=null;
		iCount--;
	}
	this.PropertyCount=function(){
		return iCount;
	}
}
function _xmlTreeAttribute(xmlNode,objNode){
	if(objNode.external==null){
		objNode.external=new externalData();
	}
	for(var i=0;i<xmlNode.attributes.length;i++){
		var attr=xmlNode.attributes[i];	
		var nodeName=attr.nodeName;
		if(!(nodeName=="src" || nodeName=="text")){
			objNode.external.AddProperty(attr.nodeName,attr.text);
		}
	}
}

//将xml转换成JS Node
function _xmlTreeToJsTree(oNode) {
	var text=oNode.getAttribute("text");
	
	var src=oNode.getAttribute("src");
	var oJsNode;
	if(src !=null && src!=""){
		
		oJsNode=new TreeXLoadItem(text,src);
	}
	else{
		oJsNode=new TreeItem(text);
	}	
	//记录字定义扩展属性

	_xmlTreeAttribute(oNode,oJsNode);
	var cs = oNode.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		if (cs[i].tagName == "tree")
			oJsNode.add( _xmlTreeToJsTree(cs[i]));
	}
	
	return oJsNode;
}

//完成下载
function _xmlFileLoaded(oXmlDoc, jsParentNode) {
	if(oXmlDoc.readyState==4){		
		//判断是否为空
		if( oXmlDoc == null || oXmlDoc.documentElement == null) {
			//alert(oXmlDoc.xml);
			//jsParentNode.errorText = parseTemplateString(webFXTreeConfig.loadErrorTextTemplate,
								//jsParentNode.src);
			jsParentNode._loadingItem.setCaption("加载错误");
			window.status ="加载错误";
			return ;
		}
		var oRoot=oXmlDoc.documentElement;	
		jsParentNode.loaded=true;	
		//alert(oXmlDoc.xml);
		jsParentNode.Nodes[0].remove();
		jsParentNode.rendered=false;
		for(var i=0;i<oRoot.childNodes.length;i++){			
			jsParentNode.add(_xmlTreeToJsTree(oRoot.childNodes[i]));
		}
		jsParentNode.expand();
		jsParentNode.select();
	}
}




///////////////////////////////////////////////////////////////////////////////////////////
//扩展功能
//getXML():取得JS Node的xml对象
//loadFromXML:从xml实例化对象


TreeItem.prototype.getXML=function(){
	var objDoc=new ActiveXObject("MSXML2.DOMDocument");
	//objDoc.loadXML("<?xml version=\"1.0\" encoding=\"gb2312\" ?>");
	objDoc.appendChild(this._getxml(objDoc));	
	return objDoc.xml;
	
}
TreeItem.prototype._getxml=function(objDoc){
	var el=objDoc.createElement("Node");
	el.setAttribute("title",this.Text);
	el.setAttribute("opened",this.open);
	el.setAttribute("src",this.xmlSrc);
	el.setAttribute("NodeID",this.id);
	var subNodes=objDoc.createElement("Nodes");	
	for(var i=0;i<this.Nodes.length;i++){
		subNodes.appendChild(this.Nodes[i]._getxml(objDoc));
	}
	el.appendChild(subNodes);
	return el;
}


function TreeXLoadObject(sCaption,sSrc){	
	this._base=TreeObject;
	this._base(sCaption);	
	this.loaded=false;
	this.loading=false;
	this.xmlSrc=sSrc;
	this._loadingItem=new TreeItem("Loading");
	this.add(this._loadingItem);
	
	_startLoadXmlTree(this.xmlSrc, this);
	
}
TreeXLoadObject.prototype=new TreeObject;
//TreeXLoadItem.prototype._TreeItem_expand=TreeItem.prototype.expand
TreeXLoadObject.prototype._TreeItem_expand=TreeObject.prototype.expand;
TreeXLoadObject.prototype.write=function(){
	var s=this.toString();	
	document.write(s);
	
	this.expand();
}
//TreeXLoadObject.prototype.expand=TreeXLoadItem.prototype.expand;
//TreeXLoadObject.prototype.expand=TreeXLoadItem.prototype.expand



///////////////////////////////////////////////////////
//提供数据接口
////////////////////////////////////////////////////////
function DataAdapter(insAdapter){
	this.Adapter=null;	
}

DataAdapter.prototype.startDownload=function(){

}

DataAdapter.prototype.Downloaded=function(){

}

DataAdapter.prototype.transformData=function(){

}
//xml方式
function DataXMLAdapter(){

}
DataXMLAdapter.prototype=new DataAdapter;
DataXMLAdapter.prototype.startDownload=function(){
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?