📄 mztreeview.js
字号:
/*---------------------------------------------------------------------------*\
| Subject: MzTreeView 2.0
| NameSpace: System.Web.UI.WebControls.MzTreeView
| Author: meizz
| Created: 2005-12-28
| Version: 2006-05-03
|-------------------------------------------------------------
| MSN: huangfr@msn.com QQ: 112889082 http://www.meizz.com
| Email: mz@126.com CSDN ID:meizz Copyright (c) meizz
\*---------------------------------------------------------------------------*/
//Using("System.Data.MzData");
//Using("System.Web.Forms.MzEffect");
function MzTreeView(){MzData.call(this);this.index = this.hashCode;}
t=MzTreeView.Extends(MzData, "MzTreeView");
MzTreeView.addProperty("defaultUrl", "#");
MzTreeView.addProperty("defaultTarget", "_self");
t.iconPath = System.path+"/system/_resource/mztreeview/";
//[configuration]
t.showPlus = true;
t.showLines = true;
t.showToolTip = true;
t.showNodeIcon = true;
t.autoSort = true;
t.useArrow = true;
t.dynamic = true;
t.useCheckbox = false;
t.autoFocused = false;
t.useContextMenu = false;
t.convertRootIcon = true;
t.canOperate = false;
MzTreeView.icons =
{
line :
{
"l0" :"_line0.gif",
"l1" :"_line1.gif",
"l2" :"_line2.gif",
"l3" :"_line3.gif",
"l4" :"_line4.gif",
"ll" :"_line5.gif",
"c0" :"_checkbox0.gif",
"c1" :"_checkbox1.gif",
"c2" :"_checkbox2.gif"
},
collapse:
{
"pm0":"_plus0.gif",
"pm1":"_plus1.gif",
"pm2":"_plus2.gif",
"pm3":"_plus3.gif",
"root":"root.gif",
"file":"file.gif",
"exit":"exit.gif",
"folder":"folder.gif"
},
expand :
{
"pm0":"_minus0.gif",
"pm1":"_minus1.gif",
"pm2":"_minus2.gif",
"pm3":"_minus3.gif",
"folder":"folderopen.gif"
}
};
MzTreeView.textLoading="正在加载...";
//public: entry
MzTreeView.prototype.render = function()
{
function loadImg(C){for(var i in C){if("string"==typeof C[i]){
var a=new Image(); a.src=me.iconPath + C[i]; C[i]=a;}}} var me=this;
loadImg(MzTreeView.icons.expand);loadImg(MzTreeView.icons.collapse);
loadImg(MzTreeView.icons.line); me.firstNode=null;
loadCssFile(this.iconPath +"mztreeview.css", "MzTreeView_CSS");
this.initialize(); var str="no data", i, root=this.rootNode;
if (root.hasChild){var a = [], c = root.childNodes; me.firstNode=c[0];
for(i=0;i<c.length;i++)a[i]=c[i].render(i==c.length-1);str=a.join("");}
setTimeout(function(){me.afterRender();}, 10);
return "<div class='mztreeview' id='MTV_root_"+ this.index +"' "+
"onclick='Instance(\""+ this.index +"\").clickHandle(event)' "+
"ondblclick='Instance(\""+ this.index +"\").dblClickHandle(event)' "+
">"+ str +"</div>";
};
MzTreeView.prototype.afterRender=function()
{
var me=this;
if(document.getElementById("MTV_root_"+ me.index))
{
if(me.firstNode)(me.currentNode=me.firstNode).focus();
this.dispatchEvent(new System.Event("onrender"));
if(me.useArrow) me.attachEventArrow();
}
else setTimeout(function(){me.afterRender();}, 100);
};
//private: attachEvent onTreeKeyDown
MzTreeView.prototype.attachEventArrow = function()
{
var a = document.getElementById("MTV_root_"+ this.index); if(!a) return;
var me= this; a.attachEvent("onkeydown", function(e)
{
e = window.event || e; var key = e.keyCode || e.which;
switch(key)
{
case 37 : me.focusUpperNode(); break; //Arrow left, shrink child node
case 38 : me.focusPreviousSibling();break; //Arrow up
case 39 : me.focusLowerNode(); break; //Arrow right, expand child node
case 40 : me.focusNextSibling(); break; //Arrow down
case 46 : if(me.canOperate) me.currentNode.removeNode(); break; //delete
}
});
};
MzTreeView.prototype.appendIcon=function(index, src, flag)
{
if(/^pm\d$/.test(index))return;
var a=new Image(); a.src=src;
if(flag){MzTreeView.icons.expand[index]=a;return;}
MzTreeView.icons.collapse[index]=a;
}
MzTreeView.prototype.setIconPath=function(path)
{
if(path.length>0) this.iconPath = path.replace(/[\\\/]*$/, "/");
}
//public: loadChildNodes(sourceId)
MzTreeView.prototype.loadChildNodes = function(id)
{
var n; if(n=this.getNodeById(id)) n.loadChildNodes();
};
//private: build treeline onTreeNodeBuild
MzTreeView.prototype.buildPrefix = function(prefix)
{
var a=prefix.replace(/^,+/,"").split(","); for(var i=0; i<a.length; i++)
if (a[i]) a[i]="<img src='"+ MzTreeView.icons.line[a[i]].src +"' alt='' />";
return this.showLines?(prefix ?a.join(""):""):(prefix ?"<div style='width:"+
(MzTreeView.icons.collapse["pm3"].width*a.length)+"px'> <\/div>":"");
};
//public: expand/collapse node
MzTreeView.prototype.expand = function(id)
{
var n; if(n=this.getNodeById(id)) n.expand();
};
MzTreeView.prototype.collapse = function(id)
{
var n; if(n=this.getNodeById(id)) n.collapse();
};
//private: attachEvent tree onClick
MzTreeView.prototype.clickHandle = function(e)
{
e = window.event || e; var B;
e = e.srcElement || e.target;
if(B=(e.id && 0==e.id.indexOf(this.index +"_")))
{
var n=this.currentNode=this.nodes[e.id.substr(e.id.lastIndexOf("_")+1)];
}
switch(e.tagName)
{
case "IMG" :
if(B)
{
if(e.id.indexOf(this.index +"_expand_")==0){
n.expanded ? n.collapse() : n.expand();}
else if(e.id.indexOf(this.index +"_icon_")==0){
n.focus();}
else if(e.id.indexOf(this.index +"_checkbox_")==0){
n.check(!n.checked); n.upCheck();}
}
break;
case "A":
if(B){ n.focus(); this.dispatchEvent(new System.Event("onclick"));}
break;
default :
if(System.ns) e = e.parentNode;
break;
}
};
//private: onTreeDoubleClick
MzTreeView.prototype.dblClickHandle = function(e)
{
e = window.event || e; e = e.srcElement || e.target;
if((e.tagName=="A" || e.tagName=="IMG")&& e.id)
{
e=this.nodes[e.id.substr(e.id.lastIndexOf("_") +1)];
e.expanded ? e.collapse() : e.expand();
this.currentNode=e;this.dispatchEvent(new System.Event("ondblclick"));
}
};
//public: focus a treeNode by node's clientIndex
MzTreeView.prototype.focus = function(id)
{
if(!this.selectedNode) this.selectedNode=this.rootNode;
var n; if(n=this.getNodeById(id)) n.focus();
};
MzTreeView.prototype.focusNodeByPath = function(path)
{
var me=this, n;
if ((n=path.indexOf(this.divider))>0)
{
var node = this.getNodeById(path.substring(0, n));
if(node){if(node!=this.rootNode && !node.expanded) node.expand();
node.focus();} else return;
path = path.substr(n + this.divider.length);
setTimeout(function(){me.focusNodeByPath(path)}, 1);
}
else this.getNodeById(path).focus();
};
//*
MzTreeView.prototype.focusUpperNode = function()
{
var e = this.selectedNode, r = this.rootNode; if(e){
if(e.parentNode==r && !e.expanded) return;
if(e.expanded) e.collapse(); else e.parentNode.focus();}
};
MzTreeView.prototype.focusLowerNode = function()
{
var e = this.selectedNode; if(e && e.hasChild){
if(e.expanded)e.childNodes[0].focus();else e.expand();}
}
MzTreeView.prototype.focusPreviousSibling = function()
{
var e = this.selectedNode;if(e && e.parentNode){
var c = e.parentNode.childNodes;for(var i=c.length-1; i>=0; i--){
if(c[i]==e){if(i==0) return; c[i-1].focus();}}}
};
MzTreeView.prototype.focusNextSibling = function()
{
var e = this.selectedNode; if(e && e.parentNode){
var c = e.parentNode.childNodes; for(var i=0; i<c.length; i++){
if(c[i]==e){ if(i==c.length-1) return; c[i+1].focus();}}}
};
MzTreeView.prototype.appendNode=function(sourceIndex, dataString)
{
if(!this.canOperate) return;
var a=sourceIndex.split(this.divider); if(a.length!=2) return;
var d={},pid=a[0],id=a[1];d[sourceIndex]=dataString;this.appendData(d);
var node=this.getNodeById(pid); if(!node) return; node.hasChild=true;
if(!node.isLoaded){node.updateNode(); node.expand();} else
node.appendNode(node.DTO(this.nodePrototype, sourceIndex));
this.currentNode=this.getNodeById(id);
this.dispatchEvent(new System.Event("onappendnode"));
if(this.useCheckbox && node.checked) node.check(true);
};
MzTreeView.prototype.updateNode = function(id)
{
if(!this.canOperate) return;
var node; if(node=this.getNodeById(id)) node.updateNode();
};
MzTreeView.prototype.removeNode = function(id)
{
if(!this.canOperate) return;
var node; if(node=this.getNodeById(id)) node.removeNode();
};
//************************ can expand a node all childNodes
MzTreeView.prototype.expandAll = function(id)
{
if("undefined"==typeof id) return;
var node=this.getNodeById(id);
if(!node||node.hasChlid) return;
node.expandAll();
};
MzTreeView.prototype.collapseAll = function(id)
{
if("undefined"==typeof id) return;
var node=this.getNodeById(id);
if(!node||node.childNodes.length==0) return;
node.collapseAll();
};
MzTreeView.prototype.expandLevel=function(level)
{
if(!/\d+/.test(level) || level==0)return;var r;
if((r=this.rootNode).hasChild)
{
for(var i=0, n=r.childNodes.length; i<n; i++)
r.childNodes[i].expandLevel(level);
}
};
window.MzTreeNode=function(){MzDataNode.call(this);};
t=MzTreeNode.Extends(MzDataNode, "MzTreeNode");
MzTreeView.prototype.nodePrototype=MzTreeNode;
MzTreeNode.htmlChildPrefix="";
t.checked = t.expanded = false;
t.childPrefix = "";
//private: load all node's node and init
MzTreeNode.prototype.loadChildNodes = function()
{
MzDataNode.prototype.loadChildNodes.call(this, MzTreeNode);
if(this.$$caller.useCheckbox)
{
var r=/^true$/i, data=this.$$caller.dataSource;
for(var i=0, n=this.childNodes.length; i<n; i++)
{
var node=this.childNodes[i];
var b=data[node.sourceIndex].getAttribute("checked");
if(b!=null) node.checked=r.test(b);
node.checked=node.parentNode.checked || node.checked;
}
if(n>0) this.childNodes[0].upCheck();
}
};
//private: single node build to HTML
MzTreeNode.prototype.render = function(last)
{
var $=this.$$caller, s=$.dataSource[this.sourceIndex],target,data,url;
var icon=s.getAttribute("icon");
if(!(target=s.getAttribute("target")))target=$.getDefaultTarget();
var hint=$.showToolTip ? s.getAttribute("hint") || this.text : "";
if(!(url=s.getAttribute("url"))) url = $.getDefaultUrl();
if(data=s.getAttribute("data"))url+=(url.indexOf("?")==-1?"?":"&")+data;
var id=this.index, s="";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -