| 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 |
1×
1×
310×
32×
62×
62×
62×
12×
12×
43×
43×
1×
43×
8×
43×
43×
18×
42×
42×
42×
10×
6×
6×
2×
6×
6×
6×
2×
2×
6×
20×
4×
4×
20×
16×
16×
16×
16×
11×
3×
3×
3×
6×
6×
5×
23×
23×
23×
10×
23×
17×
26×
26×
4×
4×
2×
2×
2×
4×
6×
6×
3×
1×
2×
3×
12×
6×
6×
12×
12×
3×
3×
3×
3×
3×
15×
1×
14×
14×
14×
14×
14×
24×
30×
48×
48×
48×
48×
6×
42×
18×
18×
24×
19×
180×
19×
7×
48×
34×
34×
1×
34×
2×
32×
25×
32×
32×
31×
32×
32×
18×
18×
| import Ember from 'ember';
import layout from '../templates/components/s-select';
const {
Component,
computed,
get,
isBlank,
isPresent,
run,
isArray
} = Ember;
const containsGroup = list => {
return isArray(list) && list.find(ele => ele.parentId);
};
export default Component.extend({
layout,
classNames: ['s-select'],
classNameBindings: [
'isOpen:es-open', 'isFocus:es-focus',
'canSearch::s-select', 'multiple:es-multiple'
],
autofocus: false,
canSearch: true,
disabled: false,
freeText: false,
dropdown: 'select-dropdown',
isDirty: false,
isFocus: false,
isOpen: false,
openOnFocus: false,
placeholder: 'Type...',
required: false,
token: '',
value: '',
labelKey: 'label',
valueKey: 'value',
canClear: computed.and('enabled', 'canSearch', 'hasOptions'),
canOpen: computed.or('hasInput', 'openOnFocus'),
enabled: computed.not('disabled'),
hasDropdown: computed.and('enabled', 'hasModel'),
hasInput: computed.notEmpty('token'),
hasModel: computed.notEmpty('model'),
hasOptions: computed.or('hasInput', 'hasValues'),
hasValues: computed.notEmpty('values'),
multiple: computed.bool('values'),
shouldFilter: computed.or('isDirty', 'multiple', 'hasChanged'),
hideDropdown: computed.not('isOpen'),
input: computed(function() {
return document.querySelector(`#${this.elementId} input`);
}),
hasChanged: computed('token', 'value', function() {
let token = this.get('token');
let option = this.get('value');
if (isPresent(token) && isPresent(option)) {
let { label } = this.retrieveOption(option);
return token !== label;
}
}),
init() {
this._super(...arguments);
if (this.disabled) {
this.set('canSearch', false);
}
if (!this.canSearch) {
this.set('openOnFocus', true);
}
this.set('oldValue', this.get('value'));
if (containsGroup(this.get('model'))) {
this.set('dropdown', 'select-dropdown-group');
}
},
didInsertElement() {
this._super(...arguments);
let value = this.get('value');
if (isPresent(value)) {
run.next(this, () => this.setOption(value));
}
},
didUpdateAttrs() {
this._super(...arguments);
// Need to open on lazy models
if (this.get('isDirty')) {
this.open();
}
// Update input if value has changed
let newValue = this.get('value');
let oldValue = this.get('oldValue');
if (oldValue && newValue && oldValue !== newValue) {
let { label } = this.retrieveOption(newValue);
label !== this.get('token') && this.setOption(newValue);
}
this.set('oldValue', newValue);
},
actions: {
blur() {
if (this.get('isDirty')) {
// Clear unallowed input in strict single mode
let option = this.get('freeText') && this.get('token') || '';
this.setOption(option, false, !this.get('multiple'));
}
this.setProperties({
isFocus: false,
isOpen: false
});
},
change(query) {
this.set('token', query);
this.set('isDirty', true);
this.sendAction('onChange', query);
if (isPresent(query)) {
this.open();
}
},
clear() {
this.setOption('', false, !this.get('multiple'));
this.sendAction('onClear');
this.send('focus');
},
dropdown() {
let isOpen = this.toggleProperty('isOpen');
if (isOpen) {
this.get('input').focus();
}
},
focus() {
let input = this.get('input');
input.focus();
if (!this.get('isFocus') && this.get('canSearch')) {
// Select text (similar to TAB)
input.select();
}
if (!this.get('isOpen')) {
this.open();
}
},
keypress(e) {
let isOpen = this.get('isOpen');
switch (e.which) {
case 8: { // Backspace
let values = this.get('values');
if (isPresent(values) && this.get('token') === '') {
let removedVal = this.get('values').pop();
this.attrs.onRemove && this.attrs.onRemove(removedVal);
e.preventDefault();
}
break;
}
case 9: // TAB
case 13: // Enter
this.set('keyEvent', e);
break;
case 27: // ESC
if (this.get('canSearch') && this.get('hasInput')) {
this.send('clear');
} else {
this.set('isOpen', false);
}
break;
case 38: // Up Arrow
case 40: // Down Arrow
if (isOpen) {
this.set('keyEvent', e);
} else {
this.set('isOpen', true);
}
e.preventDefault();
break;
}
},
remove(selection) {
let values = this.get('values');
let index = values.indexOf(selection);
values.splice(index, 1);
this.attrs.onRemove && this.attrs.onRemove(selection);
this.send('focus');
},
select(option, selected) {
if (!isPresent(option)) {
return;
}
let hasOnCreate = isPresent(this.attrs.onCreate);
let notifySelection = selected || !hasOnCreate;
!selected && hasOnCreate && this.attrs.onCreate(option);
run.next(() => {
this.setOption(option, selected, notifySelection);
});
}
},
// Handle plain arrays and Ember Data relationships
getElement(model, position) {
return model[position] || model.objectAt(position);
},
open() {
this.setProperties({
isOpen: this.get('hasDropdown') && this.get('canOpen'),
isFocus: true
});
},
/* Retrieve `option`, `value` and `label` given a selection
* which can be either an option (object) or a value */
retrieveOption(option) {
let model = this.get('model');
let label = option;
let value = option;
if (isBlank(option)) {
label = '';
} else if (typeof option === 'object') {
label = get(option, this.get('labelKey'));
value = get(option, this.get('valueKey'));
} else if (isPresent(model) && typeof this.getElement(model, 0) === 'object') {
let id = this.get('valueKey');
option = model.filter(x => get(x, id) === option).shift();
if (option) {
label = get(option, this.get('labelKey'));
}
}
return { option, value, label };
},
setOption(selection, selected, notify) {
let { option, value, label } = this.retrieveOption(selection);
if (this.get('multiple')) {
label = '';
}
if (!selected && notify && this.get('required')) {
return this.setOption(this.get('value'));
}
if (this.get('canSearch')) {
this.set('token', label);
}
// Ensure the component is DOM ready before updating
let input = this.get('input');
if (input) {
input.value = label;
}
this.set('isDirty', false);
if (notify) {
this.attrs.onSelect && this.attrs.onSelect(value, option, selected);
this.set('isOpen', false);
}
}
});
|