3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.tree.TreeSorter"></div>/**
16 * @class Ext.tree.TreeSorter
17 * Provides sorting of nodes in a {@link Ext.tree.TreePanel}. The TreeSorter automatically monitors events on the
18 * associated TreePanel that might affect the tree's sort order (beforechildrenrendered, append, insert and textchange).
19 * Example usage:<br />
21 new Ext.tree.TreeSorter(myTree, {
24 sortType: function(node) {
25 // sort by a custom, typed attribute:
26 return parseInt(node.id, 10);
31 * @param {TreePanel} tree
32 * @param {Object} config
34 Ext.tree.TreeSorter = Ext.extend(Object, {
36 constructor: function(tree, config){
37 <div id="cfg-Ext.tree.TreeSorter-folderSort"></div>/**
38 * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
40 <div id="cfg-Ext.tree.TreeSorter-property"></div>/**
41 * @cfg {String} property The named attribute on the node to sort by (defaults to "text"). Note that this
42 * property is only used if no {@link #sortType} function is specified, otherwise it is ignored.
44 <div id="cfg-Ext.tree.TreeSorter-dir"></div>/**
45 * @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")
47 <div id="cfg-Ext.tree.TreeSorter-leafAttr"></div>/**
48 * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")
50 <div id="cfg-Ext.tree.TreeSorter-caseSensitive"></div>/**
51 * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
53 <div id="cfg-Ext.tree.TreeSorter-sortType"></div>/**
54 * @cfg {Function} sortType A custom "casting" function used to convert node values before sorting. The function
55 * will be called with a single parameter (the {@link Ext.tree.TreeNode} being evaluated) and is expected to return
56 * the node's sort value cast to the specific data type required for sorting. This could be used, for example, when
57 * a node's text (or other attribute) should be sorted as a date or numeric value. See the class description for
58 * example usage. Note that if a sortType is specified, any {@link #property} config will be ignored.
61 Ext.apply(this, config);
64 beforechildrenrendered: this.doSort,
65 append: this.updateSort,
66 insert: this.updateSort,
67 textchange: this.updateSortParent
70 var desc = this.dir && this.dir.toLowerCase() == 'desc',
71 prop = this.property || 'text';
72 sortType = this.sortType;
73 folderSort = this.folderSort;
74 caseSensitive = this.caseSensitive === true;
75 leafAttr = this.leafAttr || 'leaf';
77 if(Ext.isString(sortType)){
78 sortType = Ext.data.SortTypes[sortType];
80 this.sortFn = function(n1, n2){
81 var attr1 = n1.attributes,
82 attr2 = n2.attributes;
85 if(attr1[leafAttr] && !attr2[leafAttr]){
88 if(!attr1[leafAttr] && attr2[leafAttr]){
92 var prop1 = attr1[prop],
94 v1 = sortType ? sortType(prop1) : (caseSensitive ? prop1 : prop1.toUpperCase());
95 v2 = sortType ? sortType(prop2) : (caseSensitive ? prop2 : prop2.toUpperCase());
100 return desc ? -1 : 1;
106 doSort : function(node){
107 node.sort(this.sortFn);
110 updateSort : function(tree, node){
111 if(node.childrenRendered){
112 this.doSort.defer(1, this, [node]);
116 updateSortParent : function(node){
117 var p = node.parentNode;
118 if(p && p.childrenRendered){
119 this.doSort.defer(1, this, [p]);