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.

128 lines
5.0 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-tree-TreeSorter-method-constructor'><span id='Ext-tree-TreeSorter'>/**
  19. </span></span> * @class Ext.tree.TreeSorter
  20. * Provides sorting of nodes in a {@link Ext.tree.TreePanel}. The TreeSorter automatically monitors events on the
  21. * associated TreePanel that might affect the tree's sort order (beforechildrenrendered, append, insert and textchange).
  22. * Example usage:&lt;br /&gt;
  23. * &lt;pre&gt;&lt;code&gt;
  24. new Ext.tree.TreeSorter(myTree, {
  25. folderSort: true,
  26. dir: &quot;desc&quot;,
  27. sortType: function(value, node) {
  28. // sort by a custom, typed attribute:
  29. return parseInt(node.id, 10);
  30. }
  31. });
  32. &lt;/code&gt;&lt;/pre&gt;
  33. * @constructor
  34. * @param {TreePanel} tree
  35. * @param {Object} config
  36. */
  37. Ext.tree.TreeSorter = Ext.extend(Object, {
  38. constructor: function(tree, config){
  39. <span id='Ext-tree-TreeSorter-cfg-folderSort'> /**
  40. </span> * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
  41. */
  42. <span id='Ext-tree-TreeSorter-cfg-property'> /**
  43. </span> * @cfg {String} property The named attribute on the node to sort by (defaults to &quot;text&quot;). Note that this
  44. * property is only used if no {@link #sortType} function is specified, otherwise it is ignored.
  45. */
  46. <span id='Ext-tree-TreeSorter-cfg-dir'> /**
  47. </span> * @cfg {String} dir The direction to sort (&quot;asc&quot; or &quot;desc,&quot; case-insensitive, defaults to &quot;asc&quot;)
  48. */
  49. <span id='Ext-tree-TreeSorter-cfg-leafAttr'> /**
  50. </span> * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to &quot;leaf&quot;)
  51. */
  52. <span id='Ext-tree-TreeSorter-cfg-caseSensitive'> /**
  53. </span> * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
  54. */
  55. <span id='Ext-tree-TreeSorter-cfg-sortType'> /**
  56. </span> * @cfg {Function} sortType A custom &quot;casting&quot; function used to convert node values before sorting. The function
  57. * will be called with a two parameters, the {@link #property} value for the node and the {@link Ext.tree.TreeNode} being evaluated.
  58. * It is expected to return the node's sort value cast to the specific data type required for sorting. This could be used, for example, when
  59. * a node's text (or other attribute) should be sorted as a date or numeric value. See the class description for
  60. * example usage.
  61. */
  62. Ext.apply(this, config);
  63. tree.on({
  64. scope: this,
  65. beforechildrenrendered: this.doSort,
  66. append: this.updateSort,
  67. insert: this.updateSort,
  68. textchange: this.updateSortParent
  69. });
  70. var desc = this.dir &amp;&amp; this.dir.toLowerCase() == 'desc',
  71. prop = this.property || 'text',
  72. sortType = this.sortType,
  73. folderSort = this.folderSort,
  74. caseSensitive = this.caseSensitive === true,
  75. leafAttr = this.leafAttr || 'leaf';
  76. if(Ext.isString(sortType)){
  77. sortType = Ext.data.SortTypes[sortType];
  78. }
  79. this.sortFn = function(n1, n2){
  80. var attr1 = n1.attributes,
  81. attr2 = n2.attributes;
  82. if(folderSort){
  83. if(attr1[leafAttr] &amp;&amp; !attr2[leafAttr]){
  84. return 1;
  85. }
  86. if(!attr1[leafAttr] &amp;&amp; attr2[leafAttr]){
  87. return -1;
  88. }
  89. }
  90. var prop1 = attr1[prop],
  91. prop2 = attr2[prop],
  92. v1 = sortType ? sortType(prop1, n1) : (caseSensitive ? prop1 : prop1.toUpperCase()),
  93. v2 = sortType ? sortType(prop2, n2) : (caseSensitive ? prop2 : prop2.toUpperCase());
  94. if(v1 &lt; v2){
  95. return desc ? 1 : -1;
  96. }else if(v1 &gt; v2){
  97. return desc ? -1 : 1;
  98. }
  99. return 0;
  100. };
  101. },
  102. <span id='Ext-tree-TreeSorter-method-doSort'> doSort : function(node){
  103. </span> node.sort(this.sortFn);
  104. },
  105. <span id='Ext-tree-TreeSorter-method-updateSort'> updateSort : function(tree, node){
  106. </span> if(node.childrenRendered){
  107. this.doSort.defer(1, this, [node]);
  108. }
  109. },
  110. <span id='Ext-tree-TreeSorter-method-updateSortParent'> updateSortParent : function(node){
  111. </span> var p = node.parentNode;
  112. if(p &amp;&amp; p.childrenRendered){
  113. this.doSort.defer(1, this, [p]);
  114. }
  115. }
  116. });
  117. </pre>
  118. </body>
  119. </html>