source: trunk/web/addons/job_monarch/lib/extjs/source/widgets/tree/TreeFilter.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 3.6 KB
Line 
1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/**
10* @class Ext.tree.TreeFilter
11* Note this class is experimental and doesn't update the indent (lines) or expand collapse icons of the nodes
12* @param {TreePanel} tree
13* @param {Object} config (optional)
14 */
15Ext.tree.TreeFilter = function(tree, config){
16    this.tree = tree;
17    this.filtered = {};
18    Ext.apply(this, config);
19};
20
21Ext.tree.TreeFilter.prototype = {
22    clearBlank:false,
23    reverse:false,
24    autoClear:false,
25    remove:false,
26
27     /**
28     * Filter the data by a specific attribute.
29     * @param {String/RegExp} value Either string that the attribute value
30     * should start with or a RegExp to test against the attribute
31     * @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
32     * @param {TreeNode} startNode (optional) The node to start the filter at.
33     */
34    filter : function(value, attr, startNode){
35        attr = attr || "text";
36        var f;
37        if(typeof value == "string"){
38            var vlen = value.length;
39            // auto clear empty filter
40            if(vlen == 0 && this.clearBlank){
41                this.clear();
42                return;
43            }
44            value = value.toLowerCase();
45            f = function(n){
46                return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
47            };
48        }else if(value.exec){ // regex?
49            f = function(n){
50                return value.test(n.attributes[attr]);
51            };
52        }else{
53            throw 'Illegal filter type, must be string or regex';
54        }
55        this.filterBy(f, null, startNode);
56        },
57   
58    /**
59     * Filter by a function. The passed function will be called with each
60     * node in the tree (or from the startNode). If the function returns true, the node is kept
61     * otherwise it is filtered. If a node is filtered, its children are also filtered.
62     * @param {Function} fn The filter function
63     * @param {Object} scope (optional) The scope of the function (defaults to the current node)
64     */
65    filterBy : function(fn, scope, startNode){
66        startNode = startNode || this.tree.root;
67        if(this.autoClear){
68            this.clear();
69        }
70        var af = this.filtered, rv = this.reverse;
71        var f = function(n){
72            if(n == startNode){
73                return true;
74            }
75            if(af[n.id]){
76                return false;
77            }
78            var m = fn.call(scope || n, n);
79            if(!m || rv){
80                af[n.id] = n;
81                n.ui.hide();
82                return false;
83            }
84            return true;
85        };
86        startNode.cascade(f);
87        if(this.remove){
88           for(var id in af){
89               if(typeof id != "function"){
90                   var n = af[id];
91                   if(n && n.parentNode){
92                       n.parentNode.removeChild(n);
93                   }
94               }
95           } 
96        }
97    },
98   
99    /**
100     * Clears the current filter. Note: with the "remove" option
101     * set a filter cannot be cleared.
102     */
103    clear : function(){
104        var t = this.tree;
105        var af = this.filtered;
106        for(var id in af){
107            if(typeof id != "function"){
108                var n = af[id];
109                if(n){
110                    n.ui.show();
111                }
112            }
113        }
114        this.filtered = {}; 
115    }
116};
Note: See TracBrowser for help on using the repository browser.