?? mztreeview10.js
字號(hào):
<!--
/*---------------------------------------------------------------------------*\
| Subject: Web TreeView Class |
| Version: 1.0 |
| Author: 黃方榮【meizz】【梅花雪】 |
| FileName: MzTreeView.js |
| Created: 2004-10-18 |
| LastModified: 2005-03-10 |
| Download: http://www.meizz.com/Web/Download/MzTreeView10.rar |
| Explain: http://www.meizz.com/Web/Article.asp?id=436 |
| Demo: http://www.meizz.com/Web/Demo/MzTreeView10.htm |
| |
| You may use this code on your item |
| this entire copyright notice appears unchanged |
| and you clearly display a link to http://www.meizz.com/ |
| |
|-----------------------------------------------------------------------------|
| MSN: huangfr@msn.com QQ: 112889082 http://www.meizz.com |
| CSDN Community ID: meizz Copyright (c) 2004-2005 meizz |
\*---------------------------------------------------------------------------*/
//MzTreeView1.0網(wǎng)頁樹類, 在實(shí)例化的時(shí)候請把實(shí)例名作參數(shù)傳遞進(jìn)來
function MzTreeView(Tname)
{
if(typeof(Tname) != "string" || Tname == "")
throw(new Error(-1, '創(chuàng)建類實(shí)例的時(shí)候請把類實(shí)例的引用變量名傳遞進(jìn)來!'));
//【property】
this.url = "#";
this.target = "_self";
this.name = Tname;
this.wordLine = false;
this.currentNode = null;
this.useArrow = true;
this.nodes = {};
this.node = {};
this.names = "";
this._d = "\x0f";
this.index = 0;
this.divider = "_";
this.node["0"] =
{
"id": "0",
"path": "0",
"isLoad": false,
"childNodes": [],
"childAppend": "",
"sourceIndex": "0"
};
this.colors =
{
"highLight" : "#0A246A",
"highLightText" : "#FFFFFF",
"mouseOverBgColor" : "#D4D0C8"
};
this.icons = {
L0 : 'L0.gif', //┏
L1 : 'L1.gif', //┣
L2 : 'L2.gif', //┗
L3 : 'L3.gif', //━
L4 : 'L4.gif', //┃
PM0 : 'P0.gif', //+┏
PM1 : 'P1.gif', //+┣
PM2 : 'P2.gif', //+┗
PM3 : 'P3.gif', //+━
empty : 'L5.gif', //空白圖
root : 'root.gif', //缺省的根節(jié)點(diǎn)圖標(biāo)
folder : 'folder.gif', //缺省的文件夾圖標(biāo)
file : 'file.gif', //缺省的文件圖標(biāo)
exit : 'exit.gif'
};
this.iconsExpand = { //存放節(jié)點(diǎn)圖片在展開時(shí)的對應(yīng)圖片
PM0 : 'M0.gif', //-┏
PM1 : 'M1.gif', //-┣
PM2 : 'M2.gif', //-┗
PM3 : 'M3.gif', //-━
folder : 'folderopen.gif',
exit : 'exit.gif'
};
//擴(kuò)展 document.getElementById(id) 多瀏覽器兼容性
//id 要查找的對象 id
this.getElementById = function(id)
{
if (typeof(id) != "string" || id == "") return null;
if (document.getElementById) return document.getElementById(id);
if (document.all) return document.all(id);
try {return eval(id);} catch(e){ return null;}
}
//MzTreeView 初始化入口函數(shù)
this.toString = function()
{
this.browserCheck();
this.dataFormat();
this.setStyle();
this.load("0");
var rootCN = this.node["0"].childNodes;
var str = "<A id='"+ this.name +"_RootLink' href='#' style='DISPLAY: none'></A>";
if(rootCN.length>0)
{
this.node["0"].hasChild = true;
for(var i=0; i<rootCN.length; i++)
str += this.nodeToHTML(rootCN[i], i==rootCN.length-1);
setTimeout(this.name +".expand('"+ rootCN[0].id +"', true); "+
this.name +".focusClientNode('"+ rootCN[0].id +"'); "+ this.name +".atRootIsEmpty()",10);
}
if (this.useArrow) //使用方向鍵控制跳轉(zhuǎn)到上級(jí)下級(jí)父級(jí)子級(jí)節(jié)點(diǎn)
{
if (document.attachEvent)
document.attachEvent("onkeydown", this.onkeydown);
else if (document.addEventListener)
document.addEventListener('keydown', this.onkeydown, false);
}
return "<DIV class='MzTreeView' "+
"onclick='"+ this.name +".clickHandle(event)' "+
"ondblclick='"+ this.name +".dblClickHandle(event)' "+
">"+ str +"</DIV>";
};
this.onkeydown= function(e)
{
e = window.event || e; var key = e.keyCode || e.which;
switch(key)
{
case 37 : eval(Tname).upperNode(); break; //Arrow left, shrink child node
case 38 : eval(Tname).pervNode(); break; //Arrow up
case 39 : eval(Tname).lowerNode(); break; //Arrow right, expand child node
case 40 : eval(Tname).nextNode(); break; //Arrow down
}
};
}
//瀏覽器類型及版本檢測
MzTreeView.prototype.browserCheck = function()
{
var ua = window.navigator.userAgent.toLowerCase(), bname;
if(/msie/i.test(ua))
{
this.navigator = /opera/i.test(ua) ? "opera" : "";
if(!this.navigator) this.navigator = "msie";
}
else if(/gecko/i.test(ua))
{
var vendor = window.navigator.vendor.toLowerCase();
if(vendor == "firefox") this.navigator = "firefox";
else if(vendor == "netscape") this.navigator = "netscape";
else if(vendor == "") this.navigator = "mozilla";
}
else this.navigator = "msie";
if(window.opera) this.wordLine = false;
};
//給 TreeView 樹加上樣式設(shè)置
MzTreeView.prototype.setStyle = function()
{
/*
width: 16px; \
height: 16px; \
width: 20px; \
height: 20px; \
*/
var style = "<style>"+
"DIV.MzTreeView DIV IMG{border: 0px solid #FFFFFF;}"+
"DIV.MzTreeView DIV SPAN IMG{border: 0px solid #FFFFFF;}";
if(this.wordLine)
{
style +="\
DIV.MzTreeView DIV\
{\
height: 20px;"+
(this.navigator=="firefox" ? "line-height: 20px;" : "" ) +
(this.navigator=="netscape" ? "" : "overflow: hidden;" ) +"\
}\
DIV.MzTreeView DIV SPAN\
{\
vertical-align: middle; font-size: 21px; height: 20px; color: #D4D0C8; cursor: default;\
}\
DIV.MzTreeView DIV SPAN.pm\
{\
width: "+ (this.navigator=="msie"||this.navigator=="opera" ? "11" : "9") +"px;\
height: "+ (this.navigator=="netscape"?"9":(this.navigator=="firefox"?"10":"11")) +"px;\
font-size: 7pt;\
overflow: hidden;\
margin-left: -16px;\
margin-right: 5px;\
color: #000080; \
vertical-align: middle;\
border: 1px solid #D4D0C8;\
cursor: "+ (this.navigator=="msie" ? "hand" : "pointer") +";\
padding: 0 2px 0 2px;\
text-align: center;\
background-color: #F0F0F0;\
}";
}
style += "<\/style>";
/*alert(document.getElementsByTagName("HEAD")[0].innerHTML);
if(document.body)
{
var head = document.getElementsByTagName("HEAD")[0];
head.innerHTML = head.innerHTML + style;
}
else */
document.write(style);
};
//當(dāng)根節(jié)點(diǎn)為空的時(shí)候做的處理
MzTreeView.prototype.atRootIsEmpty = function()
{
var RCN = this.node["0"].childNodes;
for(var i=0; i<RCN.length; i++)
{
if(!RCN[i].isLoad) this.expand(RCN[i].id);
if (RCN[i].text=="")
{
var node = RCN[i].childNodes[0], HCN = node.hasChild;
if(this.wordLine)
{
var span = this.getElementById(this.name +"_tree_"+ node.id);
span = span.childNodes[0].childNodes[0].childNodes[0];
span.innerHTML = RCN[i].childNodes.length>1 ? "┌" : "─";
}
else
{
node.iconExpand = RCN[i].childNodes.length>1 ? HCN ? "PM0" : "L0" : HCN ? "PM3" : "L3"
this.getElementById(this.name +"_expand_"+ node.id).src = this.icons[node.iconExpand].src;
}
}
}
};
//初始化數(shù)據(jù)源里的數(shù)據(jù)以便后面的快速檢索
MzTreeView.prototype.dataFormat = function()
{
var a = new Array();
for (var id in this.nodes) a[a.length] = id;
this.names = a.join(this._d + this._d);
this.totalNode = a.length; a = null;
};
//在數(shù)據(jù)源檢索所需的數(shù)據(jù)節(jié)點(diǎn)
//id 客戶端節(jié)點(diǎn)對應(yīng)的id
MzTreeView.prototype.load = function(id)
{
var node = this.node[id], d = this.divider, _d = this._d;
var sid = node.sourceIndex.substr(node.sourceIndex.indexOf(d) + d.length);
var reg = new RegExp("(^|"+_d+")"+ sid +d+"[^"+_d+d +"]+("+_d+"|$)", "g");
var cns = this.names.match(reg), tcn = this.node[id].childNodes; if (cns){
reg = new RegExp(_d, "g"); for (var i=0; i<cns.length; i++)
tcn[tcn.length] = this.nodeInit(cns[i].replace(reg, ""), id); }
node.isLoad = true;
};
//初始化節(jié)點(diǎn)信息, 根據(jù) this.nodes 數(shù)據(jù)源生成節(jié)點(diǎn)的詳細(xì)信息
//sourceIndex 數(shù)據(jù)源中的父子節(jié)點(diǎn)組合的字符串 0_1
//parentId 當(dāng)前樹節(jié)點(diǎn)在客戶端的父節(jié)點(diǎn)的 id
MzTreeView.prototype.nodeInit = function(sourceIndex, parentId)
{
this.index++;
var source= this.nodes[sourceIndex], d = this.divider;
var text = this.getAttribute(source, "text");
var hint = this.getAttribute(source, "hint");
var sid = sourceIndex.substr(sourceIndex.indexOf(d) + d.length);
this.node[this.index] =
{
"id" : this.index,
"text" : text,
"hint" : hint ? hint : text,
"icon" : this.getAttribute(source, "icon"),
"path" : this.node[parentId].path + d + this.index,
"isLoad": false,
"isExpand": false,
"parentId": parentId,
"parentNode": this.node[parentId],
"sourceIndex" : sourceIndex,
"childAppend" : ""
};
this.nodes[sourceIndex] = "index:"+ this.index +";"+ source;
this.node[this.index].hasChild = this.names.indexOf(this._d + sid + d)>-1;
if(this.node[this.index].hasChild) this.node[this.index].childNodes = [];
return this.node[this.index];
};
//從XML格式字符串里提取信息
//source 數(shù)據(jù)源里的節(jié)點(diǎn)信息字符串(以后可以擴(kuò)展對XML的支持)
//name 要提取的屬性名
MzTreeView.prototype.getAttribute = function(source, name)
{
var reg = new RegExp("(^|;|\\s)"+ name +"\\s*:\\s*([^;]*)(\\s|;|$)", "i");
if (reg.test(source)) return RegExp.$2.replace(/[\x0f]/g, ";"); return "";
};
//根據(jù)節(jié)點(diǎn)的詳細(xì)信息生成HTML
//node 樹在客戶端的節(jié)點(diǎn)對象
//AtEnd 布爾值 當(dāng)前要轉(zhuǎn)換的這個(gè)節(jié)點(diǎn)是否為父節(jié)點(diǎn)的子節(jié)點(diǎn)集中的最后一項(xiàng)
MzTreeView.prototype.nodeToHTML = function(node, AtEnd)
{
var source = this.nodes[node.sourceIndex];
var target = this.getAttribute(source, "target");
var data = this.getAttribute(source, "data");
var url = this.getAttribute(source, "url");
if(!url) url = this.url;
if(data) url += (url.indexOf("?")==-1?"?":"&") + data;
if(!target) target = this.target;
var id = node.id;
var HCN = node.hasChild, isRoot = node.parentId=="0";
if(isRoot && node.icon=="") node.icon = "root";
if(node.icon=="" || typeof(this.icons[node.icon])=="undefined")
node.icon = HCN ? "folder" : "file";
node.iconExpand = AtEnd ? "└" : "├";
var HTML = "<DIV noWrap='True'><NOBR>";
if(!isRoot)
{
node.childAppend = node.parentNode.childAppend + (AtEnd ? " " : "│");
if(this.wordLine)
{
HTML += "<SPAN>"+ node.parentNode.childAppend + (AtEnd ? "└" : "├") +"</SPAN>";
if(HCN) HTML += "<SPAN class='pm' id='"+ this.name +"_expand_"+ id +"'>+</SPAN>";
}
else
{
node.iconExpand = HCN ? AtEnd ? "PM2" : "PM1" : AtEnd ? "L2" : "L1";
HTML += "<SPAN>"+ this.word2image(node.parentNode.childAppend) +"<IMG "+
"align='absmiddle' id='"+ this.name +"_expand_"+ id +"' "+
"src='"+ this.icons[node.iconExpand].src +"' style='cursor: "+ (!node.hasChild ? "":
(this.navigator=="msie"||this.navigator=="opera"? "hand" : "pointer")) +"'></SPAN>";
}
}
HTML += "<IMG "+
"align='absMiddle' "+
"id='"+ this.name +"_icon_"+ id +"' "+
"src='"+ this.icons[node.icon].src +"'><A "+
"class='MzTreeview' hideFocus "+
"id='"+ this.name +"_link_"+ id +"' "+
"href='"+ url +"' "+
"target='"+ target +"' "+
"title='"+ node.hint +"' "+
"onfocus=\""+ this.name +".focusLink('"+ id +"')\" "+
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -