partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

Filter.js (3772B)


      1 Ext.define('PartKeepr.util.Filter', {
      2     extend: 'Ext.util.Filter',
      3 
      4     config: {
      5         /**
      6          * @cfg {String} [property=null]
      7          * The property to filter on. Required unless a {@link #filterFn} is passed.
      8          */
      9         subfilters: [],
     10     },
     11     /**
     12      * Creates new Filter.
     13      * @param {Object} config Config object
     14      */
     15     constructor: function (config)
     16     {
     17 
     18         //config.filterFns
     19 
     20         this.operatorFns["notin"] = function (candidate)
     21         {
     22             var v = this._filterValue;
     23             return !Ext.Array.contains(v, this.getCandidateValue(candidate, v));
     24         };
     25         //<debug>
     26         var warn = PartKeepr.util.Filter.isInvalid(config);
     27         if (warn) {
     28             Ext.log.warn(warn);
     29         }
     30         //</debug>
     31         this.initConfig(config);
     32     },
     33     getFilterDescription: function () {
     34         var config = this.getInitialConfig(),
     35             i, subfilterData = [];
     36 
     37         if (config.property !== null && config.value !== null && config.operator !== null) {
     38             subfilterData.push(config.property + " " + config.operator + " " + config.value);
     39         }
     40 
     41         if (config.subfilters instanceof Array && config.subfilters.length > 0) {
     42             for (i=0;i<config.subfilters.length;i++) {
     43                 subfilterData.push("(" + config.subfilters[i].getFilterDescription()+ ")");
     44             }
     45         }
     46 
     47         if (config.type && config.type.toLowerCase() == "or") {
     48             return subfilterData.join(" OR ");
     49         } else {
     50             return subfilterData.join(" AND ");
     51         }
     52     },
     53 
     54     preventConvert: {
     55         'in': 1,
     56         'notin': 1
     57     },
     58     /**
     59      * Returns this filter's state.
     60      * @return {Object}
     61      */
     62     getState: function () {
     63         var config = this.getInitialConfig(),
     64             result = {},
     65             name;
     66 
     67         for (name in config) {
     68             // We only want the instance properties in this case, not inherited ones,
     69             // so we need hasOwnProperty to filter out our class values.
     70 
     71             if (name === "subfilters") {
     72                 if (config[name] instanceof Array) {
     73                     var tempConfigs = new Array();
     74 
     75                     for (var i=0;i<config[name].length;i++) {
     76                         tempConfigs.push(config[name][i].getState());
     77                     }
     78 
     79                     result[name] = tempConfigs;
     80                 }
     81             } else if (config.hasOwnProperty(name)) {
     82                     result[name] = config[name];
     83 
     84             }
     85         }
     86 
     87         delete result.root;
     88 
     89         if (config["subfilters"] instanceof Array) {
     90             // Do nothing for now
     91         } else {
     92             result.value = this.getValue();
     93         }
     94         return result;
     95     },
     96     inheritableStatics: {
     97         /**
     98          * Checks whether the filter will produce a meaningful value. Since filters
     99          * may be used in conjunction with data binding, this is a sanity check to
    100          * check whether the resulting filter will be able to match.
    101          *
    102          * @param {Object} cfg The filter config object
    103          * @return {Boolean} `true` if the filter will produce a valid value
    104          *
    105          * @private
    106          */
    107         isInvalid: function(cfg) {
    108             return false;
    109             if (!cfg.filterFn) {
    110                 // If we don't have a filterFn, we must have a property
    111                 if (!cfg.property) {
    112                     return 'A Filter requires either a property or a filterFn to be set';
    113                 }
    114 
    115                 if (!cfg.hasOwnProperty('value') && !cfg.operator) {
    116                     return 'A Filter requires either a property and value, or a filterFn to be set';
    117                 }
    118 
    119             }
    120             return false;
    121         }
    122     }
    123 });