3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
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 />
13 new Ext.tree.TreeSorter(myTree, {
16 sortType: function(node) {
17 // sort by a custom, typed attribute:
18 return parseInt(node.id, 10);
23 * @param {TreePanel} tree
24 * @param {Object} config
26 Ext.tree.TreeSorter = function(tree, config){
28 * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
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.
35 * @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")
38 * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")
41 * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
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.
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);
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';
64 this.sortFn = function(n1, n2){
66 if(n1.attributes[leafAttr] && !n2.attributes[leafAttr]){
69 if(!n1.attributes[leafAttr] && n2.attributes[leafAttr]){
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());
85 Ext.tree.TreeSorter.prototype = {
86 doSort : function(node){
87 node.sort(this.sortFn);
90 compareNodes : function(n1, n2){
91 return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);
94 updateSort : function(tree, node){
95 if(node.childrenRendered){
96 this.doSort.defer(1, this, [node]);
100 updateSortParent : function(node){
101 var p = node.parentNode;
102 if(p && p.childrenRendered){
103 this.doSort.defer(1, this, [p]);