newxtree.js代码
newxtree.js代码
发布时间:2016-12-30 来源:查字典编辑
摘要:/*=========================================PoweredbyFasonEmail:fason_p...

/*=========================================

PoweredbyFason

Email:fason_pfx@hotmail.com

HomePage:http://fason.nease.net

Version:3.0

=========================================*/

varTreeConfig={

TreeIcon:{

root:'root.gif',

folderopen:'folderopen.gif',

folder:'folder.gif',

file:'file.gif',

Rplus:'Rplus.gif',

Rminus:'Rminus.gif',

join:'T.gif',

joinbottom:'L.gif',

plus:'Tplus.gif',

plusbottom:'Lplus.gif',

minus:'Tminus.gif',

minusbottom:'Lminus.gif',

blank:'blank.gif',

line:'I.gif'

},

defaultText:"New",

defaultHref:"javascript:void(0)",

defaultTarget:"_blank",

loadingText:"Loading...",

unavaibleText:"Unavaible",

useCookie:true,

contextmenu:null

};

varTreeHandler={

id:0,

all:{},

getId:function(obj,key){

varID=key==null?this.id:key;

this.all[ID]=obj;

returnkey==null?this.id++:key;

},

setImagePath:function(sPath){

for(iinTreeConfig.TreeIcon){

vartem=newImage();

tem.src=sPath+TreeConfig.TreeIcon[i];

TreeConfig.TreeIcon[i]=tem.src;

}

}

};

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

//WebCookie

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

varWebCookie=newfunction(){

this.setValue=function(sName,sValue,sExpire,sPath,sDomain,sSecure){

varcookie=sName+"="+escape(sValue);

if(sExpire)cookie+=";expires="+sExpire.toGMTString();

if(sPath)cookie+=";path="+sPath;

if(sSecure)cookie+=";secure";

document.cookie=cookie;

};

this.getValue=function(sName){

varc=document.cookie.split(";");

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

varcItem=c[i].split("=");

if(cItem[0]==sName)returnunescape(cItem[1]);

}

returnnull;

};

this.delCookie=function(sName){

varcVal=this.getValue(sName);

if(cVal!=null){

vard=newDate();d.setTime(d.getTime()-1);

this.setValue(sName,cVal,d);

}

};

};

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

//TreeNode

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

Array.prototype.Remove=function(o){

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

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

}

if(i!=this.length)returnthis.slice(0,i).concat(this.slice(i+1,this.length));

returnthis;

};

functionTreeNode(sKey,sText,sHref,sTarget,sTitle,sIcon,sOpenIcon,sXMLSrc){

this.id=TreeHandler.getId(this,sKey);

this.level=0;

this.text=sText?sText:TreeConfig.defaultText;

this.href=sHref?sHref:TreeConfig.defaultHref;

this.target=sHref?(sTarget?sTarget:TreeConfig.defaultTarget):"_self";

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

this.childNodes=newArray();

this.parentNode=null;

this.open=TreeConfig.useCookie?this.getOpen():0;

this.shown=false;

this.icon=sIcon;

this.openIcon=sOpenIcon;

this.src=sXMLSrc;

this._tree=null;

this.onexpand=null;

this.oncollapse=null;

this.onselect=null;

this.toElement();

if(sXMLSrc){

this.open=0;

this.loader=newthis.constructor(null,TreeConfig.loadingText,null,null,null);

this.add(this.loader);

}

};

TreeNode.prototype.toElement=function(){

varf=typeof(this.href)=="function";

varoThis=this;

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

this._item.className="TreeNode";

this._item.noWrap=true;

this._item.onselectstart=function(){returnfalse;}

this._handler=document.createElement("img");

this._handler.align="absmiddle";

this._handler.onclick=function(){oThis.toggle();};

this._item.appendChild(this._handler);

this._icon=document.createElement("img");

this._icon.align="absmiddle";

//this._icon.onclick=function(){oThis.select(true);};

this._icon.onclick=function(){oThis.toggle();};//superj修改,单击图标为展开

this._icon.ondblclick=function(){oThis.toggle();};

this._item.appendChild(this._icon);

this._anchor=document.createElement("a");

this._anchor.className="TreeNode-Anchor"

this._anchor.innerHTML=this.HTMLtoText(this.text);

this._anchor.target=f?"_self":this.target;

this._anchor.href=f?TreeConfig.defaultHref:this.href;

this._anchor.title=this.title;

//this._anchor.onmousedown=function(e){returnoThis.contextMenu(e);};

this._anchor.onmousedown=function(){oThis.toggle();};//superj修改,单击标签为展开

this._anchor.onfocus=function(){oThis.focus();}

this._anchor.onblur=function(){oThis.blur();};

this._anchor.onkeydown=function(e){returnoThis.KeyDown(e);}

this._item.appendChild(this._anchor);

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

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

this._item.appendChild(this._container);

};

