Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / TreeFilter.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js"><div id="cls-Ext.tree.TreeFilter"></div>/**
9  * @class Ext.tree.TreeFilter
10  * Note this class is experimental and doesn't update the indent (lines) or expand collapse icons of the nodes
11  * @param {TreePanel} tree
12  * @param {Object} config (optional)
13  */
14 Ext.tree.TreeFilter = function(tree, config){
15     this.tree = tree;
16     this.filtered = {};
17     Ext.apply(this, config);
18 };
19
20 Ext.tree.TreeFilter.prototype = {
21     clearBlank:false,
22     reverse:false,
23     autoClear:false,
24     remove:false,
25
26      <div id="method-Ext.tree.TreeFilter-filter"></div>/**
27      * Filter the data by a specific attribute.
28      * @param {String/RegExp} value Either string that the attribute value
29      * should start with or a RegExp to test against the attribute
30      * @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
31      * @param {TreeNode} startNode (optional) The node to start the filter at.
32      */
33     filter : function(value, attr, startNode){
34         attr = attr || "text";
35         var f;
36         if(typeof value == "string"){
37             var vlen = value.length;
38             // auto clear empty filter
39             if(vlen == 0 && this.clearBlank){
40                 this.clear();
41                 return;
42             }
43             value = value.toLowerCase();
44             f = function(n){
45                 return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
46             };
47         }else if(value.exec){ // regex?
48             f = function(n){
49                 return value.test(n.attributes[attr]);
50             };
51         }else{
52             throw 'Illegal filter type, must be string or regex';
53         }
54         this.filterBy(f, null, startNode);
55         },
56
57     <div id="method-Ext.tree.TreeFilter-filterBy"></div>/**
58      * Filter by a function. The passed function will be called with each
59      * node in the tree (or from the startNode). If the function returns true, the node is kept
60      * otherwise it is filtered. If a node is filtered, its children are also filtered.
61      * @param {Function} fn The filter function
62      * @param {Object} scope (optional) The scope of the function (defaults to the current node)
63      */
64     filterBy : function(fn, scope, startNode){
65         startNode = startNode || this.tree.root;
66         if(this.autoClear){
67             this.clear();
68         }
69         var af = this.filtered, rv = this.reverse;
70         var f = function(n){
71             if(n == startNode){
72                 return true;
73             }
74             if(af[n.id]){
75                 return false;
76             }
77             var m = fn.call(scope || n, n);
78             if(!m || rv){
79                 af[n.id] = n;
80                 n.ui.hide();
81                 return false;
82             }
83             return true;
84         };
85         startNode.cascade(f);
86         if(this.remove){
87            for(var id in af){
88                if(typeof id != "function"){
89                    var n = af[id];
90                    if(n && n.parentNode){
91                        n.parentNode.removeChild(n);
92                    }
93                }
94            }
95         }
96     },
97
98     <div id="method-Ext.tree.TreeFilter-clear"></div>/**
99      * Clears the current filter. Note: with the "remove" option
100      * set a filter cannot be cleared.
101      */
102     clear : function(){
103         var t = this.tree;
104         var af = this.filtered;
105         for(var id in af){
106             if(typeof id != "function"){
107                 var n = af[id];
108                 if(n){
109                     n.ui.show();
110                 }
111             }
112         }
113         this.filtered = {};
114     }
115 };
116 </pre>    \r
117 </body>\r
118 </html>