xtree.js 代码
xtree.js 代码
发布时间:2016-12-30 来源:查字典编辑
摘要:复制代码代码如下://**PoweredbyFason//**Email:fason_pfx@hotmail.comvaricon={roo...

复制代码 代码如下:

//**PoweredbyFason

//**Email:fason_pfx@hotmail.com

varicon={

root:'image/root.gif',

open:'image/open.png',

close:'image/close.png',

file:'image/file.png',

Rplus:'image/Rplus.gif',

Rminus:'image/Rminus.gif',

join:'image/T.gif',

joinbottom:'image/L.gif',

plus:'image/Tplus.gif',

plusbottom:'image/Lplus.gif',

minus:'image/Tminus.gif',

minusbottom:'image/Lminus.gif',

blank:'image/blank.gif',

line:'image/I.gif'

};

varGlobal={

id:0,

getId:function(){returnthis.id++;},

all:[],

selectedItem:null,

defaultText:"treeItem",

defaultAction:"javascript:void(0)",

defaultTarget:"_blank"

}

functionpreLoadImage(){

for(iinicon){

vartem=icon[i];

icon[i]=newImage()

icon[i].src=tem

}

};preLoadImage();

functiontreeItem(text,action,target,title,Icon)

{

this.id=Global.getId();

this.level=0;

this.text=text?text:Global.defaultText;

this.action=action?action:Global.defaultAction;

this.target=target?target:Global.defaultTarget;

this.title=title?title:this.text;

this.isLast=true;

this.childNodes=newArray();

this.indent=newArray();

this.parent=null;

varc=0;

if(getCookie("item"+this.id)!=null)c=getCookie("item"+this.id);

this.open=parseInt(c);

this.load=false;

this.setuped=false;

this.JsItem=null;

this.container=document.createElement("div");

this.icon=Icon;

Global.all[Global.all.length]=this;

}

treeItem.prototype.toString=function()

{

varo=this;

varoItem=document.createElement("div");

oItem.id="treeItem"+this.id

oItem.className="treeItem";

oItem.noWrap=true;

oItem.onselectstart=function(){returnfalse;}

oItem.oncontextmenu=function(){returnfalse;}

this.JsItem=oItem;

this.drawIndents();

variIcon=document.createElement("img");

iIcon.align="absmiddle";

iIcon.src=this.childNodes.length>0?(this.open?(this.level>0?(this.isLast?icon.minusbottom.src:icon.minus.src):icon.Rminus.src):(this.level>0?(this.isLast?icon.plusbottom.src:icon.plus.src):icon.Rplus.src)):(this.level>0?(this.isLast?icon.joinbottom.src:icon.join.src):icon.blank.src);

iIcon.id="treeItem-icon-handle-"+this.id;

iIcon.onclick=function(){o.toggle();};

oItem.appendChild(iIcon);

variIcon=document.createElement("img");

iIcon.align="absmiddle";

iIcon.src=this.icon?this.icon:(this.childNodes.length>0?(this.open?icon.open.src:icon.close.src):icon.file.src);

iIcon.id="treeItem-icon-folder-"+this.id;

iIcon.onclick=function(){o.select();};

iIcon.ondblclick=function(){o.toggle();};

oItem.appendChild(iIcon);

vareText=document.createElement("span");

vareA=document.createElement("a");

eA.innerHTML=this.text;

eA.target=this.target;

eA.href=this.action;

eA.onkeydown=function(e){returno.KeyDown(e);}

if(this.action==Global.defaultAction)eA.onclick=function(){o.toggle();returnfalse;}

eText.appendChild(eA);

eText.id="treeItem-text-"+this.id;

eText.className="treeItem-unselect"

eText.onclick=function(){o.select(1);};

eText.title=this.title;

oItem.appendChild(eText);

this.container.id="treeItem-container-"+this.id;

this.container.style.display=this.open?"":"none";

oItem.appendChild(this.container);

returnoItem;

}

treeItem.prototype.root=function()

{

varp=this;

while(p.parent)

p=p.parent;

returnp;

}

treeItem.prototype.setText=function(sText)

{

if(this.root().setuped)

{

varoItem=document.getElementById("treeItem-text-"+this.id);

oItem.firstChild.innerHTML=sText;

}

this.text=sText;

}

treeItem.prototype.setIndent=function(l,v)

{

for(vari=0;i<this.childNodes.length;i++)

{

this.childNodes[i].indent[l]=v;

this.childNodes[i].setIndent(l,v);

}

}

treeItem.prototype.drawIndents=function()

{

varoItem=this.JsItem;

for(vari=0;i<this.indent.length;i++){

variIcon=document.createElement("img");

iIcon.align="absmiddle";

iIcon.id="treeItem-icon-"+this.id+"-"+i;

iIcon.src=this.indent[i]?icon.blank.src:icon.line.src;

oItem.appendChild(iIcon);

}

}

treeItem.prototype.add=function(oItem)

