4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.tree.TreeSorter
15 * Provides sorting of nodes in a TreePanel
17 * @cfg {Boolean} folderSort True to sort leaf nodes under non leaf nodes
18 * @cfg {String} property The named attribute on the node to sort by (defaults to text)
19 * @cfg {String} dir The direction to sort (asc or desc) (defaults to asc)
20 * @cfg {String} leafAttr The attribute used to determine leaf nodes in folder sort (defaults to "leaf")
21 * @cfg {Boolean} caseSensitive true for case sensitive sort (defaults to false)
22 * @cfg {Function} sortType A custom "casting" function used to convert node values before sorting
24 * @param {TreePanel} tree
25 * @param {Object} config
27 Roo.tree.TreeSorter = function(tree, config){
28 Roo.apply(this, config);
29 tree.on("beforechildrenrendered", this.doSort, this);
30 tree.on("append", this.updateSort, this);
31 tree.on("insert", this.updateSort, this);
33 var dsc = this.dir && this.dir.toLowerCase() == "desc";
34 var p = this.property || "text";
35 var sortType = this.sortType;
36 var fs = this.folderSort;
37 var cs = this.caseSensitive === true;
38 var leafAttr = this.leafAttr || 'leaf';
40 this.sortFn = function(n1, n2){
42 if(n1.attributes[leafAttr] && !n2.attributes[leafAttr]){
45 if(!n1.attributes[leafAttr] && n2.attributes[leafAttr]){
49 var v1 = sortType ? sortType(n1) : (cs ? n1.attributes[p] : n1.attributes[p].toUpperCase());
50 var v2 = sortType ? sortType(n2) : (cs ? n2.attributes[p] : n2.attributes[p].toUpperCase());
61 Roo.tree.TreeSorter.prototype = {
62 doSort : function(node){
63 node.sort(this.sortFn);
66 compareNodes : function(n1, n2){
67 return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);
70 updateSort : function(tree, node){
71 if(node.childrenRendered){
72 this.doSort.defer(1, this, [node]);