3
* Copyright(c) 2006-2008, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.tree.TreeSorter
11
* Provides sorting of nodes in a {@link Ext.tree.TreePanel}. The TreeSorter automatically monitors events on the
12
* associated TreePanel that might affect the tree's sort order (beforechildrenrendered, append, insert and textchange).
13
* Example usage:<br />
15
new Ext.tree.TreeSorter(myTree, {
18
sortType: function(node) {
19
// sort by a custom, typed attribute:
20
return parseInt(node.id, 10);
25
* @param {TreePanel} tree
26
* @param {Object} config
28
Ext.tree.TreeSorter = function(tree, config){
30
* @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
33
* @cfg {String} property The named attribute on the node to sort by (defaults to "text"). Note that this
34
* property is only used if no {@link #sortType} function is specified, otherwise it is ignored.
37
* @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")
40
* @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")
43
* @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
46
* @cfg {Function} sortType A custom "casting" function used to convert node values before sorting. The function
47
* will be called with a single parameter (the {@link Ext.tree.TreeNode} being evaluated) and is expected to return
48
* the node's sort value cast to the specific data type required for sorting. This could be used, for example, when
49
* a node's text (or other attribute) should be sorted as a date or numeric value. See the class description for
50
* example usage. Note that if a sortType is specified, any {@link #property} config will be ignored.
53
Ext.apply(this, config);
54
tree.on("beforechildrenrendered", this.doSort, this);
55
tree.on("append", this.updateSort, this);
56
tree.on("insert", this.updateSort, this);
57
tree.on("textchange", this.updateSortParent, this);
59
var dsc = this.dir && this.dir.toLowerCase() == "desc";
60
var p = this.property || "text";
61
var sortType = this.sortType;
62
var fs = this.folderSort;
63
var cs = this.caseSensitive === true;
64
var leafAttr = this.leafAttr || 'leaf';
66
this.sortFn = function(n1, n2){
68
if(n1.attributes[leafAttr] && !n2.attributes[leafAttr]){
71
if(!n1.attributes[leafAttr] && n2.attributes[leafAttr]){
75
var v1 = sortType ? sortType(n1) : (cs ? n1.attributes[p] : n1.attributes[p].toUpperCase());
76
var v2 = sortType ? sortType(n2) : (cs ? n2.attributes[p] : n2.attributes[p].toUpperCase());
87
Ext.tree.TreeSorter.prototype = {
88
doSort : function(node){
89
node.sort(this.sortFn);
92
compareNodes : function(n1, n2){
93
return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);
96
updateSort : function(tree, node){
97
if(node.childrenRendered){
98
this.doSort.defer(1, this, [node]);
102
updateSortParent : function(node){
103
var p = node.parentNode;
104
if(p && p.childrenRendered){
105
this.doSort.defer(1, this, [p]);
b'\\ No newline at end of file'