You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.0 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. Ext.ns('Ext.ux.tree');
  16. /**
  17. * @class Ext.ux.tree.XmlTreeLoader
  18. * @extends Ext.tree.TreeLoader
  19. * <p>A TreeLoader that can convert an XML document into a hierarchy of {@link Ext.tree.TreeNode}s.
  20. * Any text value included as a text node in the XML will be added to the parent node as an attribute
  21. * called <tt>innerText</tt>. Also, the tag name of each XML node will be added to the tree node as
  22. * an attribute called <tt>tagName</tt>.</p>
  23. * <p>By default, this class expects that your source XML will provide the necessary attributes on each
  24. * node as expected by the {@link Ext.tree.TreePanel} to display and load properly. However, you can
  25. * provide your own custom processing of node attributes by overriding the {@link #processNode} method
  26. * and modifying the attributes as needed before they are used to create the associated TreeNode.</p>
  27. * @constructor
  28. * Creates a new XmlTreeloader.
  29. * @param {Object} config A config object containing config properties.
  30. */
  31. Ext.ux.tree.XmlTreeLoader = Ext.extend(Ext.tree.TreeLoader, {
  32. /**
  33. * @property XML_NODE_ELEMENT
  34. * XML element node (value 1, read-only)
  35. * @type Number
  36. */
  37. XML_NODE_ELEMENT : 1,
  38. /**
  39. * @property XML_NODE_TEXT
  40. * XML text node (value 3, read-only)
  41. * @type Number
  42. */
  43. XML_NODE_TEXT : 3,
  44. // private override
  45. processResponse : function(response, node, callback){
  46. var xmlData = response.responseXML,
  47. root = xmlData.documentElement || xmlData;
  48. try{
  49. node.beginUpdate();
  50. node.appendChild(this.parseXml(root));
  51. node.endUpdate();
  52. this.runCallback(callback, scope || node, [node]);
  53. }catch(e){
  54. this.handleFailure(response);
  55. }
  56. },
  57. // private
  58. parseXml : function(node) {
  59. var nodes = [];
  60. Ext.each(node.childNodes, function(n){
  61. if(n.nodeType == this.XML_NODE_ELEMENT){
  62. var treeNode = this.createNode(n);
  63. if(n.childNodes.length > 0){
  64. var child = this.parseXml(n);
  65. if(typeof child == 'string'){
  66. treeNode.attributes.innerText = child;
  67. }else{
  68. treeNode.appendChild(child);
  69. }
  70. }
  71. nodes.push(treeNode);
  72. }
  73. else if(n.nodeType == this.XML_NODE_TEXT){
  74. var text = n.nodeValue.trim();
  75. if(text.length > 0){
  76. return nodes = text;
  77. }
  78. }
  79. }, this);
  80. return nodes;
  81. },
  82. // private override
  83. createNode : function(node){
  84. var attr = {
  85. tagName: node.tagName
  86. };
  87. Ext.each(node.attributes, function(a){
  88. attr[a.nodeName] = a.nodeValue;
  89. });
  90. this.processAttributes(attr);
  91. return Ext.ux.tree.XmlTreeLoader.superclass.createNode.call(this, attr);
  92. },
  93. /*
  94. * Template method intended to be overridden by subclasses that need to provide
  95. * custom attribute processing prior to the creation of each TreeNode. This method
  96. * will be passed a config object containing existing TreeNode attribute name/value
  97. * pairs which can be modified as needed directly (no need to return the object).
  98. */
  99. processAttributes: Ext.emptyFn
  100. });
  101. //backwards compat
  102. Ext.ux.XmlTreeLoader = Ext.ux.tree.XmlTreeLoader;