~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/source/widgets/tree/TreeSorter.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 2.2
 
3
 * Copyright(c) 2006-2008, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
/**
 
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 />
 
14
 * <pre><code>
 
15
new Ext.tree.TreeSorter(myTree, {
 
16
    folderSort: true,
 
17
    dir: "desc",
 
18
    sortType: function(node) {
 
19
        // sort by a custom, typed attribute:
 
20
        return parseInt(node.id, 10);
 
21
    }
 
22
});
 
23
</code></pre>
 
24
 * @constructor
 
25
 * @param {TreePanel} tree
 
26
 * @param {Object} config
 
27
 */
 
28
Ext.tree.TreeSorter = function(tree, config){
 
29
    /**
 
30
         * @cfg {Boolean} folderSort True to sort leaf nodes under non-leaf nodes (defaults to false)
 
31
     */
 
32
        /** 
 
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.
 
35
     */
 
36
    /** 
 
37
         * @cfg {String} dir The direction to sort ("asc" or "desc," case-insensitive, defaults to "asc")
 
38
     */
 
39
    /** 
 
40
         * @cfg {String} leafAttr The attribute used to determine leaf nodes when {@link #folderSort} = true (defaults to "leaf")
 
41
     */
 
42
    /** 
 
43
         * @cfg {Boolean} caseSensitive true for case-sensitive sort (defaults to false)
 
44
     */
 
45
    /** 
 
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.
 
51
     */
 
52
    
 
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);
 
58
    
 
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';
 
65
 
 
66
    this.sortFn = function(n1, n2){
 
67
        if(fs){
 
68
            if(n1.attributes[leafAttr] && !n2.attributes[leafAttr]){
 
69
                return 1;
 
70
            }
 
71
            if(!n1.attributes[leafAttr] && n2.attributes[leafAttr]){
 
72
                return -1;
 
73
            }
 
74
        }
 
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());
 
77
        if(v1 < v2){
 
78
                        return dsc ? +1 : -1;
 
79
                }else if(v1 > v2){
 
80
                        return dsc ? -1 : +1;
 
81
        }else{
 
82
                return 0;
 
83
        }
 
84
    };
 
85
};
 
86
 
 
87
Ext.tree.TreeSorter.prototype = {
 
88
    doSort : function(node){
 
89
        node.sort(this.sortFn);
 
90
    },
 
91
    
 
92
    compareNodes : function(n1, n2){
 
93
        return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);
 
94
    },
 
95
    
 
96
    updateSort : function(tree, node){
 
97
        if(node.childrenRendered){
 
98
            this.doSort.defer(1, this, [node]);
 
99
        }
 
100
    },
 
101
    
 
102
    updateSortParent : function(node){
 
103
                var p = node.parentNode;
 
104
                if(p && p.childrenRendered){
 
105
            this.doSort.defer(1, this, [p]);
 
106
        }
 
107
    }
 
108
};
 
 
b'\\ No newline at end of file'