-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-picker.ts
52 lines (45 loc) · 1.64 KB
/
multi-picker.ts
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
import { Component, Input, EventEmitter, Output, OnInit } from '@angular/core';
@Component({
selector: 'multi-picker',
templateUrl: './multi-picker.html',
styleUrls: ['./multi-picker.scss'],
})
export class MultiPickerComponent implements OnInit {
@Input('model') model: string[];
@Input('data') options: (IKeyValuePair<string, string> & ISelectable)[] = [];
@Input() placeholder: string;
@Output('modelChange') modelChange = new EventEmitter<string[]>();
selectedLabel: string;
ngOnInit(): void {
if (!this.model) this.model = [];
this.options.forEach(s => s.selected = this.model.contains(s.value));
this.afterChanged();
}
handleSelection(item: (IKeyValuePair<string, string> & ISelectable), $event: Event) {
item.selected = !item.selected;
if (!item.selected)
this.model.splice(this.model.indexOf(item.value), 1);
else
this.model.push(item.value);
this.afterChanged();
$event.stopPropagation();
}
toggleAll($event: Event) {
this.model = this.options.map(s => {
s.selected = true;
return s.value;
});
this.afterChanged();
$event.stopPropagation();
}
toggleNone($event: Event) {
this.model = [];
this.options.forEach(state => state.selected = false);
this.afterChanged();
$event.stopPropagation();
}
private afterChanged() {
this.selectedLabel = (this.model.length) ? this.model.length + ' selected' : this.placeholder;
this.modelChange.emit(this.model);
}
}