All files / app/assets/javascripts/filtered_search/droplab/plugins filter.js

2.22% Statements 1/45
0% Branches 0/23
0% Functions 0/8
2.43% Lines 1/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97    8x                                                                                                                                                                                            
/* eslint-disable */
 
const Filter = {
  keydown: function (e) {
    if (this.destroyed) return;
 
    var hiddenCount = 0;
    var dataHiddenCount = 0;
 
    var list = e.detail.hook.list;
    var data = list.data;
    var value = e.detail.hook.trigger.value.toLowerCase();
    var config = e.detail.hook.config.Filter;
    var matches = [];
    var filterFunction;
    // will only work on dynamically set data
    if (!data) {
      return;
    }
 
    if (config && config.filterFunction && typeof config.filterFunction === 'function') {
      filterFunction = config.filterFunction;
    } else {
      filterFunction = function (o) {
        // cheap string search
        o.droplab_hidden = o[config.template].toLowerCase().indexOf(value) === -1;
        return o;
      };
    }
 
    dataHiddenCount = data.filter(function (o) {
      return !o.droplab_hidden;
    }).length;
 
    matches = data.map(function (o) {
      return filterFunction(o, value);
    });
 
    hiddenCount = matches.filter(function (o) {
      return !o.droplab_hidden;
    }).length;
 
    if (dataHiddenCount !== hiddenCount) {
      list.setData(matches);
      list.currentIndex = 0;
    }
  },
 
  debounceKeydown: function debounceKeydown(e) {
    if (
      [
        13, // enter
        16, // shift
        17, // ctrl
        18, // alt
        20, // caps lock
        37, // left arrow
        38, // up arrow
        39, // right arrow
        40, // down arrow
        91, // left window
        92, // right window
        93, // select
      ].indexOf(e.detail.which || e.detail.keyCode) > -1
    )
      return;
 
    if (this.timeout) clearTimeout(this.timeout);
    this.timeout = setTimeout(this.keydown.bind(this, e), 200);
  },
 
  init: function init(hook) {
    var config = hook.config.Filter;
 
    if (!config || !config.template) return;
 
    this.hook = hook;
    this.destroyed = false;
 
    this.eventWrapper = {};
    this.eventWrapper.debounceKeydown = this.debounceKeydown.bind(this);
 
    this.hook.trigger.addEventListener('keydown.dl', this.eventWrapper.debounceKeydown);
    this.hook.trigger.addEventListener('mousedown.dl', this.eventWrapper.debounceKeydown);
  },
 
  destroy: function destroy() {
    if (this.timeout) clearTimeout(this.timeout);
    this.destroyed = true;
 
    this.hook.trigger.removeEventListener('keydown.dl', this.eventWrapper.debounceKeydown);
    this.hook.trigger.removeEventListener('mousedown.dl', this.eventWrapper.debounceKeydown);
  },
};
 
export default Filter;