X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/6746dc89c47ed01b165cc1152533605f97eb8e8d..f562e4c6e5fac7bcb445985b99acbea4d706e6f0:/src/util/Filter.js diff --git a/src/util/Filter.js b/src/util/Filter.js index 8082c19e..97f14a4f 100644 --- a/src/util/Filter.js +++ b/src/util/Filter.js @@ -13,40 +13,38 @@ If you are unsure which license is appropriate for your use, please contact the */ /** - * @class Ext.util.Filter - * @extends Object - *

Represents a filter that can be applied to a {@link Ext.util.MixedCollection MixedCollection}. Can either simply - * filter on a property/value pair or pass in a filter function with custom logic. Filters are always used in the context - * of MixedCollections, though {@link Ext.data.Store Store}s frequently create them when filtering and searching on their - * records. Example usage:

-

-//set up a fictional MixedCollection containing a few people to filter on
-var allNames = new Ext.util.MixedCollection();
-allNames.addAll([
-    {id: 1, name: 'Ed',    age: 25},
-    {id: 2, name: 'Jamie', age: 37},
-    {id: 3, name: 'Abe',   age: 32},
-    {id: 4, name: 'Aaron', age: 26},
-    {id: 5, name: 'David', age: 32}
-]);
-
-var ageFilter = new Ext.util.Filter({
-    property: 'age',
-    value   : 32
-});
-
-var longNameFilter = new Ext.util.Filter({
-    filterFn: function(item) {
-        return item.name.length > 4;
-    }
-});
-
-//a new MixedCollection with the 3 names longer than 4 characters
-var longNames = allNames.filter(longNameFilter);
-
-//a new MixedCollection with the 2 people of age 24:
-var youngFolk = allNames.filter(ageFilter);
-
+ * Represents a filter that can be applied to a {@link Ext.util.MixedCollection MixedCollection}. Can either simply + * filter on a property/value pair or pass in a filter function with custom logic. Filters are always used in the + * context of MixedCollections, though {@link Ext.data.Store Store}s frequently create them when filtering and searching + * on their records. Example usage: + * + * //set up a fictional MixedCollection containing a few people to filter on + * var allNames = new Ext.util.MixedCollection(); + * allNames.addAll([ + * {id: 1, name: 'Ed', age: 25}, + * {id: 2, name: 'Jamie', age: 37}, + * {id: 3, name: 'Abe', age: 32}, + * {id: 4, name: 'Aaron', age: 26}, + * {id: 5, name: 'David', age: 32} + * ]); + * + * var ageFilter = new Ext.util.Filter({ + * property: 'age', + * value : 32 + * }); + * + * var longNameFilter = new Ext.util.Filter({ + * filterFn: function(item) { + * return item.name.length > 4; + * } + * }); + * + * //a new MixedCollection with the 3 names longer than 4 characters + * var longNames = allNames.filter(longNameFilter); + * + * //a new MixedCollection with the 2 people of age 24: + * var youngFolk = allNames.filter(ageFilter); + * */ Ext.define('Ext.util.Filter', { @@ -54,57 +52,63 @@ Ext.define('Ext.util.Filter', { /* End Definitions */ /** - * @cfg {String} property The property to filter on. Required unless a {@link #filterFn} is passed + * @cfg {String} property + * The property to filter on. Required unless a {@link #filterFn} is passed */ /** - * @cfg {Function} filterFn A custom filter function which is passed each item in the {@link Ext.util.MixedCollection} - * in turn. Should return true to accept each item or false to reject it + * @cfg {Function} filterFn + * A custom filter function which is passed each item in the {@link Ext.util.MixedCollection} in turn. Should return + * true to accept each item or false to reject it */ /** - * @cfg {Boolean} anyMatch True to allow any match - no regex start/end line anchors will be added. Defaults to false + * @cfg {Boolean} anyMatch + * True to allow any match - no regex start/end line anchors will be added. */ anyMatch: false, /** - * @cfg {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. - * Ignored if anyMatch is true. + * @cfg {Boolean} exactMatch + * True to force exact match (^ and $ characters added to the regex). Ignored if anyMatch is true. */ exactMatch: false, /** - * @cfg {Boolean} caseSensitive True to make the regex case sensitive (adds 'i' switch to regex). Defaults to false. + * @cfg {Boolean} caseSensitive + * True to make the regex case sensitive (adds 'i' switch to regex). */ caseSensitive: false, /** - * @cfg {String} root Optional root property. This is mostly useful when filtering a Store, in which case we set the - * root to 'data' to make the filter pull the {@link #property} out of the data object of each item + * @cfg {String} root + * Optional root property. This is mostly useful when filtering a Store, in which case we set the root to 'data' to + * make the filter pull the {@link #property} out of the data object of each item */ /** * Creates new Filter. - * @param {Object} config (optional) Config object + * @param {Object} [config] Config object */ constructor: function(config) { - Ext.apply(this, config); + var me = this; + Ext.apply(me, config); //we're aliasing filter to filterFn mostly for API cleanliness reasons, despite the fact it dirties the code here. //Ext.util.Sorter takes a sorterFn property but allows .sort to be called - we do the same here - this.filter = this.filter || this.filterFn; + me.filter = me.filter || me.filterFn; - if (this.filter == undefined) { - if (this.property == undefined || this.value == undefined) { + if (me.filter === undefined) { + if (me.property === undefined || me.value === undefined) { // Commented this out temporarily because it stops us using string ids in models. TODO: Remove this once // Model has been updated to allow string ids // Ext.Error.raise("A Filter requires either a property or a filterFn to be set"); } else { - this.filter = this.createFilterFn(); + me.filter = me.createFilterFn(); } - this.filterFn = this.filter; + me.filterFn = me.filter; } }, @@ -118,7 +122,8 @@ Ext.define('Ext.util.Filter', { property = me.property; return function(item) { - return matcher.test(me.getRoot.call(me, item)[property]); + var value = me.getRoot.call(me, item)[property]; + return matcher === null ? value === null : matcher.test(value); }; }, @@ -129,7 +134,8 @@ Ext.define('Ext.util.Filter', { * @return {Object} The root property of the object */ getRoot: function(item) { - return this.root == undefined ? item : item[this.root]; + var root = this.root; + return root === undefined ? item : item[root]; }, /** @@ -143,6 +149,10 @@ Ext.define('Ext.util.Filter', { exactMatch = me.exactMatch, caseSensitive = me.caseSensitive, escapeRe = Ext.String.escapeRegex; + + if (value === null) { + return value; + } if (!value.exec) { // not a regex value = String(value);