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