NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / recordset-filter / recordset-filter.js
blob9884a40de4835232540850208c31a0faa46697d9
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('recordset-filter', function (Y, NAME) {
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  */
18 var YArray = Y.Array,
19 Lang = Y.Lang;
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);
31 Y.mix(RecordsetFilter, {
32     NS: "filter",
34     NAME: "recordsetFilter",
36     ATTRS: {
37     }
39 });
42 Y.extend(RecordsetFilter, Y.Plugin.Base, {
45     /**
46     Filter through the recordset with a custom filter function, or a key-value
47     pair.
49     @method filter
50     @param {Function|String} filter A custom filter function or a string
51         representing the key to filter by.
52     @param {Any} [value] If filtering by key (_filter_ is a string), further
53         filter by a specific value.
54     @return {Recordset} A new filtered Recordset instance
55     **/
56     filter: function (filter, value) {
57         var recs = this.get('host').get('records'),
58             key;
60         //If a key-value pair is passed in, generate a custom function
61         if (value && Lang.isString(filter)) {
62             key = filter;
63             filter = function(item) {
64                 return (item.getValue(key) === value);
65             };
66         }
68         //TODO: PARENT CHILD RELATIONSHIP
69         return new Y.Recordset({
70             records: YArray.filter(recs, filter)
71         });
72     },
74     /**
75     The inverse of filter. Executes the supplied function on each item. Returns
76     a new Recordset containing the items that the supplied function returned
77     `false` for.
79     @method reject
80     @param {Function} filter A boolean function, executed on each item.
81     @return {Recordset} A new Recordset instance containing the items on which
82         the supplied function returned false.
83     **/
84     reject: function (filter) {
85         return new Y.Recordset({
86             records: YArray.reject(this.get('host').get('records'), filter)
87         });
88     },
90     /**
91     Iterates over the Recordset, returning a new Recordset of all the elements
92     that match the supplied regular expression
94     @method grep
95     @param {RegExp} pattern The regular expression to test against each record.
96     @return {Recordset} A Recordset instance containing all the items in the
97         collection that produce a match against the supplied regular
98         expression. If no items match, an empty Recordset instance is returned.
99     **/
100     grep: function (pattern) {
101         return new Y.Recordset({
102             records: YArray.grep(this.get('host').get('records'), pattern)
103         });
104     }
106     //TODO: Add more pass-through methods to arrayextras
109 Y.namespace("Plugin").RecordsetFilter = RecordsetFilter;
112 }, '3.13.0', {"requires": ["recordset-base", "array-extras", "plugin"]});