- uploading 1.1 in tags
[mootools.git] / Element / Element.Filters.js
blobc46bc1815e2fdb7b26569b4b7fef79ecb5fd6049
1 /*
2 Script: Element.Filters.js
3         add Filters capability to <Elements>.
5 License:
6         MIT-style license.
7 */
9 /*
10 Class: Elements
11         A collection of methods to be used with <$$> elements collections.
14 Elements.extend({
15         
16         /*
17         Property: filterByTag
18                 Filters the collection by a specified tag name.
19                 Returns a new Elements collection, while the original remains untouched.
20         */
21         
22         filterByTag: function(tag){
23                 return new Elements(this.filter(function(el){
24                         return (Element.getTag(el) == tag);
25                 }));
26         },
27         
28         /*
29         Property: filterByClass
30                 Filters the collection by a specified class name.
31                 Returns a new Elements collection, while the original remains untouched.
32         */
33         
34         filterByClass: function(className, nocash){
35                 var elements = this.filter(function(el){
36                         return (el.className && el.className.contains(className, ' '));
37                 });
38                 return (nocash) ? elements : new Elements(elements);
39         },
40         
41         /*
42         Property: filterById
43                 Filters the collection by a specified ID.
44                 Returns a new Elements collection, while the original remains untouched.
45         */
46         
47         filterById: function(id, nocash){
48                 var elements = this.filter(function(el){
49                         return (el.id == id);
50                 });
51                 return (nocash) ? elements : new Elements(elements);
52         },
53         
54         /*
55         Property: filterByAttribute
56                 Filters the collection by a specified attribute.
57                 Returns a new Elements collection, while the original remains untouched.
58                 
59         Arguments:
60                 name - the attribute name.
61                 operator - optional, the attribute operator.
62                 value - optional, the attribute value, only valid if the operator is specified.
63         */
64         
65         filterByAttribute: function(name, operator, value, nocash){
66                 var elements = this.filter(function(el){
67                         var current = Element.getProperty(el, name);
68                         if (!current) return false;
69                         if (!operator) return true;
70                         switch(operator){
71                                 case '=': return (current == value);
72                                 case '*=': return (current.contains(value));
73                                 case '^=': return (current.substr(0, value.length) == value);
74                                 case '$=': return (current.substr(current.length - value.length) == value);
75                                 case '!=': return (current != value);
76                                 case '~=': return current.contains(value, ' ');
77                         }
78                         return false;
79                 });
80                 return (nocash) ? elements : new Elements(elements);
81         }
83 });