provide installation instructions
[extjs.git] / examples / tree / XmlTreeLoader.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.ux.XmlTreeLoader\r
11  * @extends Ext.tree.TreeLoader\r
12  * <p>A TreeLoader that can convert an XML document into a hierarchy of {@link Ext.tree.TreeNode}s.\r
13  * Any text value included as a text node in the XML will be added to the parent node as an attribute\r
14  * called <tt>innerText</tt>.  Also, the tag name of each XML node will be added to the tree node as\r
15  * an attribute called <tt>tagName</tt>.</p>\r
16  * <p>By default, this class expects that your source XML will provide the necessary attributes on each \r
17  * node as expected by the {@link Ext.tree.TreePanel} to display and load properly.  However, you can\r
18  * provide your own custom processing of node attributes by overriding the {@link #processNode} method\r
19  * and modifying the attributes as needed before they are used to create the associated TreeNode.</p>\r
20  * @constructor\r
21  * Creates a new XmlTreeloader.\r
22  * @param {Object} config A config object containing config properties.\r
23  */\r
24 Ext.ux.XmlTreeLoader = Ext.extend(Ext.tree.TreeLoader, {\r
25     /**\r
26      * @property  XML_NODE_ELEMENT\r
27      * XML element node (value 1, read-only)\r
28      * @type Number\r
29      */\r
30     XML_NODE_ELEMENT : 1,\r
31     /**\r
32      * @property  XML_NODE_TEXT\r
33      * XML text node (value 3, read-only)\r
34      * @type Number\r
35      */\r
36     XML_NODE_TEXT : 3,\r
37     \r
38     // private override\r
39     processResponse : function(response, node, callback){\r
40         var xmlData = response.responseXML;\r
41         var root = xmlData.documentElement || xmlData;\r
42         \r
43         try{\r
44             node.beginUpdate();\r
45             node.appendChild(this.parseXml(root));\r
46             node.endUpdate();\r
47             \r
48             if(typeof callback == "function"){\r
49                 callback(this, node);\r
50             }\r
51         }catch(e){\r
52             this.handleFailure(response);\r
53         }\r
54     },\r
55     \r
56     // private\r
57     parseXml : function(node) {\r
58         var nodes = [];\r
59         Ext.each(node.childNodes, function(n){\r
60             if(n.nodeType == this.XML_NODE_ELEMENT){\r
61                 var treeNode = this.createNode(n);\r
62                 if(n.childNodes.length > 0){\r
63                     var child = this.parseXml(n);\r
64                     if(typeof child == 'string'){\r
65                         treeNode.attributes.innerText = child;\r
66                     }else{\r
67                         treeNode.appendChild(child);\r
68                     }\r
69                 }\r
70                 nodes.push(treeNode);\r
71             }\r
72             else if(n.nodeType == this.XML_NODE_TEXT){\r
73                 var text = n.nodeValue.trim();\r
74                 if(text.length > 0){\r
75                     return nodes = text;\r
76                 }\r
77             }\r
78         }, this);\r
79         \r
80         return nodes;\r
81     },\r
82     \r
83     // private override\r
84     createNode : function(node){\r
85         var attr = {\r
86             tagName: node.tagName\r
87         };\r
88         \r
89         Ext.each(node.attributes, function(a){\r
90             attr[a.nodeName] = a.nodeValue;\r
91         });\r
92         \r
93         this.processAttributes(attr);\r
94         \r
95         return Ext.ux.XmlTreeLoader.superclass.createNode.call(this, attr);\r
96     },\r
97     \r
98     /*\r
99      * Template method intended to be overridden by subclasses that need to provide\r
100      * custom attribute processing prior to the creation of each TreeNode.  This method\r
101      * will be passed a config object containing existing TreeNode attribute name/value\r
102      * pairs which can be modified as needed directly (no need to return the object).\r
103      */\r
104     processAttributes: Ext.emptyFn\r
105 });\r