-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathselectable-item-group.js
More file actions
89 lines (77 loc) · 2.31 KB
/
selectable-item-group.js
File metadata and controls
89 lines (77 loc) · 2.31 KB
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
import Component from '@ember/component';
import { A } from '@ember/array';
import { computed, get } from '@ember/object';
import ParentComponentSupport from 'ember-composability/mixins/parent-component-support';
import layout from '../templates/components/selectable-item-group';
export default Component.extend(ParentComponentSupport, {
layout,
content: null,
selection: null,
optionValuePath: 'content',
optionLabelPath: 'content',
multiple: false,
__materializeSelectableItemGroup: true,
init() {
this._super(...arguments);
if ((this.get('selection') === null || this.get('selection') === undefined) && !!this.get('multiple')) {
this.set('selection', new A([]));
}
},
isValueSelected(value) {
if (this.get('multiple')) {
return this.get('selection').indexOf(value) >= 0;
} else {
return this.get('selection') === value;
}
},
setValueSelection(value, select) {
if (select) {
return this.addToSelection(value);
} else {
return this.removeFromSelection(value);
}
},
addToSelection(value) {
if (this.get('multiple')) {
this.get('selection').addObject(value);
} else {
this.set('selection', value);
}
},
removeFromSelection(value) {
if (this.get('multiple')) {
this.get('selection').removeObject(value);
} else {
if (this.get('selection') === value) {
this.set('selection', null);
}
}
},
disabled: false,
_valuePath: computed('optionValuePath', function() {
const optionValuePath = get(this, 'optionValuePath');
return optionValuePath.replace(/^content\.?/, '');
}),
_labelPath: computed('optionLabelPath', function() {
const optionLabelPath = get(this, 'optionLabelPath');
return optionLabelPath.replace(/^content\.?/, '');
}),
_content: computed('content.[]', '_valuePath', '_labelPath', function() {
const valuePath = get(this, '_valuePath');
const labelPath = get(this, '_labelPath');
const content = get(this, 'content') || new A([]);
if (valuePath && labelPath) {
return A(
content.map(el => {
return { value: get(el, valuePath), label: get(el, labelPath) };
})
);
} else {
return A(
content.map(el => {
return { value: el, label: el };
})
);
}
})
});