github.com/outbrain/consul@v1.4.5/ui-v2/app/mixins/with-filtering.js (about)

     1  import Mixin from '@ember/object/mixin';
     2  import { computed, get, set } from '@ember/object';
     3  
     4  const toKeyValue = function(el) {
     5    const key = el.name;
     6    let value = '';
     7    switch (el.type) {
     8      case 'radio':
     9      case 'search':
    10      case 'text':
    11        value = el.value;
    12        break;
    13    }
    14    return { [key]: value };
    15  };
    16  export default Mixin.create({
    17    filters: {},
    18    filtered: computed('items', 'filters', function() {
    19      const filters = get(this, 'filters');
    20      return get(this, 'items').filter(item => {
    21        return this.filter(item, filters);
    22      });
    23    }),
    24    setProperties: function() {
    25      this._super(...arguments);
    26      const query = get(this, 'queryParams');
    27      query.forEach((item, i, arr) => {
    28        const filters = get(this, 'filters');
    29        Object.keys(item).forEach(key => {
    30          set(filters, key, get(this, key));
    31        });
    32        set(this, 'filters', filters);
    33      });
    34    },
    35    actions: {
    36      filter: function(e) {
    37        const obj = toKeyValue(e.target);
    38        Object.keys(obj).forEach((key, i, arr) => {
    39          set(this, key, obj[key] != '' ? obj[key] : null);
    40        });
    41        set(this, 'filters', {
    42          ...this.get('filters'),
    43          ...obj,
    44        });
    45      },
    46    },
    47  });