0

0

xtree.js 代码_javascript技巧

php中文网

php中文网

发布时间:2016-05-16 19:16:33

|

1183人浏览过

|

来源于php中文网

原创

复制代码 代码如下:

//** 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();
}

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法

本专题汇总了包子漫画官网和网页版入口,提供最新章节抢先看方法、正版免费阅读指南,以及稳定访问方式,帮助用户快速直达包子漫画页面,无广告畅享全集漫画内容。

38

2026.02.10

MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法
MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法

本专题汇总了MC.JS官网入口和网页版快速畅玩方法,提供免安装访问、不同版本(1.8.8、1.12.8)在线体验指南,以及正版网页端操作说明,帮助玩家轻松进入MC.JS世界,实现即时畅玩与高效体验。

24

2026.02.10

谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程
谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程

本专题汇总了谷歌邮箱网页版的最新登录入口和注册方法,详细提供官方账号快速访问方式、网页版操作教程及安全登录技巧,帮助用户轻松管理Gmail邮箱账户,实现高效、安全的邮箱使用体验。

20

2026.02.10

铁路12306订票与退改全攻略_高效购票与座位选取技巧
铁路12306订票与退改全攻略_高效购票与座位选取技巧

本专题全面汇总铁路12306订票、退票、改签及候补订单操作技巧,提供车厢座位分布参考、抢票攻略和高铁安检注意事项,帮助新手用户快速掌握高效购票与退改流程,提高出行效率和体验。

15

2026.02.10

TensorFlow2深度学习模型实战与优化
TensorFlow2深度学习模型实战与优化

本专题面向 AI 与数据科学开发者,系统讲解 TensorFlow 2 框架下深度学习模型的构建、训练、调优与部署。内容包括神经网络基础、卷积神经网络、循环神经网络、优化算法及模型性能提升技巧。通过实战项目演示,帮助开发者掌握从模型设计到上线的完整流程。

0

2026.02.10

Vue3组合式API与组件开发实战
Vue3组合式API与组件开发实战

本专题讲解 Vue 3 组合式 API 的核心概念与应用技巧,深入分析响应式系统、生命周期管理、组件设计与复用策略。通过完整项目案例,指导前端开发者实现高性能、结构清晰的 Vue 应用,提升开发效率与代码可维护性。

4

2026.02.10

Go语言微服务架构与gRPC实战
Go语言微服务架构与gRPC实战

本专题面向有 Go 基础的开发者,系统讲解微服务架构设计与 gRPC 的高效应用。内容涵盖服务拆分、RPC 通信、负载均衡、错误处理、服务注册与发现等关键技术。通过实战案例,帮助开发者搭建高性能、可扩展的 Go 微服务系统。

1

2026.02.10

React 18状态管理与Hooks高级实践
React 18状态管理与Hooks高级实践

本专题专注于 React 18 的高级开发技术,详细讲解 useState、useEffect、useReducer、useContext 等 Hooks 的使用技巧,以及 Redux、Zustand 等状态管理工具的集成与优化方法。通过真实案例,帮助前端开发者构建可维护、性能优良的现代 React 应用。

4

2026.02.10

Node.js后端开发与Express框架实践
Node.js后端开发与Express框架实践

本专题针对初中级 Node.js 开发者,系统讲解如何使用 Express 框架搭建高性能后端服务。内容包括路由设计、中间件开发、数据库集成、API 安全与异常处理,以及 RESTful API 的设计与优化。通过实际项目演示,帮助开发者快速掌握 Node.js 后端开发流程。

2

2026.02.10

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Git 教程
Git 教程

共21课时 | 3.5万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.5万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 3.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号