diff options
Diffstat (limited to 'client/src/app/shared/shared-forms/select/select-checkbox.component.ts')
-rw-r--r-- | client/src/app/shared/shared-forms/select/select-checkbox.component.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-forms/select/select-checkbox.component.ts b/client/src/app/shared/shared-forms/select/select-checkbox.component.ts new file mode 100644 index 000000000..93653fef1 --- /dev/null +++ b/client/src/app/shared/shared-forms/select/select-checkbox.component.ts | |||
@@ -0,0 +1,75 @@ | |||
1 | import { Component, Input, forwardRef } from '@angular/core' | ||
2 | import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms' | ||
3 | import { SelectOptionsItem } from './select-options.component' | ||
4 | |||
5 | export type ItemSelectCheckboxValue = { id?: string | number, group?: string } | string | ||
6 | |||
7 | @Component({ | ||
8 | selector: 'my-select-checkbox', | ||
9 | styleUrls: [ './select-shared.component.scss', 'select-checkbox.component.scss' ], | ||
10 | templateUrl: './select-checkbox.component.html', | ||
11 | providers: [ | ||
12 | { | ||
13 | provide: NG_VALUE_ACCESSOR, | ||
14 | useExisting: forwardRef(() => SelectCheckboxComponent), | ||
15 | multi: true | ||
16 | } | ||
17 | ] | ||
18 | }) | ||
19 | export class SelectCheckboxComponent implements ControlValueAccessor { | ||
20 | @Input() availableItems: SelectOptionsItem[] = [] | ||
21 | @Input() selectedItems: ItemSelectCheckboxValue[] = [] | ||
22 | @Input() selectableGroup: boolean | ||
23 | @Input() selectableGroupAsModel: boolean | ||
24 | @Input() maxSelectedItems: number | ||
25 | |||
26 | propagateChange = (_: any) => { /* empty */ } | ||
27 | |||
28 | writeValue (items: ItemSelectCheckboxValue[]) { | ||
29 | if (Array.isArray(items)) { | ||
30 | this.selectedItems = items.map(i => { | ||
31 | if (typeof i === 'string' || typeof i === 'number') { | ||
32 | return i + '' | ||
33 | } | ||
34 | |||
35 | if (i.group) { | ||
36 | return { group: i.group } | ||
37 | } | ||
38 | |||
39 | return { id: i.id + '' } | ||
40 | }) | ||
41 | } else { | ||
42 | this.selectedItems = items | ||
43 | } | ||
44 | |||
45 | this.propagateChange(this.selectedItems) | ||
46 | } | ||
47 | |||
48 | registerOnChange (fn: (_: any) => void) { | ||
49 | this.propagateChange = fn | ||
50 | } | ||
51 | |||
52 | registerOnTouched () { | ||
53 | // Unused | ||
54 | } | ||
55 | |||
56 | onModelChange () { | ||
57 | this.propagateChange(this.selectedItems) | ||
58 | } | ||
59 | |||
60 | compareFn (item: SelectOptionsItem, selected: ItemSelectCheckboxValue) { | ||
61 | if (typeof selected === 'string') { | ||
62 | return item.id === selected | ||
63 | } | ||
64 | |||
65 | if (this.selectableGroup && item.group && selected.group) { | ||
66 | return item.group === selected.group | ||
67 | } | ||
68 | |||
69 | if (selected.id && item.id) { | ||
70 | return item.id === selected.id | ||
71 | } | ||
72 | |||
73 | return false | ||
74 | } | ||
75 | } | ||