all files / s-select/components/ select-dropdown-group.js

100% Statements 17/17
100% Branches 2/2
100% Functions 4/4
100% Lines 17/17
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                                    18×   18× 18×   18×       34× 10×     34×       70× 56×   15×     15× 150×               40×          
import Ember from 'ember';
import layout from '../templates/components/select-dropdown-group';
import SelectDropdown from './select-dropdown';
import { getDescendents } from '../utils/tree';
 
const {
  computed,
  get,
  isPresent,
  on,
  observer
} = Ember;
 
export default SelectDropdown.extend({
  layout,
  groups: null,
  list: null,
 
  modelChanged: on('init', observer('model', function() {
    this._super(...arguments);
    // Tree built in extended component
    let groups = this.get('list');
    let list = getDescendents(groups);
 
    this.setProperties({ list, groups });
  })),
 
  options: computed('token', 'model.[]', 'values.[]', 'shouldFilter', function() {
    if (this.get('shouldFilter')) {
      this.filterModel();
    }
 
    return this.get('groups');
  }),
 
  setVisibility(list, token) {
    list
      .filter(el => isPresent(get(el, 'parentId')))
      .filter(el => get(el, 'name').toLowerCase().indexOf(token) > -1)
      .forEach(el => {
        el.set('isVisible', true);
 
        // Mark parent visible
        list
          .filter(x => x.id === get(el, 'parentId'))
          .shift()
          .set('isVisible', true);
      });
  },
 
  upDownKeys(selected, keyEvent) {
    let list = this.get('list')
      .filterBy('isVisible')
      .filter(el => isPresent(get(el, 'parentId')));
 
    this.move(list, selected, keyEvent.which);
  }
});