Xtree. js Code

Source: Internet
Author: User

Copy codeThe Code is as follows: // ** 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.tar get = 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 ("");
EA. innerHTML = this. text;
EA.tar get = this.tar get;
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. length; 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 <this. indent. length; 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. getpreviussibling ();
// 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 <this. childNodes. length; j ++) {this. childNodes [j]. reload (1 );}
For (var I = 0; I <this. indent. length; 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. length; I ++)
This. childNodes [I]. expandAll ();
}

TreeItem. prototype. collapseChildren = function ()
{
For (var I = 0; I <this. childNodes. length; I ++)
This. childNodes [I]. collapseAll ()
}

TreeItem. prototype. openURL = function ()
{
If (this. action! = Global. defaultAction ){
Publish publish open(this.action,this.tar get );
}
}

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. getpreviussibling = function ()
{
If (! This. parent) return null;
For (var I = 0; I <this. parent. childNodes. length; 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 <this. parent. childNodes. length; 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. getpreviussibling ();
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 <this. length; 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 <oItem. childNodes. length; 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 xmlitem1_oitem.parseelement(xmlhttp.responsexml.doc umentElement );
If (XmlItem. childNodes. length = 0) {oLoad. setText ("unavaible ")}
Else
{
OItem. addNodesLoop (XmlItem );
For (var I = 0; I <oItem. childNodes. length; 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 x 24x60*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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.