Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / examples / ux / XmlTreeLoader.js
1 /*!
2  * Ext JS Library 3.3.1
3  * Copyright(c) 2006-2010 Sencha Inc.
4  * licensing@sencha.com
5  * http://www.sencha.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             root = xmlData.documentElement || xmlData;
42
43         try{
44             node.beginUpdate();
45             node.appendChild(this.parseXml(root));
46             node.endUpdate();
47
48             this.runCallback(callback, scope || node, [node]);
49         }catch(e){
50             this.handleFailure(response);
51         }
52     },
53
54     // private
55     parseXml : function(node) {
56         var nodes = [];
57         Ext.each(node.childNodes, function(n){
58             if(n.nodeType == this.XML_NODE_ELEMENT){
59                 var treeNode = this.createNode(n);
60                 if(n.childNodes.length > 0){
61                     var child = this.parseXml(n);
62                     if(typeof child == 'string'){
63                         treeNode.attributes.innerText = child;
64                     }else{
65                         treeNode.appendChild(child);
66                     }
67                 }
68                 nodes.push(treeNode);
69             }
70             else if(n.nodeType == this.XML_NODE_TEXT){
71                 var text = n.nodeValue.trim();
72                 if(text.length > 0){
73                     return nodes = text;
74                 }
75             }
76         }, this);
77
78         return nodes;
79     },
80
81     // private override
82     createNode : function(node){
83         var attr = {
84             tagName: node.tagName
85         };
86
87         Ext.each(node.attributes, function(a){
88             attr[a.nodeName] = a.nodeValue;
89         });
90
91         this.processAttributes(attr);
92
93         return Ext.ux.tree.XmlTreeLoader.superclass.createNode.call(this, attr);
94     },
95
96     /*
97      * Template method intended to be overridden by subclasses that need to provide
98      * custom attribute processing prior to the creation of each TreeNode.  This method
99      * will be passed a config object containing existing TreeNode attribute name/value
100      * pairs which can be modified as needed directly (no need to return the object).
101      */
102     processAttributes: Ext.emptyFn
103 });
104
105 //backwards compat
106 Ext.ux.XmlTreeLoader = Ext.ux.tree.XmlTreeLoader;