TreeNode.prototype.HTMLtoText=function(s){

returnString(s).replace(/&/g,"&").replace(/"/g,'"').replace(/</g,'<').replace(/>/g,'>');

};

TreeNode.prototype.isLast=function(){

varp=this.parentNode;

if(p==null)returnfalse;

returnp.childNodes[p.childNodes.length-1]==this;

};

TreeNode.prototype.indent=function(){

for(vari=0;i<this.childNodes.length;i++){this.childNodes[i].indent();}

vart=this._item,iv=this.level;

if(iv)while(--iv){t.removeChild(t.firstChild);}

varnode=this.parentNode,v=0,_root=this.getRoot();

while(node){

v++;

if(node==_root)break;

varm=document.createElement("img");

m.align="absmiddle";

m.src=node.isLast()?TreeConfig.TreeIcon.blank:TreeConfig.TreeIcon.line;

t.insertBefore(m,t.firstChild);

node=node.parentNode;

}

this.level=v;

};

TreeNode.prototype.recalIndent=function(nLevel,b){

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

this.childNodes[i]._item.childNodes[nLevel-1].src=b?TreeConfig.TreeIcon.blank:TreeConfig.TreeIcon.line;

this.childNodes[i].recalIndent(nLevel,b);

}

};

TreeNode.prototype.reloadIcon=function(){

varl=this.isLast(),o=this.open,m=TreeConfig.TreeIcon;

if(this.parentNode){

this._handler.src=this.childNodes.length>0?(o?(l?m.minusbottom:m.minus):(l?m.plusbottom:m.plus)):(l?m.joinbottom:m.join);

}

this._icon.src=this.childNodes.length>0?(o?(this.openIcon?this.openIcon:(this.icon?this.icon:m.folderopen)):(this.icon?this.icon:m.folder)):(this.icon?this.icon:m.file);

};

TreeNode.prototype.addXMLNodeLoop=function(doc){

varc=doc.childNodes;

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

varo=c[i];

if(o.nodeType==1){

varX=this.constructor;

varnode=newX(o.getAttribute("id"),o.getAttribute("text"),o.getAttribute("href"),o.getAttribute("target"),o.getAttribute("title"),o.getAttribute("icon"),o.getAttribute("openicon"),o.getAttribute('src'));

this.add(node);

if(!o.getAttribute("src")){

node.addXMLNodeLoop(o);

}

}

}

};

TreeNode.prototype.XMLHttpCallBack=function(){

if(this._xmlhttp.readyState!=4)return;

varoLoad=this.loader;

vardoc=this._xmlhttp.responseXML;

varsXML=String(this._xmlhttp.responseText).replace(/<?xml[^?]*?>/i,"");

if(window.DOMParser){

doc=(newDOMParser()).parseFromString(sXML,'text/xml');

}else{

doc.loadXML(sXML);

}

if(doc.documentElement){

varoRoot=doc.getElementsByTagName("Tree")[0];

if(oRoot.childNodes.length==0){this.setText(TreeConfig.unavaibleText);}

else{

vars=this._tree.getSelectedNode()==oLoad;

this.addXMLNodeLoop(oRoot);

oLoad.remove();

this.async();

this.loader=null;

}

}

else{

oLoad.setText(TreeConfig.unavaibleText);

}

};