{

oItem.parent=this;

this.childNodes[this.childNodes.length]=oItem;

oItem.level=this.level+1;

oItem.indent=this.indent.concat();

oItem.indent[oItem.indent.length]=this.isLast;

if(this.childNodes.length>1){

varo=this.childNodes[this.childNodes.length-2];

o.isLast=false;

o.setIndent(o.level,0);

if(this.root().setuped)o.reload(1);

}

elseif(this.root().setuped)

this.reload(0);

this.container.appendChild(oItem.toString());

this.container.style.display=this.open?"":"none";

}

treeItem.prototype.loadChildren=function()

{

//dosomething

}

treeItem.prototype.remove=function()

{

vartmp=this.getPreviousSibling();

//if(tmp){tmp.select();}

this.removeChildren();

varp=this.parent;

if(!p){return};

if(p.childNodes.length>0){

varo=p.childNodes[p.childNodes.length-1];

o.isLast=true;

o.setIndent(o.level,1);

if(o.root().setuped)o.reload(1);

}

else

p.reload();

}

treeItem.prototype.removeChildren=function()

{

if(this==Global.selectedItem){Global.selectedItem=null;}

for(vari=this.childNodes.length-1;i>=0;i--)

this.childNodes[i].removeChildren();

varo=this;

varp=this.parent;

if(p){p.childNodes=p.childNodes._remove(o);}

Global.all[this.id]=null

varoItem=document.getElementById("treeItem"+this.id);

if(oItem){oItem.parentNode.removeChild(oItem);}

}

treeItem.prototype.reload=function(flag)

{

if(flag){

for(varj=0;j<this.childNodes.length;j++){this.childNodes[j].reload(1);}

for(vari=0;i<this.indent.length;i++)

document.getElementById("treeItem-icon-"+this.id+"-"+i).src=this.indent[i]?icon.blank.src:icon.line.src;

}

document.getElementById("treeItem-icon-handle-"+this.id).src=this.childNodes.length>0?(this.open?(this.level>0?(this.isLast?icon.minusbottom.src:icon.minus.src):icon.Rminus.src):(this.level>0?(this.isLast?icon.plusbottom.src:icon.plus.src):icon.Rplus.src)):(this.level>0?(this.isLast?icon.joinbottom.src:icon.join.src):icon.blank.src);

if(!this.icon)

document.getElementById("treeItem-icon-folder-"+this.id).src=this.childNodes.length>0?(this.open?icon.open.src:icon.close.src):icon.file.src;

}

treeItem.prototype.toggle=function()

{

if(this.childNodes.length>0){

if(this.open)

this.collapse();

else

this.expand();

}

}

treeItem.prototype.expand=function()

{

this.open=1;

setCookie("item"+this.id,1);

if(!this.load){

this.load=true;

this.loadChildren();

this.reload(1);

}

else

this.reload(0);

this.container.style.display="";

}

treeItem.prototype.collapse=function()

{

this.open=0;

setCookie("item"+this.id,0);

this.container.style.display="none";

this.reload(0);

this.select(1);

}

treeItem.prototype.expandAll=function()

{

if(this.childNodes.length>0&&!this.open)this.expand();

this.expandChildren();

}

treeItem.prototype.collapseAll=function()

{

this.collapseChildren();

if(this.childNodes.length>0&&this.open)this.collapse();

}

treeItem.prototype.expandChildren=function()

{

for(vari=0;i<this.childNodes.length;i++)

this.childNodes[i].expandAll();

}

treeItem.prototype.collapseChildren=function()

{

for(vari=0;i<this.childNodes.length;i++)

this.childNodes[i].collapseAll()

}

treeItem.prototype.openURL=function()

{

if(this.action!=Global.defaultAction){

window.open(this.action,this.target);

}

}

treeItem.prototype.select=function(o)

{

if(Global.selectedItem)Global.selectedItem.unselect();

varoItem=document.getElementById("treeItem-text-"+this.id);

oItem.className="treeItem-selected";

oItem.firstChild.focus();

Global.selectedItem=this;

if(!o)this.openURL();

}

treeItem.prototype.unselect=function()

{

varoItem=document.getElementById("treeItem-text-"+this.id);

oItem.className="treeItem-unselect";

oItem.firstChild.blur();

Global.selectedItem=null;

}

treeItem.prototype.setup=function(oTaget)

{

oTaget.appendChild(this.toString());

this.setuped=true;

if(this.childNodes.length>0||this.open)this.expand();

}

/**********************************************/

/*

ArrowKeyEvent

*/

/**********************************************/

treeItem.prototype.getFirstChild=function()

{

if(this.childNodes.length>0&&this.open)

returnthis.childNodes[0];

returnthis;

}

treeItem.prototype.getLastChild=function()

{

if(this.childNodes.length>0&&this.open)

returnthis.childNodes[this.childNodes.length-1].getLastChild();

returnthis;

}

treeItem.prototype.getPreviousSibling=function()

{

if(!this.parent)returnnull;

for(vari=0;i<this.parent.childNodes.length;i++)

if(this.parent.childNodes[i]==this)break;

if(i==0)

returnthis.parent;

else

returnthis.parent.childNodes[i-1].getLastChild();

}

