diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-11 16:07:53 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-08-11 16:18:42 +0200 |
commit | 52c4976fcf4ee255a3af68ff9778e4f5c4f84bd4 (patch) | |
tree | 887d2b6106548ad23cf016d82baf1977198027d9 /client/src/app/shared/shared-forms/select/select-channel.component.ts | |
parent | 3d25d5de33d8aa0ba00d7514522b25d22bf0e0a1 (diff) | |
download | PeerTube-52c4976fcf4ee255a3af68ff9778e4f5c4f84bd4.tar.gz PeerTube-52c4976fcf4ee255a3af68ff9778e4f5c4f84bd4.tar.zst PeerTube-52c4976fcf4ee255a3af68ff9778e4f5c4f84bd4.zip |
Use ng select for multiselect
Diffstat (limited to 'client/src/app/shared/shared-forms/select/select-channel.component.ts')
-rw-r--r-- | client/src/app/shared/shared-forms/select/select-channel.component.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-forms/select/select-channel.component.ts b/client/src/app/shared/shared-forms/select/select-channel.component.ts new file mode 100644 index 000000000..1b0db9b6f --- /dev/null +++ b/client/src/app/shared/shared-forms/select/select-channel.component.ts | |||
@@ -0,0 +1,58 @@ | |||
1 | import { Component, forwardRef, Input } from '@angular/core' | ||
2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' | ||
3 | import { Actor } from '@app/shared/shared-main/account/actor.model' | ||
4 | |||
5 | export type SelectChannelItem = { | ||
6 | id: number | ||
7 | label: string | ||
8 | support: string | ||
9 | avatarPath?: string | ||
10 | } | ||
11 | |||
12 | @Component({ | ||
13 | selector: 'my-select-channel', | ||
14 | styleUrls: [ './select-shared.component.scss' ], | ||
15 | templateUrl: './select-channel.component.html', | ||
16 | providers: [ | ||
17 | { | ||
18 | provide: NG_VALUE_ACCESSOR, | ||
19 | useExisting: forwardRef(() => SelectChannelComponent), | ||
20 | multi: true | ||
21 | } | ||
22 | ] | ||
23 | }) | ||
24 | export class SelectChannelComponent implements ControlValueAccessor { | ||
25 | @Input() items: SelectChannelItem[] = [] | ||
26 | |||
27 | selectedId: number | ||
28 | |||
29 | // ng-select options | ||
30 | bindLabel = 'label' | ||
31 | bindValue = 'id' | ||
32 | clearable = false | ||
33 | searchable = false | ||
34 | |||
35 | get channels () { | ||
36 | return this.items.map(c => Object.assign(c, { | ||
37 | avatarPath: c.avatarPath ? c.avatarPath : Actor.GET_DEFAULT_AVATAR_URL() | ||
38 | })) | ||
39 | } | ||
40 | |||
41 | propagateChange = (_: any) => { /* empty */ } | ||
42 | |||
43 | writeValue (id: number) { | ||
44 | this.selectedId = id | ||
45 | } | ||
46 | |||
47 | registerOnChange (fn: (_: any) => void) { | ||
48 | this.propagateChange = fn | ||
49 | } | ||
50 | |||
51 | registerOnTouched () { | ||
52 | // Unused | ||
53 | } | ||
54 | |||
55 | onModelChange () { | ||
56 | this.propagateChange(this.selectedId) | ||
57 | } | ||
58 | } | ||