TreeNode.prototype.getXML=function(){

varoLoad=this.loader;

varoThis=this;

this._xmlhttp=null;

try{

if(window.XMLHttpRequest)

this._xmlhttp=newXMLHttpRequest();

elseif(window.ActiveXObject)

this._xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

}catch(e){}

if(this._xmlhttp==null)thrownewError('Yourbrowserdoesn'tsupport!');

this._xmlhttp.onreadystatechange=function(){oThis.XMLHttpCallBack();};

try{

vartemp=(/?/g.test(this.src)?"&":"?")+"temp="+String(newDate().getTime())

this._xmlhttp.open("get",this.src+temp,true);

this._xmlhttp.send(null);

}catch(e){oLoad.setText(TreeConfig.unavaibleText);}

};

TreeNode.prototype.resetTree=function(oTree){

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

this.childNodes[i].resetTree(oTree);

this._tree=oTree;

};

TreeNode.prototype.setOpen=function(v){

this.open=v;

if(TreeConfig.useCookie){

WebCookie.setValue("o"+this.id,v);

}

};

TreeNode.prototype.getOpen=function(){

varo=WebCookie.getValue("o"+this.id);

if(o!=null)

returnparseInt(o);

return0;

};

TreeNode.prototype.toHTML=function(){

varo=this._item;

this.indent();

this.reloadIcon();

if(this.parentNode==null)o.removeChild(this._handler);

returno;

};

TreeNode.prototype.getRoot=function(){

varroot=this;

while(root.parentNode)root=root.parentNode;

returnroot;

};

TreeNode.prototype.setText=function(sText){

this.text=sText;

this._anchor.innerHTML=this.HTMLtoText(sText);

};

TreeNode.prototype.add=function(oItem){

vartree=this._tree;

oItem.parentNode=this;

varlen=this.childNodes.length;

this.childNodes[len]=oItem;

if(len>0){

varo=this.childNodes[len-1];

o.recalIndent(o.level,false);

o.reloadIcon();

}elseif(tree){

if(tree._rendered)this.open=0;

this.reloadIcon();

}

if(tree)this.resetTree(tree);

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

this._container.appendChild(oItem.toHTML());

returnoItem;

};

TreeNode.prototype.remove=function(f){

for(vari=0;i<this.childNodes.length;i++){this.childNodes[i].remove(true);}

this.unselect();

varv=this.getPreviousSibling();

varp=this.parentNode;

if(p){

p.childNodes=p.childNodes.Remove(this);

if(p.childNodes.length>0){

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

node.recalIndent(node.level,true);

node.reloadIcon();

}else{

p.setOpen(0);

p._container.style.display="none";

p.reloadIcon();

}

}

vartmp=this._item;

if(tmp)tmp.parentNode.removeChild(tmp);

deleteTreeConfig[this.id];

if(v&&!f)v.select(false);

};

TreeNode.prototype.toggle=function(){

if(this.childNodes.length>0){

if(this.open){

this.collapse();

}

else{

this.expand();

}

}

};

TreeNode.prototype.expand=function(){

this.setOpen(1);

if(!this.shown){

this.shown=true;

if(this.src)this.getXML();

}

this.reloadIcon();

this._container.style.display="";

if(typeofthis.onexpand=="function"){

this.onexpand();

}else{

eval(this.onexpand);

}

};

TreeNode.prototype.collapse=function(){

this.setOpen(0);

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

this.reloadIcon();

this.select(false);

if(typeofthis.oncollapse=="function"){

this.oncollapse();

}else{

eval(this.oncollapse);

}

};

TreeNode.prototype.async=function(){

vara=this._tree.context;

if(!a.length)return;

varid=a[a.length-1];

varnode=TreeHandler.all[id];

if(typeof(node)!='undefined'){

if(node.parentNode==this){

this._.context=a.slice(0,-1);

if(node.childNodes.length>0)

node.expand();

else

node.select();

}

}

};

TreeNode.prototype.expandAll=function(){

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

this.expandChildren();

};

TreeNode.prototype.collapseAll=function(){

this.collapseChildren();

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

};

TreeNode.prototype.expandChildren=function(){

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

this.childNodes[i].expandAll();

};

TreeNode.prototype.collapseChildren=function(){

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

this.childNodes[i].collapseAll();

};

TreeNode.prototype.openURL=function(){

if(typeofthis.href=="function"){

this.href();

}elseif(this.href!=TreeConfig.defaultHref){

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

}

};

TreeNode.prototype.select=function(b){

this._anchor.focus();

if(b){

this.openURL();

}

};

TreeNode.prototype.unselect=function(){

this._anchor.className="TreeNode-Anchor";

varselected=this._tree.getSelectedNode();

if(selected==this)this._tree.setSelectedNode(null);

};

TreeNode.prototype.focus=function(){

varnode=this._tree.getSelectedNode();

if(node&&node!=this){node.unselect();}

this._tree.setSelectedNode(this);

varoItem=this._anchor;

oItem.className="TreeNode-Anchorfocus";

if(typeofthis.onselect=="function"){

this.onselect();

}else{

eval(this.onselect);

}

};

TreeNode.prototype.blur=function(){

varoItem=this._anchor;

oItem.className="TreeNode-Anchorselected";

};

TreeNode.prototype.contextMenu=function(e){

e=e||window.event;

if(e.button==2){

if(typeofTreeConfig.contextmenu=="function")

TreeConfig.contextmenu();

returnfalse;

}

returntrue;

};

TreeNode.prototype.getFirstChild=function(){

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

returnthis.childNodes[0];

returnthis;

};

TreeNode.prototype.getLastChild=function(){

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

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

returnthis;

};

TreeNode.prototype.getPreviousSibling=function(){

if(!this.parentNode)returnnull;

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

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

if(i==0)

returnthis.parentNode;

else

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

};

TreeNode.prototype.getNextSibling=function(){

if(!this.parentNode)returnnull;

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

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

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

returnthis.parentNode.getNextSibling();

else

returnthis.parentNode.childNodes[i+1];

}

TreeNode.prototype.KeyDown=function(e){

e=e||window.event;

varcode=e.which||e.keyCode;

varo=this;

if(code==37){

if(this.open)this.collapse();

else{

if(this.parentNode)this.parentNode.select(false);

}

returnfalse;

}

elseif(code==38){

varel=o.getPreviousSibling();

if(el)el.select(false);

returnfalse;

}

elseif(code==39){

if(this.childNodes.length>0){

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

else{

varel=o.getFirstChild();

if(el)el.select(false);

}

}

returnfalse;

}

elseif(code==40){

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

else{

varel=o.getNextSibling();

if(el)el.select(false);

}

returnfalse;

}

elseif(code==13){

this.toggle();

returntrue;

}

returntrue;

};

functionCheckBoxTreeNode(sKey,sName,sText,sHref,sTarget,sTitle,sIcon,sOpenIcon,sXMLSrc){

this._base=TreeNode;

this._base(sKey,sText,sHref,sTarget,sTitle,sIcon,sOpenIcon,sXMLSrc);

this.name=sName;

this.checked=false;

};

CheckBoxTreeNode.prototype=newTreeNode;

CheckBoxTreeNode.prototype.toHTML=function(){

this._base=TreeNode.prototype.toHTML;

this._base();

varoThis=this;

this._checkbox=document.createElement("input");

this._checkbox.id=this._checkbox.name=this.name;

this._checkbox.type="checkbox";

this._checkbox.defaultChecked=this.parentNodeinstanceofCheckBoxTreeNode?this.parentNode.getChecked():this.checked;

this._checkbox.onclick=function(){oThis.check()};

this._checkbox.hideFocus=true;

this._item.insertBefore(this._checkbox,this._icon);

returnthis._item;

};

CheckBoxTreeNode.prototype.check=function(){

this.setCheckedChildren(this.getChecked());

this.setCheckedParent();

if(typeofthis.oncheck=="function"){

this.oncheck();

}else{

eval(this.oncheck);

}

};

CheckBoxTreeNode.prototype.setCheckedChildren=function(b){

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

if(this.childNodes[i]instanceofCheckBoxTreeNode)

this.childNodes[i].setCheckedChildren(b);

}

this._checkbox.checked=b;

};

