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