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