treeItem.prototype.getNextSibling=function()

{

if(!this.parent)returnnull;

for(vari=0;i<this.parent.childNodes.length;i++)

if(this.parent.childNodes[i]==this)break;

if(i==this.parent.childNodes.length-1)

returnthis.parent.getNextSibling();

else

returnthis.parent.childNodes[i+1];

}

treeItem.prototype.KeyDown=function(e){

varcode,o;

if(!e)e=window.event;

code=e.which?e.which:e.keyCode;

o=this;

if(code==37)

{

if(o.open)o.collapse();

else

{

if(o.parent)o.parent.select();

}

returnfalse;

}

elseif(code==38)

{

vartmp=o.getPreviousSibling();

if(tmp)tmp.select();

returnfalse;

}

elseif(code==39)

{

if(o.childNodes.length>0)

{

if(!o.open)o.expand();

else

{

vartmp=o.getFirstChild();

if(tmp)tmp.select();

}

}

returnfalse;

}

elseif(code==40)

{

if(o.open&&o.childNodes.length>0)o.getFirstChild().select();

else

{

vartmp=o.getNextSibling();

if(tmp)tmp.select();

}

returnfalse;

}

elseif(code==13)

{

o.toggle();

o.openURL();

returnfalse;

}

returntrue;

}

/*****************************************************/

Array.prototype.indexOf=function(o){

for(vari=0;i<this.length;i++)

if(this[i]==o)returni;

return-1;

}

Array.prototype.removeAt=function(i){

returnthis.slice(0,i).concat(this.slice(i+1))

}

Array.prototype._remove=function(o){

vari=this.indexOf(o);

if(i!=-1)returnthis.removeAt(i)

returnthis

}

/*****************************************************/

/*****************************************************/

/*

xtreeItemClass

*/

/*****************************************************/

functionxtreeItem(uid,text,action,target,title,Icon,xml){

this.uid=uid;

this.base=treeItem;

this.base(text,action,target,title,Icon);

this.Xml=xml;

}

xtreeItem.prototype=newtreeItem;

xtreeItem.prototype.parseElement=function(dom){

returndom.selectSingleNode("/TreeNode");

}

xtreeItem.prototype.addNodesLoop=function(oItem)

{

for(vari=0;i<oItem.childNodes.length;i++)

{

varo=oItem.childNodes[i];

vartmp=newxtreeItem(o.getAttribute("id"),o.getAttribute("text"),o.getAttribute("href"),o.getAttribute("target"),o.getAttribute("title"),o.getAttribute("icon"),o.getAttribute('Xml'));

this.add(tmp);

if(o.getAttribute("Xml"))tmp.add(newtreeItem("Loading..."));

else

{

tmp.load=true;

tmp.addNodesLoop(o);

}

}

}

xtreeItem.prototype.loadChildren=function()

{

varoItem=this;

varoLoad=oItem.childNodes[0];

varXmlHttp=newActiveXObject("Microsoft.XMLHTTP");

XmlHttp.onreadystatechange=function(){

if(XmlHttp.readyState==4){

if(XmlHttp.status==200){

if(XmlHttp.responseXML.xml==""){oLoad.setText("unavaible1");return;}

varXmlItem=oItem.parseElement(XmlHttp.responseXML.documentElement);

if(XmlItem.childNodes.length==0){oLoad.setText("unavaible")}

else

{

oItem.addNodesLoop(XmlItem);

for(vari=0;i<oItem.childNodes.length;i++)

{

if(parseInt(getCookie("item"+oItem.childNodes[i].id))==1)

{oItem.childNodes[i].expand();}

}

if(Global.selectedItem==oItem.childNodes[0])oItem.select();

oLoad.remove();

}

}

else{

oLoad.setText("unavaible");

}

XmlHttp=null;

oItem.select(1);

}

}

try{

XmlHttp.open("get",this.Xml+(/?/g.test(this.Xml)?"&":"?")+"temp="+Math.random(),true);

XmlHttp.send();

}catch(e){oLoad.setText("unavaible");}

}

xtreeItem.prototype.setup=function(oTarget){

this.add(newtreeItem("Loading..."));

oTarget.appendChild(this.toString());

this.setuped=true;

if(this.childNodes.length>0||this.open)this.expand();

}

/*****************************************************/

functionsetCookie(name,value)

{

varDays=7;

varexp=newDate();

exp.setTime(exp.getTime()+Days*24*60*60*1000);

document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();

}

functiongetCookie(name)

{

vararr=document.cookie.match(newRegExp("(^|)"+name+"=([^;]*)(;|$)"));

if(arr!=null)returnunescape(arr[2]);returnnull;

}

functiondelCookie(name)

{

varexp=newDate();

exp.setTime(exp.getTime()-1);

varcval=getCookie(name);

if(cval!=null)document.cookie=name+"="+cval+";expires="+exp.toGMTString();

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类