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

100% Statements 23/23
100% Branches 8/8
100% Functions 4/4
100% Lines 21/21
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                                    26×   26× 26×   26×       46× 14×     46×       80×   72×   17×     17× 168×   17×           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) {
    let filtered = list;
    if (!this.get('canSelectGroup')) {
      filtered = filtered.filter(el => isPresent(get(el, 'parentId')));
    }
    filtered
      .filter(el => get(el, 'name').toLowerCase().indexOf(token) > -1)
      .forEach(el => {
        el.set('isVisible', true);
 
        // Mark parent visible
        let parent = list
          .filter(x => x.id === get(el, 'parentId'))
          .shift();
        parent && parent.set('isVisible', true);
      });
  },
 
  upDownKeys(selected, keyEvent) {
    let list = this.get('list')
      .filterBy('isVisible');
    if (!this.get('canSelectGroup')) {
      list = list.filter(el => isPresent(get(el, 'parentId')));
    }
    this.move(list, selected, keyEvent.which);
  }
});