]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/recordset/recordset-filter.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / recordset / recordset-filter.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('recordset-filter', function(Y) {
9
10 /**
11  * Plugin that provides the ability to filter through a recordset.
12  * Uses the filter methods available on Y.Array (see arrayextras submodule) to filter the recordset.
13  * @module recordset
14  * @submodule recordset-filter
15  */
16
17
18 var YArray = Y.Array,
19 Lang = Y.Lang;
20
21
22 /**
23  * Plugin that provides the ability to filter through a recordset.
24  * Uses the filter methods available on Y.Array (see arrayextras submodule) to filter the recordset. 
25  * @class RecordsetFilter
26  */
27 function RecordsetFilter(config) {
28     RecordsetFilter.superclass.constructor.apply(this, arguments);
29 }
30
31 Y.mix(RecordsetFilter, {
32     NS: "filter",
33
34     NAME: "recordsetFilter",
35
36     ATTRS: {
37     }
38
39 });
40
41
42 Y.extend(RecordsetFilter, Y.Plugin.Base, {
43
44
45     initializer: function(config) {
46     },
47
48     destructor: function(config) {
49     },
50
51     /**
52      * @description Filter through the recordset with a custom filter function, or a key-value pair.
53          *
54          * @method filter
55      * @param f {Function, String} A custom filter function or a string representing the key to filter by.
56      * @param v {any} (optional) If a string is passed into f, this represents the value that key should take in order to be accepted by the filter. Do not pass in anything if 'f' is a custom function
57          * @return recordset {Y.Recordset} A new filtered recordset instance
58      * @public
59      */
60     filter: function(f, v) {
61         var recs = this.get('host').get('records'),
62         oRecs = [],
63         func = f;
64
65         //If a key-value pair is passed in, generate a custom function
66         if (Lang.isString(f) && Lang.isValue(v)) {
67
68             func = function(item) {
69                 if (item.getValue(f) === v) {
70                     return true;
71                 }
72                 else {
73                     return false;
74                 }
75             };
76         }
77
78         oRecs = YArray.filter(recs, func);
79
80
81         //TODO: PARENT CHILD RELATIONSHIP
82         return new Y.Recordset({
83             records: oRecs
84         });
85         //return new host.constructor({records:arr});
86     },
87
88     /**
89         * @description The inverse of filter. Executes the supplied function on each item. Returns a new Recordset containing the items that the supplied function returned *false* for.
90         * @method reject
91         * @param {Function} f is the function to execute on each item.
92         * @return {Y.Recordset} A new Recordset instance containing the items on which the supplied function returned false.
93         */
94     reject: function(f) {
95         return new Y.Recordset({
96             records: YArray.reject(this.get('host').get('records'), f)
97         });
98     },
99
100     /**
101         * @description Iterates over the Recordset, returning a new Recordset of all the elements that match the supplied regular expression
102         * @method grep
103         * @param {pattern} pattern The regular expression to test against
104         * each record.
105         * @return {Y.Recordset} A Recordset instance containing all the items in the collection that produce a match against the supplied regular expression. If no items match, an empty Recordset instance is returned.
106         */
107     grep: function(pattern) {
108         return new Y.Recordset({
109             records: YArray.grep(this.get('host').get('records'), pattern)
110         });
111     }
112
113     //TODO: Add more pass-through methods to arrayextras
114 });
115
116 Y.namespace("Plugin").RecordsetFilter = RecordsetFilter;
117
118
119
120
121 }, '3.3.0' ,{requires:['recordset-base','array-extras','plugin']});