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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/**
* This doesn't update the indent (lines) or expand collapse icons of the nodes
*/
YAHOO.ext.tree.TreeFilter = function(tree, config){
this.tree = tree;
this.filtered = {};
YAHOO.ext.util.Config.apply(this, config, {
clearBlank:false,
reverse:false,
autoClear:false,
remove:false
});
};
YAHOO.ext.tree.TreeFilter.prototype = {
/**
* Filter the data by a specific attribute.
* @param {String/RegExp} value Either string that the attribute value
* should start with or a RegExp to test against the attribute
* @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
* @param {TreeNode} startNode (optional) The node to start the filter at.
*/
filter : function(value, attr, startNode){
attr = attr || 'text';
var f;
if(typeof value == 'string'){
var vlen = value.length;
// auto clear empty filter
if(vlen == 0 && this.clearBlank){
this.clearFilter();
return;
}
value = value.toLowerCase();
f = function(n){
return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
}
}else if(value.exec){ // regex?
f = function(n){
return value.test(n.attributes[attr]);
}
}else{
throw 'Illegal filter type, must be string or regex';
}
this.filterBy(f, null, startNode);
},
/**
* Filter by a function. The passed function will be called with each
* node in the tree (or from the startNode). If the function returns true, the node is kept
* otherwise it is filtered. If a node is filtered, it's children are also filtered.
* @param {Function} fn The filter function
* @param {Object} scope (optional) The scope of the function (defaults to the current node)
*/
filterBy : function(fn, scope, startNode){
startNode = startNode || this.tree.root;
if(this.autoClear){
this.clearFilter();
}
var af = this.filtered, rv = this.reverse;
var f = function(n){
if(n == startNode){
return true;
}
if(af[n.id]){
return false;
}
var m = fn.call(scope || n, n);
if(!m || rv){
af[n.id] = n;
n.ui.hide();
return false;
}
return true;
}
startNode.cascade(f);
if(this.remove){
for(var id in af){
if(typeof id != 'function'){
var n = af[id];
if(n && n.parentNode){
n.parentNode.removeChild(n);
}
}
}
}
},
/**
* Clears the current filter. Note: with the "remove" option
* set a filter cannot be cleared.
*/
clear : function(){
var t = this.tree;
var af = this.filtered;
for(var id in af){
if(typeof id != 'function'){
var n = af[id];
if(n){
n.ui.show();
}
}
}
this.filtered = {};
}
};
|