您好,欢迎访问一九零五行业门户网

xtree.js 代码_javascript技巧

复制代码 代码如下:
//** powered by fason
//** email: fason_pfx@hotmail.com
var icon={
    root    :'image/root.gif',
    open    :'image/open.png',
    close    :'image/close.png',
    file    :'image/file.png',
    rplus    :'image/rplus.gif',
    rminus    :'image/rminus.gif',
    join    :'image/t.gif',
    joinbottom:'image/l.gif',
    plus    :'image/tplus.gif',
    plusbottom:'image/lplus.gif',
    minus    :'image/tminus.gif',
    minusbottom:'image/lminus.gif',
    blank    :'image/blank.gif',
    line    :'image/i.gif'
};
var global={
    id:0,
    getid:function(){return this.id++;},
    all:[],
    selecteditem:null,
    defaulttext:treeitem,
    defaultaction:javascript:void(0),
    defaulttarget:_blank
}
function preloadimage(){
    for(i in icon){
        var tem=icon[i];
        icon[i]=new image()
        icon[i].src=tem
    }
};preloadimage();
function treeitem(text,action,target,title,icon)
{
    this.id=global.getid();
    this.level=0;
    this.text=text?text:global.defaulttext;
    this.action=action?action:global.defaultaction;
    this.target=target?target:global.defaulttarget;
    this.title=title?title:this.text;
    this.islast=true;
    this.childnodes=new array();
    this.indent=new array();
    this.parent=null;
    var c =0; 
    if(getcookie(item+this.id) != null) c = getcookie(item+this.id);
    this.open=parseint(c);
    this.load=false;
    this.setuped=false;
    this.jsitem=null;
    this.container=document.createelement(div);
    this.icon=icon;
    global.all[global.all.length]=this;
}
treeitem.prototype.tostring = function()
{
    var o = this;
    var oitem = document.createelement(div);
    oitem.id = treeitem+this.id
    oitem.classname = treeitem;
    oitem.nowrap = true;
    oitem.onselectstart = function(){ return false;}
    oitem.oncontextmenu = function(){ return false;}
    this.jsitem = oitem;
    this.drawindents();
    var iicon = document.createelement(img);
    iicon.align = absmiddle;
    iicon.src = this.childnodes.length>0?(this.open?(this.level>0?(this.islast?icon.minusbottom.src:icon.minus.src):icon.rminus.src):(this.level>0?(this.islast?icon.plusbottom.src:icon.plus.src):icon.rplus.src)):(this.level>0?(this.islast?icon.joinbottom.src:icon.join.src):icon.blank.src);
    iicon.id = treeitem-icon-handle- + this.id;
    iicon.onclick = function(){ o.toggle();};
    oitem.appendchild(iicon);
    var iicon = document.createelement(img);
    iicon.align = absmiddle;
    iicon.src = this.icon?this.icon:(this.childnodes.length>0?(this.open?icon.open.src:icon.close.src):icon.file.src);
    iicon.id = treeitem-icon-folder- + this.id;
    iicon.onclick = function(){ o.select();};
    iicon.ondblclick = function(){ o.toggle();};
    oitem.appendchild(iicon);
    var etext = document.createelement(span);
    var ea=document.createelement(a);
    ea.innerhtml = this.text;
    ea.target = this.target;
    ea.href = this.action;
    ea.onkeydown = function(e){ return o.keydown(e);}
    if(this.action == global.defaultaction) ea.onclick = function(){ o.toggle(); return false;}
    etext.appendchild(ea);
    etext.id = treeitem-text- + this.id;
    etext.classname = treeitem-unselect
    etext.onclick = function(){ o.select(1);};
    etext.title = this.title;
    oitem.appendchild(etext);
    this.container.id = treeitem-container-+this.id;
    this.container.style.display = this.open?:none;
    oitem.appendchild(this.container);
    return oitem;
}
treeitem.prototype.root = function()
{
    var p = this;
    while(p.parent)
        p = p.parent;
    return p;
}
treeitem.prototype.settext = function(stext)
{
    if(this.root().setuped)
    {
        var oitem = document.getelementbyid(treeitem-text- + this.id);
        oitem.firstchild.innerhtml = stext;
    }
    this.text = stext;
}
treeitem.prototype.setindent = function(l,v)
{
    for(var i=0;i    {
        this.childnodes[i].indent[l] = v;
        this.childnodes[i].setindent(l,v);
    }
}
treeitem.prototype.drawindents = function()
{
    var oitem = this.jsitem;
    for(var i=0;i        var iicon = document.createelement(img);
        iicon.align = absmiddle;
        iicon.id = treeitem-icon- + this.id + - + i;
        iicon.src = this.indent[i]?icon.blank.src:icon.line.src;
        oitem.appendchild(iicon);
    }
}
treeitem.prototype.add = function(oitem)
{
    oitem.parent=this;
    this.childnodes[this.childnodes.length]=oitem;
    oitem.level=this.level+1;
    oitem.indent=this.indent.concat();
    oitem.indent[oitem.indent.length]=this.islast;
    if(this.childnodes.length>1){
        var o=this.childnodes[this.childnodes.length-2];
        o.islast=false;
        o.setindent(o.level,0);
        if(this.root().setuped)o.reload(1);
    }
    else if(this.root().setuped)
        this.reload(0);
    this.container.appendchild(oitem.tostring());
    this.container.style.display=this.open?:none;
}
treeitem.prototype.loadchildren = function()
{
    //do something
}
treeitem.prototype.remove = function()
{
    var tmp = this.getprevioussibling();
    //if(tmp){ tmp.select();}
    this.removechildren();
    var p = this.parent;
    if(!p){ return };
    if(p.childnodes.length>0){
        var o = p.childnodes[p.childnodes.length-1];
        o.islast = true;
        o.setindent(o.level,1);
        if(o.root().setuped)o.reload(1);
    }
    else
        p.reload();
}
treeitem.prototype.removechildren = function ()
{
    if(this == global.selecteditem){ global.selecteditem = null;}
    for(var i=this.childnodes.length-1;i>=0;i--)
        this.childnodes[i].removechildren();
    var o = this;
    var p = this.parent;
    if (p) { p.childnodes = p.childnodes._remove(o);}
    global.all[this.id] = null
    var oitem = document.getelementbyid(treeitem+this.id);
    if (oitem) { oitem.parentnode.removechild(oitem); }
}
treeitem.prototype.reload = function(flag)
{
    if (flag){
        for(var j=0;j        for(var i=0;i            document.getelementbyid(treeitem-icon- +this.id+ -+i).src = this.indent[i]?icon.blank.src:icon.line.src;
    }
    document.getelementbyid(treeitem-icon-handle- +this.id).src = this.childnodes.length>0?(this.open?(this.level>0?(this.islast?icon.minusbottom.src:icon.minus.src):icon.rminus.src):(this.level>0?(this.islast?icon.plusbottom.src:icon.plus.src):icon.rplus.src)):(this.level>0?(this.islast?icon.joinbottom.src:icon.join.src):icon.blank.src);
    if (!this.icon)
        document.getelementbyid(treeitem-icon-folder-+this.id).src = this.childnodes.length>0?(this.open?icon.open.src:icon.close.src):icon.file.src;
}
treeitem.prototype.toggle = function()
{
    if(this.childnodes.length>0){
        if(this.open)
            this.collapse();
        else
            this.expand();
    }
}
treeitem.prototype.expand = function()
{
    this.open=1;
    setcookie(item+this.id,1);
    if(!this.load){
        this.load=true;
        this.loadchildren();
        this.reload(1);
    }
    else 
        this.reload(0);
    this.container.style.display = ;
}
treeitem.prototype.collapse = function()
{
    this.open=0;
    setcookie(item+this.id,0);
    this.container.style.display = none;
    this.reload(0);
    this.select(1);
}
treeitem.prototype.expandall = function()
{
    if(this.childnodes.length>0 && !this.open)this.expand();
    this.expandchildren();
}
treeitem.prototype.collapseall = function()
{
    this.collapsechildren();
    if(this.childnodes.length>0 && this.open)this.collapse();
}
treeitem.prototype.expandchildren = function()
{
    for(var i=0;i    this.childnodes[i].expandall();
}
treeitem.prototype.collapsechildren = function()
{
    for(var i=0;i    this.childnodes[i].collapseall()
}
treeitem.prototype.openurl=function()
{
    if(this.action!=global.defaultaction){
        window.open(this.action,this.target);
    }
}
treeitem.prototype.select=function(o)
{
    if (global.selecteditem) global.selecteditem.unselect();
    var oitem = document.getelementbyid(treeitem-text- + this.id);
    oitem.classname = treeitem-selected;
    oitem.firstchild.focus();
    global.selecteditem = this;
    if(!o) this.openurl();
}
treeitem.prototype.unselect=function()
{
    var oitem = document.getelementbyid(treeitem-text- + this.id);
    oitem.classname = treeitem-unselect;
    oitem.firstchild.blur();
    global.selecteditem = null;
}
treeitem.prototype.setup = function(otaget)
{
    otaget.appendchild(this.tostring());
    this.setuped = true;
    if(this.childnodes.length>0 || this.open) this.expand();
}
/**********************************************/
/*
    arrow key event
*/
/**********************************************/
treeitem.prototype.getfirstchild = function()
{
    if(this.childnodes.length>0 && this.open)
        return this.childnodes[0];
    return this;
}
treeitem.prototype.getlastchild = function()
{
    if(this.childnodes.length>0 && this.open)
        return this.childnodes[this.childnodes.length-1].getlastchild();
    return this;
}
treeitem.prototype.getprevioussibling = function()
{
    if(!this.parent) return null;
    for(var i=0;i        if(this.parent.childnodes[i] == this)break;
    if(i == 0) 
        return this.parent;
    else
        return this.parent.childnodes[i-1].getlastchild();
}
treeitem.prototype.getnextsibling = function()
{
    if(!this.parent) return null;
    for(var i=0;i        if(this.parent.childnodes[i] == this)break;
    if(i == this.parent.childnodes.length-1)
        return this.parent.getnextsibling();
    else
        return this.parent.childnodes[i+1];
}
treeitem.prototype.keydown=function(e){
    var code,o;
    if(!e) e = window.event;
    code = e.which ? e.which : e.keycode;
    o = this;
    if(code == 37)
    {
        if(o.open) o.collapse();
        else
        {
            if(o.parent) o.parent.select();
        }
        return false;
    }
    else if(code == 38)
    {
        var tmp = o.getprevioussibling();
        if(tmp) tmp.select();
        return false;
    }
    else if(code == 39)
    {
        if(o.childnodes.length>0)
        {
            if(!o.open) o.expand();
            else
            {
                var tmp = o.getfirstchild();
                if(tmp) tmp.select();
            }
        }
        return false;
    }
    else if(code == 40)
    {
        if(o.open&&o.childnodes.length>0)o.getfirstchild().select();
        else
        {
            var tmp = o.getnextsibling();
            if(tmp) tmp.select();
        }
        return false;
    }
    else if(code == 13)
    {
        o.toggle();
        o.openurl();
        return false;
    }
    return true;
}
/*****************************************************/
array.prototype.indexof=function(o){
    for(var i=0;i        if(this[i]==o)return i;
    return -1;
}
array.prototype.removeat=function(i){
    return this.slice(0,i).concat(this.slice(i+1))
}
array.prototype._remove=function(o){
    var i=this.indexof(o);
    if(i!= -1) return this.removeat(i)
    return this
}
/*****************************************************/
/*****************************************************/
/*
    xtreeitem class
*/
/*****************************************************/
function xtreeitem(uid,text,action,target,title,icon,xml){
    this.uid=uid;
    this.base=treeitem;
    this.base(text,action,target,title,icon);
    this.xml=xml;
}
xtreeitem.prototype=new treeitem;
xtreeitem.prototype.parseelement=function(dom){
    return dom.selectsinglenode(/treenode);
}
xtreeitem.prototype.addnodesloop = function(oitem)
{
    for(var i=0;i    {
        var o = oitem.childnodes[i];
        var tmp = new xtreeitem(o.getattribute(id),o.getattribute(text),o.getattribute(href),o.getattribute(target),o.getattribute(title),o.getattribute(icon),o.getattribute('xml'));
        this.add(tmp);
        if(o.getattribute(xml)) tmp.add(new treeitem(loading...));
        else
        {
            tmp.load=true;
            tmp.addnodesloop(o);
        }
    }
}
xtreeitem.prototype.loadchildren=function()
{
    var oitem = this;
    var oload = oitem.childnodes[0];
    var xmlhttp = new activexobject(microsoft.xmlhttp);
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readystate==4){
            if(xmlhttp.status==200){
                if(xmlhttp.responsexml.xml == ){ oload.settext(unavaible1);return;}
                var xmlitem=oitem.parseelement(xmlhttp.responsexml.documentelement);
                if(xmlitem.childnodes.length == 0){ oload.settext(unavaible) }
                else
                {
                    oitem.addnodesloop(xmlitem);
                    for(var i=0;i                    {
if(parseint(getcookie(item+oitem.childnodes[i].id)) ==1)
                        { oitem.childnodes[i].expand();}
                    }
                    if(global.selecteditem == oitem.childnodes[0])oitem.select();
                    oload.remove();
                }
            }
            else{
                oload.settext(unavaible);
            }
            xmlhttp = null;
            oitem.select(1);
        }
    }
    try{
        xmlhttp.open(get,this.xml+(/\?/g.test(this.xml)?&:?)+temp=+math.random(),true);
        xmlhttp.send();
    }catch(e){ oload.settext(unavaible);}
}
xtreeitem.prototype.setup=function(otarget){
    this.add(new treeitem(loading...));
    otarget.appendchild(this.tostring());
    this.setuped=true;
    if(this.childnodes.length>0 || this.open) this.expand();
}
/*****************************************************/
function setcookie(name,value)
{
    var days = 7; 
    var exp  = new date();
    exp.settime(exp.gettime() + days*24*60*60*1000);
    document.cookie = name + =+ escape (value) + ;expires= + exp.togmtstring();
}
function getcookie(name)
{
    var arr = document.cookie.match(new regexp((^| )+name+=([^;]*)(;|$)));
    if(arr != null) return unescape(arr[2]); return null;
}
function delcookie(name)
{
    var exp = new date();
    exp.settime(exp.gettime() - 1);
    var cval=getcookie(name);
    if(cval!=null) document.cookie= name + =+cval+;expires=+exp.togmtstring();
}
其它类似信息

推荐信息