Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / TreeSorter.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js"><div id="cls-Ext.tree.TreeSorter"></div>/**\r
9  * @class Ext.tree.TreeSorter\r
10  * Provides sorting of nodes in a {@link Ext.tree.TreePanel}.  The TreeSorter automatically monitors events on the \r
11  * associated TreePanel that might affect the tree's sort order (beforechildrenrendered, append, insert and textchange).\r
12  * Example usage:<br />\r
13  * <pre><code>\r
14 new Ext.tree.TreeSorter(myTree, {\r
15     folderSort: true,\r
16     dir: "desc",\r
17     sortType: function(node) {\r
18         // sort by a custom, typed attribute:\r
19         return parseInt(node.id, 10);\r
20     }\r
21 });\r
22 </code></pre>\r
23  * @constructor\r
24  * @param {TreePanel} tree\r
25  * @param {Object} config\r
26  */\r
27 Ext.tree.TreeSorter = function(tree, config){\r
28     <div id="cfg-Ext.tree.TreeSorter-folderSort"></div>/**\r
29      * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)\r
30      */\r
31     <div id="cfg-Ext.tree.TreeSorter-property"></div>/** \r
32      * @cfg {String} property The named attribute on the node to sort by (defaults to "text").  Note that this \r
33      * property is only used if no {@link #sortType} function is specified, otherwise it is ignored.\r
34      */\r
35     <div id="cfg-Ext.tree.TreeSorter-dir"></div>/** \r
36      * @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")\r
37      */\r
38     <div id="cfg-Ext.tree.TreeSorter-leafAttr"></div>/** \r
39      * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")\r
40      */\r
41     <div id="cfg-Ext.tree.TreeSorter-caseSensitive"></div>/** \r
42      * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)\r
43      */\r
44     <div id="cfg-Ext.tree.TreeSorter-sortType"></div>/** \r
45      * @cfg {Function} sortType A custom "casting" function used to convert node values before sorting.  The function\r
46      * will be called with a single parameter (the {@link Ext.tree.TreeNode} being evaluated) and is expected to return\r
47      * the node's sort value cast to the specific data type required for sorting.  This could be used, for example, when\r
48      * a node's text (or other attribute) should be sorted as a date or numeric value.  See the class description for \r
49      * example usage.  Note that if a sortType is specified, any {@link #property} config will be ignored.\r
50      */\r
51     \r
52     Ext.apply(this, config);\r
53     tree.on("beforechildrenrendered", this.doSort, this);\r
54     tree.on("append", this.updateSort, this);\r
55     tree.on("insert", this.updateSort, this);\r
56     tree.on("textchange", this.updateSortParent, this);\r
57     \r
58     var dsc = this.dir && this.dir.toLowerCase() == "desc";\r
59     var p = this.property || "text";\r
60     var sortType = this.sortType;\r
61     var fs = this.folderSort;\r
62     var cs = this.caseSensitive === true;\r
63     var leafAttr = this.leafAttr || 'leaf';\r
64 \r
65     this.sortFn = function(n1, n2){\r
66         if(fs){\r
67             if(n1.attributes[leafAttr] && !n2.attributes[leafAttr]){\r
68                 return 1;\r
69             }\r
70             if(!n1.attributes[leafAttr] && n2.attributes[leafAttr]){\r
71                 return -1;\r
72             }\r
73         }\r
74         var v1 = sortType ? sortType(n1) : (cs ? n1.attributes[p] : n1.attributes[p].toUpperCase());\r
75         var v2 = sortType ? sortType(n2) : (cs ? n2.attributes[p] : n2.attributes[p].toUpperCase());\r
76         if(v1 < v2){\r
77                         return dsc ? +1 : -1;\r
78                 }else if(v1 > v2){\r
79                         return dsc ? -1 : +1;\r
80         }else{\r
81                 return 0;\r
82         }\r
83     };\r
84 };\r
85 \r
86 Ext.tree.TreeSorter.prototype = {\r
87     doSort : function(node){\r
88         node.sort(this.sortFn);\r
89     },\r
90     \r
91     compareNodes : function(n1, n2){\r
92         return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);\r
93     },\r
94     \r
95     updateSort : function(tree, node){\r
96         if(node.childrenRendered){\r
97             this.doSort.defer(1, this, [node]);\r
98         }\r
99     },\r
100     \r
101     updateSortParent : function(node){\r
102                 var p = node.parentNode;\r
103                 if(p && p.childrenRendered){\r
104             this.doSort.defer(1, this, [p]);\r
105         }\r
106     }\r
107 };</pre>    \r
108 </body>\r
109 </html>