3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.tree.TreeSorter"></div>/**
\r
9 * @class Ext.tree.TreeSorter
\r
10 * Provides sorting of nodes in a {@link Ext.tree.TreePanel}. The TreeSorter automatically monitors events on the
\r
11 * associated TreePanel that might affect the tree's sort order (beforechildrenrendered, append, insert and textchange).
\r
12 * Example usage:<br />
\r
14 new Ext.tree.TreeSorter(myTree, {
\r
17 sortType: function(node) {
\r
18 // sort by a custom, typed attribute:
\r
19 return parseInt(node.id, 10);
\r
24 * @param {TreePanel} tree
\r
25 * @param {Object} config
\r
27 Ext.tree.TreeSorter = function(tree, config){
\r
28 <div id="cfg-Ext.tree.TreeSorter-folderSort"></div>/**
\r
29 * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
\r
31 <div id="cfg-Ext.tree.TreeSorter-property"></div>/**
\r
32 * @cfg {String} property The named attribute on the node to sort by (defaults to "text"). Note that this
\r
33 * property is only used if no {@link #sortType} function is specified, otherwise it is ignored.
\r
35 <div id="cfg-Ext.tree.TreeSorter-dir"></div>/**
\r
36 * @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")
\r
38 <div id="cfg-Ext.tree.TreeSorter-leafAttr"></div>/**
\r
39 * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")
\r
41 <div id="cfg-Ext.tree.TreeSorter-caseSensitive"></div>/**
\r
42 * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
\r
44 <div id="cfg-Ext.tree.TreeSorter-sortType"></div>/**
\r
45 * @cfg {Function} sortType A custom "casting" function used to convert node values before sorting. The function
\r
46 * will be called with a single parameter (the {@link Ext.tree.TreeNode} being evaluated) and is expected to return
\r
47 * the node's sort value cast to the specific data type required for sorting. This could be used, for example, when
\r
48 * a node's text (or other attribute) should be sorted as a date or numeric value. See the class description for
\r
49 * example usage. Note that if a sortType is specified, any {@link #property} config will be ignored.
\r
52 Ext.apply(this, config);
\r
53 tree.on("beforechildrenrendered", this.doSort, this);
\r
54 tree.on("append", this.updateSort, this);
\r
55 tree.on("insert", this.updateSort, this);
\r
56 tree.on("textchange", this.updateSortParent, this);
\r
58 var dsc = this.dir && this.dir.toLowerCase() == "desc";
\r
59 var p = this.property || "text";
\r
60 var sortType = this.sortType;
\r
61 var fs = this.folderSort;
\r
62 var cs = this.caseSensitive === true;
\r
63 var leafAttr = this.leafAttr || 'leaf';
\r
65 this.sortFn = function(n1, n2){
\r
67 if(n1.attributes[leafAttr] && !n2.attributes[leafAttr]){
\r
70 if(!n1.attributes[leafAttr] && n2.attributes[leafAttr]){
\r
74 var v1 = sortType ? sortType(n1) : (cs ? n1.attributes[p] : n1.attributes[p].toUpperCase());
\r
75 var v2 = sortType ? sortType(n2) : (cs ? n2.attributes[p] : n2.attributes[p].toUpperCase());
\r
77 return dsc ? +1 : -1;
\r
79 return dsc ? -1 : +1;
\r
86 Ext.tree.TreeSorter.prototype = {
\r
87 doSort : function(node){
\r
88 node.sort(this.sortFn);
\r
91 compareNodes : function(n1, n2){
\r
92 return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);
\r
95 updateSort : function(tree, node){
\r
96 if(node.childrenRendered){
\r
97 this.doSort.defer(1, this, [node]);
\r
101 updateSortParent : function(node){
\r
102 var p = node.parentNode;
\r
103 if(p && p.childrenRendered){
\r
104 this.doSort.defer(1, this, [p]);
\r