📄 tv.js
字号:
/*============================
Author : fason 阿信
Email : fason_pfx@hotmail.com
============================*/
var Icon = {
root : "function.gif",
folderopen : "menu_arrowdown.gif",
folderclosed : "menu_arrow.gif",
leaf : "menu_dot.gif",
leaftop : "menu_arrowdot.gif",
Rminus: "arrowred_right_icon.gif",
Rplus: "arrowred_icon.gif",
minusbottom: "arrowred_right_icon.gif",
plusbottom: "arrowred_icon.gif",
minus: "arrowred_right_icon.gif",
plus: "arrowred_icon.gif",
join: "menu_dot.gif",
joinbottom: "menu_dot.gif",
blank: "blank.gif",
line: "blank.gif"
}
window.TV = [];
function TreeView()
{
this.id = window.TV.length;
window.TV[this.id] = this;
this.target = "_self";
this.showLeaf = true;
this.showAll = true;
this.Nodes ={ 0 : { ID : 0, ParentID : -1, Text : null, Href : null, Image : null, childNodes: new Array() } };
}
var tv = TreeView.prototype;
tv.setTarget = function(v) {
this.target = v;
}
tv.useLeaf = function(v) {
this.showLeaf = v;
}
tv.setShowAll = function(v) {
this.showAll = v;
}
tv.setImagePath = function(sPath) {
for(o in Icon){
tmp = sPath + Icon[o];
Icon[o] = new Image();
Icon[o].src = tmp;
}
}
tv.add = function(iD,ParentiD,sText,sHref,sTarget,sImage) {
this.Nodes[iD] = { ID : iD, ParentID : ParentiD, Text : sText, Href : sHref, Target : sTarget, Image : sImage , childNodes: new Array() , open : false ,isLast : true};
var ch = this.getNode(ParentiD).childNodes;
ch[ch.length] = this.Nodes[iD];
if (ch.length>1) { ch[ch.length-2].isLast = false;}
};
tv.getNode = function(sKey) {
if (typeof this.Nodes[sKey] != "undefined")
{
return this.Nodes[sKey];
}
return null;
};
tv.getParentNode = function(ID) {
var key = this.getNode(ID).ParentID;
if(this.getNode(key) == null) return null;
return this.getNode(key);
}
tv.hasChildNodes = function(sKey) {
return this.getNode(sKey).childNodes.length > 0;
};
tv.getRoot = function(ID) {
var par = this.getParentNode(ID);
if (this.getNode(ID).ParentID == 0){
return this.getNode(ID);
}
else{
return this.getRoot(par.ID);
}
}
tv.drawNode = function(ID) {
var html = "";
var node = this.getNode(ID);
var rootid = this.getRoot(ID).ID;
var hc = this.hasChildNodes(ID);
var ir = this.getRoot(ID).ID == ID;
html += '<div class="TreeNode" nowrap> '+this.drawIndent(ID)+
'<a target="_self" href="javascript: void window.TV['+this.id+'].openHandler('+ID+')"><img style="display:none" id="icon'+ID+'" src="'+( node.Image ? node.Image : (hc ? Icon.folderclosed.src : (this.useLeaf ? Icon.leaf.src : Icon.folderclosed.src)))+'" align="absmiddle"></a>'+
'<span><a id="node'+ID+'" class="normal" href="'+node.Href+'" target="'+(node.Target ? node.Target : this.target)+'" onclick="window.TV['+this.id+'].openHandler('+ID+')">'+ node.Text +'</a></span></div>'
if(ir){
html='<tr><td height="24" bgcolor="E4F3FB" style="border-bottom:#C6D3DE 1px solid;">'+html+'</td></tr>';
}
else {
html='<tr><td style="border-bottom:#C6D3DE 1px solid;" height="24" >'+html+'</td></tr>';
}
if (hc) {
var io = ID == rootid;
node.open = io;
html += ('<tr><td><table border="0" cellspacing="0" cellpadding="0" width="95%" align="center" id="container'+ID+'" style="display:'+(io ? '' : 'none')+'">');
html += this.addNode(ID);
html += '</table></td></tr>';
}
return html;
}
tv.addNode = function(ID) {
var node = this.getNode(ID);
var html = "";
for(var i = 0;i<node.childNodes.length;i++)
html += this.drawNode(node.childNodes[i].ID);
return html;
}
tv.drawIndent = function(ID) {
var s = ''
var ir = this.getRoot(ID).ID == ID;
var hc = this.hasChildNodes(ID);
var iL = this.getNode(ID).isLast;
if(ir && !hc){
s += ('<img id="handler'+ID+'" src="'+ Icon.leaftop.src + '" align="absmiddle">');
}
else{
if(this.getParentNode(ID) != null)
s += ((hc ? '<a href="javascript:void window.TV['+this.id+'].openHandler('+ID+');" target="_self">':'')+'<img id="handler'+ID+'" src="'+ (this.hasChildNodes(ID) ? (ir ? Icon.Rminus.src : (iL ? Icon.plusbottom.src : Icon.plus.src)) : (ir ? Icon.blank.src : (iL ? Icon.joinbottom.src : Icon.join.src))) + '" align="absmiddle">'+(hc?'</a>':''));
}
var p = this.getParentNode(ID);
/*while(p != null)
{
if(this.getParentNode(p.ID) == null)break;
s = ('<img src="'+(this.getNode(p.ID).isLast ? Icon.blank.src : Icon.line.src) + '" align="absmiddle">')+s;
p = this.getParentNode(p.ID);
}*/
return s;
}
tv.setSelected = function(ID) {
if(this.selectedID) { document.getElementById("node" + this.selectedID).className = "normal";}
this.selectedID = ID;
var node = document.getElementById("node" + ID);
node.className = "selected";
node.focus();
}
tv.openHandler = function(ID) {
if (this.hasChildNodes(ID)) {
if (this.getNode(ID).open) {
this.collapse(ID);
}
else {
this.expand(ID);
}
}
this.setSelected(ID);
}
tv.openFolder = function(ID)
{
if (this.hasChildNodes(ID) && !this.getNode(ID).open) {
this.expand(ID);
}
this.setSelected(ID);
}
tv.expand = function(ID) {
var handler = document.getElementById("handler"+ID);
var container = document.getElementById("container"+ID);
var folder = document.getElementById("icon" + ID);
handler.src = this.getRoot(ID).ID == ID ? Icon.Rminus.src : ( this.getNode(ID).isLast ? Icon.minusbottom.src : Icon.minus.src);
container.style.display = '';
if(this.getRoot(ID).ID == ID) handler.style.display='none';
else handler.style.display='';
if(this.getRoot(ID).ID != ID) folder.style.display = 'none';
else folder.style.display = '';
if (this.hasChildNodes(ID)) {
if(this.getNode(ID).Image == null) folder.src = Icon.folderopen.src;
}
this.getNode(ID).open = true;
if(this.showAll == false) {
var ch = this.getParentNode(ID).childNodes;
for (var i=0; i<ch.length; i++) {
if(this.getNode(ID) != ch[i] && this.hasChildNodes(ch[i].ID)) this.collapse(ch[i].ID);
}
}
}
tv.collapse = function(ID) {
var handler = document.getElementById("handler"+ID);
var container = document.getElementById("container"+ID);
var folder = document.getElementById("icon" + ID);
if(this.getRoot(ID).ID != ID) folder.style.display = 'none';
else folder.style.display = '';
if(this.getRoot(ID).ID == ID) handler.style.display='none';
else handler.style.display='';
handler.src = this.getRoot(ID).ID == ID ? Icon.Rplus.src : ( this.getNode(ID).isLast ? Icon.plusbottom.src : Icon.plus.src);
container.style.display = 'none';
if (this.hasChildNodes(ID)) {
if(this.getNode(ID).Image == null) folder.src = Icon.folderclosed.src;
}
this.getNode(ID).open = false;
}
tv.expandAll = function() { this.All(1);}
tv.collapseAll = function() { this.All(0);}
tv.All = function(v) {
for(var i in this.Nodes) {
if(this.Nodes[i].ID==0)continue;
if(this.hasChildNodes(this.Nodes[i].ID)) {
if (v) {
this.expand(this.Nodes[i].ID);
}
else {
this.collapse(this.Nodes[i].ID);
}
}
}
}
tv.setup = function() {
document.write('<table border="0" cellspacing="0" cellpadding="0" width="100%">'+this.addNode(0)+'</table>');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -