📄 tv20.js
字号:
//定义一个数组,nodeitems,所有nodeitem的容器
nodeitems = new Array() ;
//根节点
tv_topnodeitem = null ;
//====================================================================================================================
//函数,添加一个节点,
//参数1:父节点ID,
//参数2:节点ID
//参数3:节点内容
//参数4:节点选用图片
function addNode(parentkey , key , lable , img){
return new nodeitem( parentkey , key , lable , img ) ;
}
//====================================================================================================================
// 显示树,调用根结点的show方法
function showTV() {
tv_topnodeitem.show() ;
}
//====================================================================================================================
//得到父节点
function findNode( key ) {
pppp = null;
//遍历所有的nodeitem,如果某一个节点不为空并且key等于传进来的key值,返回改节点
for( i = 0 ; i < nodeitems.length ; i ++ ) {
if( nodeitems[ i ] != null ) {
if( nodeitems[ i ].key == key ) {
pppp = nodeitems[ i ] ;
}
}
}
return pppp ;
}
//=========================================================================================================
//定义类:nodeitem
//lable:内容
//key:ID
//parent:父ID
//img:图片
//subitems:子节点,是数组
//questionId:
//description:
//status------------------------1:two-direction 0:nobox 0: disactivite
// 2:three-0direction 1:close-box 1: activite
// 2:open-box
//一个function就是一个类。当你编写如下function时,其实就是定义了一个类,该function就是它的构造函数。
function nodeitem( parentkey , key , lable , img ) {
this.lable = lable ;
this.key = key ;
this.parent = findNode( parentkey ) ;
if( this.parent != null ) {
aa = this.parent.status ;
if( aa.substring( 1 , 2 ) == "0" )
this.parent.status = aa.substring( 0 , 1 ) + "1" + aa.substring( 2 , 3 ) ;
if( this.parent.maxsubitem != null )
this.parent.maxsubitem.status = "2" + this.parent.maxsubitem.status.substring( 1 , 3 ) ;
this.parent.subitems[ this.parent.subitems.length ] = this ;
this.parent.maxsubitem = this ;
}
else {
if( tv_topnodeitem != null ) {
alert( "不能有两个顶项!" ) ;
return ;
}
tv_topnodeitem = this ;
}
this.img = img ;
this.tag = null ;
this.status = "100" ;
this.subitems = new Array() ;
this.maxsubitem = null ;
//设置id号
this.id = nodeitemRegister( this ) ;
//**********************
this.questionId = 0;
this.description = "";
//this.url = null;
//**********************
//added by msb for the sort and move up/down
/*if ( this == tv_topnodeitem )
{
this.nodeIndex = 0;
} else {
this.nodeIndex = this.parent.subitems.length;
}*/
//end added
}
//added by msb for the move up/down
nodeitem.prototype.moveUp = nodeitem_moveUp;
nodeitem.prototype.moveDown = nodeitem_moveDown;
//end added
//如何在Javascript实现OO编程?恐怕最好的方式就是充分利用prototype属性
//当你用prototype编写一个类后,当你new一个新的object,浏览器会自动把prototype中的内容替你附加在object上。
//这样,通过利用prototype,你也就实现了类似OO的Javascript
//定义show方法
nodeitem.prototype.show = nodeitem_show ;
nodeitem.prototype.refresh = nodeitem_refresh ;
//boxclick()方法,对应nodeitem_boxclick()函数
nodeitem.prototype.boxclick = nodeitem_boxclick ;
nodeitem.prototype.close = nodeitem_close ;
nodeitem.prototype.open = nodeitem_open ;
nodeitem.prototype.remove = nodeitem_remove ;
nodeitem.prototype.setTag = nodeitem_setTag ;
nodeitem.prototype.getTag = nodeitem_getTag ;
//=========================================
//各种图片
//=========================================
//空白
treeview_box_0_none = "images/4_clos.gif" ;
//只有竖线
treeview_box_0_line = "images/4_none.gif" ;
//三个方向有竖线
treeview_box_2_open = "images/2_open.gif" ;
treeview_box_2_none = "images/2_none.gif" ;
treeview_box_2_close = "images/2_clos.gif" ;
//两个方向有竖线
treeview_box_1_open = "images/3_open.gif" ;
treeview_box_1_none = "images/3_none.gif" ;
treeview_box_1_close = "images/3_clos.gif" ;
function nodeitem_show() {
//<span id = \"preface0\"><table border='0' cellspacing='0' cellpadding='0'><tr><td>
//每个Node一个span.
str = "<span id = \"preface" + this.id + "\"><table border='1' cellspacing='0' cellpadding='0'><tr><td>" ;
str_f = "" ;
//
//如果有父节点,缩进
for( j = this.parent ; j != null ; j = j.parent ) {
//alert(j.status);
//显示空白
if( j.status.substring( 0 , 1 ) == 1 )
str_f = "<img src = '" + treeview_box_0_none + "' align='absmiddle'>" + str_f ;
//只显示竖线
else
str_f = "<img src = '" + treeview_box_0_line + "' align='absmiddle'>" + str_f ;
}
str = str + str_f ;
//判断状态码头两位,决定显示加号还是减号
str += "<img id = 'box" + this.id + "' nodeid = '" + this.id + "' src = '" ;
switch( this.status.substring( 0 , 2 ) ) {
case "10" : str += treeview_box_1_none ; break ;
case "11" : str += treeview_box_1_close ; break ;
case "12" : str += treeview_box_1_open ; break ;
case "20" : str += treeview_box_2_none ; break ;
case "21" : str += treeview_box_2_close ; break ;
case "22" : str += treeview_box_2_open ; break ;
}
//当单击事件发生,调用box_on_click,把自己传进去
str += "' align='absmiddle' onclick='box_on_click(this)'>" ;
//如果没有前导图片,就不显示
if( this.img == "" )
str += this.img ;
//否则,显示前导图片
else
str += "<img src = '" + this.img + "' align='absmiddle' width='16' height='16'>" ;
str += "</td><td><table border='0' cellspacing='1' cellpadding='1' style='font-size:9pt; color:#333333' id='lablePanel" + this.id + "'><tr><td ondblclick = 'lable_on_dblclick(" + this.id + ")' onclick='lable_on_click(" + this.id + ")' style='cursor:hand' id='f_lablePanel" + this.id + "' nowrap>" + this.lable + "</td></tr></table></td></tr></table>" ;
//显示下一个SPAN,id=tv_panel_0
str += "</span><span id = 'tv_panel_" + this.id + "' style='display:" ;
//如果是打开状态,style="DISPLAY: "
if( this.status.substring( 1 , 2 ) == '2' )
str += "" ;
//否则style="DISPLAY: none"
else
str += "none" ;
str += "'></span>" ;
//如果上面没有父亲了,找到BODY标签,再BODY标签刚刚开始的位置,加入这个str
if( this.parent == null )
for(var i in document.all){
if (document.all[i].tagName == "BODY")
{
document.all[i].insertAdjacentHTML( "AfterBegin" , str ) ;
break
}
}
else
document.all( "tv_panel_" + this.parent.id ).insertAdjacentHTML( "BeforeEnd" , str ) ;
//如果有儿子,循环每一个儿子
for( m = 0 ; m < this.subitems.length ; m ++ ) {
if( this.subitems[ m ] != null ) {
//把这个儿子放到栈里面
userstack.put( m ) ;
//调用这个儿子的栈方法
this.subitems[ m ].show() ;
m = userstack.get() ;
//alert(m);
}
}
}
//===========================================================
// 当单击事件发生,调用本方法,
//本方法调用类nodeitem的boxclick()方法
//===========================================================
function box_on_click( obj ) {
nodeitems[ obj.nodeid ].boxclick() ;
}
//当点击事件发生,调用本函数
function nodeitem_boxclick() {
//如果没有加号或者减号,直接返回
if( this.status.substring( 1 ,2 ) == "0" )
return ;
//如果是关闭的状态,则打开
if( this.status.substring( 1 ,2 ) == "1" )
this.open() ;
//如果是打开的状态,则关闭
else
this.close() ;
}
//关闭节点
function nodeitem_close() {
//更新自己的状态
this.status = this.status.substring( 0 , 1 ) + "1" + this.status.substring( 2 , 3 ) ;
//
document.all( "tv_panel_" + this.id ).style.display = "none" ;
eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_close" ) ;
}
//打开节点
function nodeitem_open() {
this.status = this.status.substring( 0 , 1 ) + "2" + this.status.substring( 2 , 3 ) ;
document.all( "tv_panel_" + this.id ).style.display = "" ;
eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_open" ) ;
}
//向nodeitems数组中加入obj
function nodeitemRegister( obj ) {
//nodeitems数组中加入obj
nodeitems[ nodeitems.length ] = obj ;
//返回id号
return nodeitems.length - 1 ;
}
//=================================
//定义一个类stack
//metheds : get()
// put( obj )
//=================================
function stack() {
this.value = new Array() ;
this.cursor = 0 ;
}
function stack_get() {
this.cursor = this.cursor - 1 ;
return this.value[ this.cursor ] ;
}
function stack_put( obj ) {
this.value[ this.cursor ] = obj ;
this.cursor = this.cursor + 1 ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -