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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 1× 54× 54× 54× 54× 25× 55× 13× 55× 3× 3× 1× 3× 3× 13× 23× 23× 23× 204× 23× 26× 23× 74× 14× 14× 36× 78× 8× 8× 27× 2× 25× 25× 9× 9× 16× 16× 1× 16× 7× 16× 2× 14× 14× 14× 5× 4× 5× 3× 9× 3× 9× 6× 14× 14× 14× 7× 56× 3× 9× 4× 5× 3× 12× 12× | import Ember from 'ember'; import layout from '../templates/components/select-dropdown'; import { buildTree } from '../utils/tree'; import { bringInView } from '../utils/view'; const { Component, computed, get, observer, isEmpty, isNone, isPresent, run, on } = Ember; export default Component.extend({ layout, list: null, classNames: ['es-options'], modelChanged: on('init', observer('model', function() { let options = this.getProperties('valueKey', 'labelKey'); let model = this.get('model'); let list = buildTree(model, options); this.setProperties({ list }); })), keyPressed: observer('keyEvent', function() { this.keys.call(this, this.get('keyEvent')); }), options: computed('token', 'model.[]', 'values.[]', 'shouldFilter', function() { if (this.get('shouldFilter')) { this.filterModel(); } return this.get('list'); }), actions: { hover(node) { let selected = this.get('selected'); if (selected) { selected.set('isSelected', false); } this.set('selected', node); node.set('isSelected', true); }, select(node) { this.attrs.select(node.content, true); } }, /* Filter out existing selections. Mark everything visible if no search, otherwise update visiblity. */ filterModel() { let list = this.get('list'); let token = this.get('token'); let values = this.get('values'); list.forEach(el => el.set('isVisible', false)); if (isPresent(values)) { list = list.filter(el => values.indexOf(el.content) === -1); } if (isEmpty(token)) { list.forEach(el => el.set('isVisible', true)); } else { token = token.toLowerCase(); this.setVisibility(list, token); } // Mark first visible element as selected if (!this.get('freeText') && isPresent(token) && list.some(x => get(x, 'isVisible'))) { let [firstVisible] = list.filter(x => get(x, 'isVisible')); firstVisible.set('isSelected', true); this.set('selected', firstVisible); } }, keys(keyEvent) { if (!keyEvent || !keyEvent.which) { return; } let selected = this.get('selected'); switch (keyEvent.which) { case 9: // TAB case 13: // Enter this.tabEnterKeys(selected); break; case 38: // Up case 40: // Down this.upDownKeys(selected, keyEvent); break; } }, // Prevent mousedown event from stealing focus from input mouseDown(event) { event.preventDefault(); }, // Down: 40, Up: 38 move(list, selected, direction) { if (isPresent(selected)) { selected.set('isSelected', false); } if (isEmpty(list)) { return; } let index = selected ? list.findIndex(node => selected.id === node.id) : -1; let node; if (direction === 38) { if (index !== -1) { node = list[index - 1]; } if (isNone(node)) { node = list[list.length - 1]; } } else { if (index !== -1) { node = list[index + 1]; } if (isNone(node)) { node = list[0]; } } this.set('selected', node); node.set('isSelected', true); run.next(this, bringInView, '.es-options', '.es-highlight'); }, setVisibility(list, token) { list .filter(el => get(el, 'name').toLowerCase().indexOf(token) > -1) .forEach(el => el.set('isVisible', true)); }, tabEnterKeys(selected) { if (selected && this.get('list').includes(selected)) { this.send('select', selected); } else if (this.get('freeText')) { this.attrs.select(this.get('token')); } }, upDownKeys(selected, keyEvent) { let list = this.get('list').filterBy('isVisible'); this.move(list, selected, keyEvent.which); } }); |