CheckBoxTreeNode.prototype.setCheckedParent=function(){

varp=this.parentNode;

if(pinstanceofCheckBoxTreeNode){

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

if(!p.childNodes[i].getChecked())break;

}

p._checkbox.checked=i==p.childNodes.length

p.setCheckedParent();

}

};

CheckBoxTreeNode.prototype.getChecked=function(){

returnthis._checkbox.checked;

};

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

//TreeView

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

functionTreeView(sKey,sText,sHref,sTarget,sTitle,sIcon,sOpenIcon,sXMLSrc){

this.base=TreeNode;

this.base(sKey,sText,sHref,sTarget,sTitle,sIcon,sOpenIcon,sXMLSrc);

this.icon=sIcon||TreeConfig.TreeIcon.root;

this.context=newArray();

this._rendered=false;

this._tree=this;

};

TreeView.prototype=newTreeNode;

TreeView.prototype.getSelectedNode=function(){

if(window.selectedNode)returnwindow.selectedNode;

returnnull;

};

TreeView.prototype.setSelectedNode=function(oNode){

window.selectedNode=oNode;

};

TreeView.prototype.setContext=function(){

this.context=newArray();

for(vari=arguments.length-1,j=0;i>=0;i--,j++){

this.context[j]=arguments[i];

}

};

TreeView.prototype.create=function(oTarget){

oTarget.appendChild(this.toHTML());

this._rendered=true;

if(this.childNodes.length>0||this.open||this.src)

this.expand();

};

TreeView.prototype.toString=function(){

varobj=this.toHTML();

varo=document.createElement("div");

o.appendChild(obj);

this._rendered=true;

returno.innerHTML;

};

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