Jxtree object, read external XML file data, generate Tree function _javascript skill

Source: Internet
Author: User
/******************************************
*jxtree object, reading external XML file data, spanning tree
* @author Brull
* @email brull@163.com
* @date 2007-03-27
*******************************************/

/*
* @param the address of the XmlURL XML file
*/
var jxtree = function (XmlURL)
{
var result = new Array ();

/*****************************
* First define TreeNode abstract objects
*treenode Object Properties:
*id unique number that must be defined as a node attribute in the XML file
*level node hierarchy, starting with-1 (that is, the root node)
*_click node click, defined as node attribute in XML file [optional]
*islast is the last node in the hierarchy where this node is located
*parent_islast whether the parent node is the last node in the hierarchy where the parent node is located
How to *tohtml this node into HTML code
*******************************/
var TreeNode = function (node,level)
{
var parent_elements = Node.parentnode? (Node.parentNode.parentNode node.parentNode.parentNode.childNodes:null): null;
var elements = Node.parentnode? Node.parentNode.childNodes:null;
This.id = xmldom.getattribute (node, "id")? Xmldom.getattribute (node, "id"): "";
This.level = level of level;//nodes
This.islast = elements? ((Elements.item (elements.length-2) = = node) true:false): false;
This._click = xmldom.getattribute (node, ' click ')? Xmldom.getattribute (node, ' click '): "";
this.tohtml = Null;//function
}

/*****************************
*elementnode objects, inheriting from abstract objects TreeNode
* New properties:
*_nodename node Name
******************************/
var elementnode = function (node,level)
{
Treenode.apply (this,arguments);
This._nodename = xmldom.getattribute (node, "name")? Xmldom.getattribute (node, "name"): "";
this.tohtml = function () {
var result = "";
if (this.islast) result + = "<div><div class= ' Minus_bottom '";
else result = "<div><div class= ' minus '";
Result + = "id= '" +this.id+ "_join ' onclick=\" jxtree.changestate (' "+this.id+") \ "></div><div id=" "+ this.id+ "_folder ' class= ' folder_open ' ></div><span class= ' text ' onclick=\ '" +this._click+ "\" > "+this. _nodename+ "</span></div>";
return result;
}
}

/*****************************
*texnode objects, inheriting from abstract objects TreeNode
* property is the same as TreeNode
* New properties:
*_nodevalue node Value
******************************/
var textnode = function (node,level)
{
Treenode.apply (this,arguments);
This._nodevalue = Node.firstChild.nodeValue;
this.tohtml = function () {
var result = "";
if (this.islast) result + = "<div><div class= ' Join_bottom ' ></div>";
else result = "<div><div class= ' join ' ></div>";
Result + = "<div class= ' page ' ></div><span class= ' text ' id= '" +this.id + "_item ' onclick=\" Jxtree.setfocus (this.id); " +this._click+ ">" +this._nodevalue+ "</span></div>";
return result;
}
}

/**********node node build ends, start interpreting XML file ************/

var domroot=xmldom.loadxml (XmlURL). documentelement;//Loading XML files synchronously
var level = -1;//root node level
var stack = new Array (1);
Result.push ("<div><div class= ' root ' ></div><span class= ' text ' >" +xmldom.getattribute ( Domroot, "name") + "</span></div>");
HTML code that interprets the contents of an XML file as a tree state, recursively calls
This.parsexml = function (node) {
Stack.push (level);
level++;
var element = new Elementnode (node,level);
var elements = Node.childnodes;
If (level!= 0) {
if (element.islast)
Result.push ("<div id= '" +element.id+ "_body ' class= ' Body_empty ' >");
Else
Result.push ("<div id= '" +element.id+ "_body ' class= ' Body_line ' >");
}
for (Var i=0;i<elements.length;i++) {
if (Elements.item (i). nodename = = "Item") {//node as leaf
var textnode = new Textnode (Elements.item (i), level);
Result.push (textnode.tohtml ());
Textnode = null;//Release the object in time
}
else if (Elements.item (i). NodeType ==1) {//node as Branch
var elementnode = new Elementnode (Elements.item (i), level);
Result.push (elementnode.tohtml ());
Elementnode = null;//Release the object in time
This.parsexml (Elements.item (i));
}
}
If (level!= 0) Result.push ("</div>");
Level = Stack.pop ();
}

Get an explanation of the result and return
This.gettree = function () {
This.parsexml (Domroot);
Domroot = null;//Free DOM Object
Return Result.join ("");
}
/************* Static Properties ***************/
Jxtree.curtext = null;//ID of current text

/************ static Method ***************/
Jxtree.changestate = function (ID) {//Expand or Shrink node contents
var _body = document.getElementById (id + "_body");
var _join = document.getElementById (id+ "_join");
var folder = document.getElementById (id+ "_folder");
(_body.style.display = "None")? (
_body.style.display = "Block",
_join.classname = _join.classname.replace ("Plus", "minus"),
Folder.classname = "Folder_open"
) : (
_body.style.display = "None",
_join.classname = _join.classname.replace ("Minus", "plus"),
Folder.classname = "Folder_close"
)
};//changestate
Jxtree.setfocus = function (ID) {
if (Jxtree.curtext)
With (document.getElementById (jxtree.curtext). Style) {
BackgroundColor = "";
color = "#000";
}
With (document.getElementById (ID). style) {
BackgroundColor = "#003366";
color = "#FFF";
}
Jxtree.curtext = ID;
}
}

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.