summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/tree/TreeSorter.js
blob: 99607037693b614b210854f1cba0a41f1e36dccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
YAHOO.ext.tree.TreeSorter = function(tree, config){
    YAHOO.ext.util.Config.apply(this, config);
    tree.on('beforechildrenrendered', this.doSort, this, true);
    tree.on('append', this.updateSort, this, true);
    tree.on('insert', this.updateSort, this, true);
    
    var dsc = this.dir && this.dir.toLowerCase() == 'desc';
    var p = this.property || 'text';
    var sortType = this.sortType;
    var fs = this.folderSort;
    var cs = this.caseSensitive === true;
    
    this.sortFn = function(n1, n2){
        if(fs){
            if(n1.leaf && !n2.leaf){
                return 1;
            }
            if(!n1.leaf && n2.leaf){
                return -1;
            }
        }
    	var v1 = sortType ? sortType(n1) : (cs ? n1[p] : n1[p].toUpperCase());
    	var v2 = sortType ? sortType(n2) : (cs ? n2[p] : n2[p].toUpperCase());
    	if(v1 < v2){
			return dsc ? +1 : -1;
		}else if(v1 > v2){
			return dsc ? -1 : +1;
        }else{
	    	return 0;
        }
    };
};

YAHOO.ext.tree.TreeSorter.prototype = {
    doSort : function(node){
        node.sort(this.sortFn);
    },
    
    compareNodes : function(n1, n2){
        
        return (n1.text.toUpperCase() > n2.text.toUpperCase() ? 1 : -1);
    },
    
    updateSort : function(tree, node){
        if(node.childrenRendered){
            this.doSort.defer(1, this, [node]);
        }
    }
};