partkeepr

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

BaseStore.js (5569B)


      1 Ext.define("PartKeepr.Data.Store.BaseStore", {
      2     extend: "Ext.data.Store",
      3 
      4     autoSync: false, // Do not change. If true, new (empty) records would be immediately committed to the database.
      5     remoteFilter: true,
      6     remoteSort: true,
      7     pageSize: 15,
      8 
      9     /**
     10      * @var {Boolean} Specifies if the search field has an active search
     11      */
     12     hasSearch: false,
     13 
     14     /**
     15      * @cfg {String} Specifies the target property to search
     16      */
     17     targetField: 'query',
     18 
     19     /**
     20      * @cfg {String} Specifies the system property which defines all fields to be searched
     21      */
     22     searchFieldSystemPreference: null,
     23 
     24     /**
     25      * @cfg {Array} Specifies the default fields to be searched
     26      */
     27     searchFieldSystemPreferenceDefaults: [],
     28 
     29     /**
     30      * @cfg {String} Specifies the system property which defines if the search terms should be splitted
     31      */
     32     splitSearchTermSystemPreference: null,
     33 
     34     /**
     35      * @cfg {String} Specifies the default for search term splitting
     36      */
     37     splitSearchTermSystemPreferenceDefaults: true,
     38 
     39     /**
     40      * @var {Ext.util.Filter} The filter set by the search field
     41      */
     42     searchFilter: null,
     43 
     44     listeners: {
     45         // Workaround for bug http://www.sencha.com/forum/showthread.php?133767-Store.sync()-does-not-update-dirty-flag&p=607093#post607093
     46         // TODO: Check if this is still present in ExtJS 6.x
     47         write: function (store, operation) {
     48             var success = operation.wasSuccessful();
     49             if (success) {
     50                 Ext.each(operation.records, function (record)
     51                 {
     52                     if (record.dirty) {
     53                         record.commit();
     54                     }
     55                 });
     56             }
     57         }
     58     },
     59 
     60     constructor: function () {
     61         this.searchFilter = Ext.create("PartKeepr.util.Filter");
     62         this.callParent(arguments);
     63     },
     64 
     65     doSearch: function (searchValue) {
     66         if (typeof searchValue !== "string") {
     67             return;
     68         }
     69 
     70         var searchTerms = searchValue.split(" "),
     71             splitTerms = true,
     72             orSubFilters = [],
     73             i,
     74             j,
     75             subFilters = [];
     76 
     77         if (this.splitSearchTermSystemPreference !== null) {
     78             splitTerms = Boolean(PartKeepr.getApplication().getSystemPreference(this.splitSearchTermSystemPreference,
     79                 this.splitSearchTermSystemPreferenceDefaults));
     80         }
     81 
     82         if (this.searchFieldSystemPreference !== null) {
     83             var fields = PartKeepr.getApplication().getSystemPreference(this.searchFieldSystemPreference,
     84                 this.searchFieldSystemPreferenceDefaults);
     85 
     86             if (splitTerms === true) {
     87                 for (j = 0; j < searchTerms.length; j++) {
     88                     orSubFilters = [];
     89                     for (i = 0; i < fields.length; i++) {
     90                         orSubFilters.push(this.createSearchFilter(fields[i], searchTerms[j]));
     91                     }
     92 
     93                     subFilters.push(Ext.create("PartKeepr.util.Filter", {
     94                         type: "OR",
     95                         subfilters: orSubFilters
     96                     }));
     97                 }
     98 
     99                 this.searchFilter.setConfig({
    100                     type: "AND",
    101                     subfilters: subFilters
    102                 });
    103 
    104             } else {
    105                 for (i = 0; i < fields.length; i++) {
    106                     subFilters.push(this.createSearchFilter(fields[i], searchValue));
    107                 }
    108 
    109                 this.searchFilter.setConfig({
    110                     type: "OR",
    111                     subfilters: subFilters
    112                 });
    113             }
    114 
    115 
    116         } else {
    117             if (splitTerms === true) {
    118                 for (j = 0; j < searchTerms.length; j++) {
    119                     subFilters.push(this.createSearchFilter(this.targetField, searchTerms[j]));
    120                 }
    121 
    122                 this.searchFilter.setConfig({
    123                     type: "OR",
    124                     subfilters: subFilters
    125                 });
    126             } else {
    127                 this.searchFilter.setConfig({
    128                     property: this.targetField,
    129                     value: "%" + searchValue+ "%",
    130                     operator: 'like'
    131                 });
    132             }
    133         }
    134 
    135         if (searchValue.length < 1) {
    136             this.resetSearch();
    137             return;
    138         }
    139 
    140         if (this.isLoading()) {
    141             Ext.defer(this.startSearch, 200, this);
    142             return;
    143         }
    144 
    145         this.searchFilter.setValue(searchValue);
    146 
    147         if (!this.getFilters().contains(this.searchFilter)) {
    148             this.getFilters().add(this.searchFilter);
    149         }
    150 
    151         this.getFilters().itemChanged(this.searchFilter);
    152 
    153         this.hasSearch = true;
    154     },
    155 
    156     createSearchFilter: function (property, term) {
    157         return Ext.create("PartKeepr.util.Filter", {
    158             property: property,
    159             value: "%" + term + "%",
    160             operator: 'like'
    161         });
    162     },
    163     /**
    164      * Resets the search field to empty and re-triggers the store to load the matching records.
    165      */
    166     resetSearch: function ()
    167     {
    168         if (this.isLoading()) {
    169             Ext.defer(this.resetSearch, 200, this);
    170             return;
    171         }
    172 
    173         this.searchFilter.setValue('');
    174 
    175         if (this.hasSearch) {
    176 
    177             if (this.getFilters().contains(this.searchFilter)) {
    178                 this.getFilters().remove(this.searchFilter);
    179             }
    180 
    181             this.currentPage = 1;
    182             this.load({start: 0});
    183             this.hasSearch = false;
    184 
    185         }
    186     }
    187 
    188 
    189 });