3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.tree.TreeSorter"></div>/**
\r
15 * @class Ext.tree.TreeSorter
\r
16 * Provides sorting of nodes in a {@link Ext.tree.TreePanel}. The TreeSorter automatically monitors events on the
\r
17 * associated TreePanel that might affect the tree's sort order (beforechildrenrendered, append, insert and textchange).
\r
18 * Example usage:<br />
\r
20 new Ext.tree.TreeSorter(myTree, {
\r
23 sortType: function(node) {
\r
24 // sort by a custom, typed attribute:
\r
25 return parseInt(node.id, 10);
\r
30 * @param {TreePanel} tree
\r
31 * @param {Object} config
\r
33 Ext.tree.TreeSorter = function(tree, config){
\r
34 <div id="cfg-Ext.tree.TreeSorter-folderSort"></div>/**
\r
35 * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
\r
37 <div id="cfg-Ext.tree.TreeSorter-property"></div>/**
\r
38 * @cfg {String} property The named attribute on the node to sort by (defaults to "text"). Note that this
\r
39 * property is only used if no {@link #sortType} function is specified, otherwise it is ignored.
\r
41 <div id="cfg-Ext.tree.TreeSorter-dir"></div>/**
\r
42 * @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")
\r
44 <div id="cfg-Ext.tree.TreeSorter-leafAttr"></div>/**
\r
45 * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")
\r
47 <div id="cfg-Ext.tree.TreeSorter-caseSensitive"></div>/**
\r
48 * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
\r
50 <div id="cfg-Ext.tree.TreeSorter-sortType"></div>/**
\r
51 * @cfg {Function} sortType A custom "casting" function used to convert node values before sorting. The function
\r
52 * will be called with a single parameter (the {@link Ext.tree.TreeNode} being evaluated) and is expected to return
\r
53 * the node's sort value cast to the specific data type required for sorting. This could be used, for example, when
\r
54 * a node's text (or other attribute) should be sorted as a date or numeric value. See the class description for
\r
55 * example usage. Note that if a sortType is specified, any {@link #property} config will be ignored.
\r
58 Ext.apply(this, config);
\r
59 tree.on("beforechildrenrendered", this.doSort, this);
\r
60 tree.on("append", this.updateSort, this);
\r
61 tree.on("insert", this.updateSort, this);
\r
62 tree.on("textchange", this.updateSortParent, this);
\r
64 var dsc = this.dir && this.dir.toLowerCase() == "desc";
\r
65 var p = this.property || "text";
\r
66 var sortType = this.sortType;
\r
67 var fs = this.folderSort;
\r
68 var cs = this.caseSensitive === true;
\r
69 var leafAttr = this.leafAttr || 'leaf';
\r
71 this.sortFn = function(n1, n2){
\r
73 if(n1.attributes[leafAttr] && !n2.attributes[leafAttr]){
\r
76 if(!n1.attributes[leafAttr] && n2.attributes[leafAttr]){
\r
80 var v1 = sortType ? sortType(n1) : (cs ? n1.attributes[p] : n1.attributes[p].toUpperCase());
\r
81 var v2 = sortType ? sortType(n2) : (cs ? n2.attributes[p] : n2.attributes[p].toUpperCase());
\r
83 return dsc ? +1 : -1;
\r
85 return dsc ? -1 : +1;
\r
92 Ext.tree.TreeSorter.prototype = {
\r
93 doSort : function(node){
\r
94 node.sort(this.sortFn);
\r
97 compareNodes : function(n1, n2){
\r
98 return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);
\r
101 updateSort : function(tree, node){
\r
102 if(node.childrenRendered){
\r
103 this.doSort.defer(1, this, [node]);
\r
107 updateSortParent : function(node){
\r
108 var p = node.parentNode;
\r
109 if(p && p.childrenRendered){
\r
110 this.doSort.defer(1, this, [p]);
\r