]>
Commit | Line | Data |
---|---|---|
421d935d | 1 | import { AfterContentInit, ChangeDetectorRef, Component, ContentChildren, forwardRef, Input, QueryList, TemplateRef } from '@angular/core' |
0f7fedc3 | 2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
421d935d | 3 | import { PeerTubeTemplateDirective } from '@app/shared/angular/peertube-template.directive' |
0f7fedc3 C |
4 | |
5 | @Component({ | |
6 | selector: 'my-peertube-checkbox', | |
7 | styleUrls: [ './peertube-checkbox.component.scss' ], | |
8 | templateUrl: './peertube-checkbox.component.html', | |
9 | providers: [ | |
10 | { | |
11 | provide: NG_VALUE_ACCESSOR, | |
12 | useExisting: forwardRef(() => PeertubeCheckboxComponent), | |
13 | multi: true | |
14 | } | |
15 | ] | |
16 | }) | |
421d935d | 17 | export class PeertubeCheckboxComponent implements ControlValueAccessor, AfterContentInit { |
0f7fedc3 C |
18 | @Input() checked = false |
19 | @Input() inputName: string | |
20 | @Input() labelText: string | |
8e8eb261 | 21 | @Input() helpPlacement = 'top' |
14e2014a | 22 | @Input() disabled = false |
0f7fedc3 | 23 | |
421d935d C |
24 | @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'label' | 'help'>> |
25 | ||
8dfceec4 C |
26 | // FIXME: https://github.com/angular/angular/issues/10816#issuecomment-307567836 |
27 | @Input() onPushWorkaround = false | |
28 | ||
421d935d C |
29 | labelTemplate: TemplateRef<any> |
30 | helpTemplate: TemplateRef<any> | |
31 | ||
8dfceec4 C |
32 | constructor (private cdr: ChangeDetectorRef) { } |
33 | ||
421d935d C |
34 | ngAfterContentInit () { |
35 | { | |
36 | const t = this.templates.find(t => t.name === 'label') | |
37 | if (t) this.labelTemplate = t.template | |
38 | } | |
39 | ||
40 | { | |
41 | const t = this.templates.find(t => t.name === 'help') | |
42 | if (t) this.helpTemplate = t.template | |
43 | } | |
44 | } | |
45 | ||
0f7fedc3 C |
46 | propagateChange = (_: any) => { /* empty */ } |
47 | ||
48 | writeValue (checked: boolean) { | |
49 | this.checked = checked | |
8dfceec4 C |
50 | |
51 | if (this.onPushWorkaround) { | |
52 | this.cdr.markForCheck() | |
53 | } | |
0f7fedc3 C |
54 | } |
55 | ||
56 | registerOnChange (fn: (_: any) => void) { | |
57 | this.propagateChange = fn | |
58 | } | |
59 | ||
60 | registerOnTouched () { | |
61 | // Unused | |
62 | } | |
63 | ||
64 | onModelChange () { | |
65 | this.propagateChange(this.checked) | |
66 | } | |
67 | ||
68 | setDisabledState (isDisabled: boolean) { | |
14e2014a | 69 | this.disabled = isDisabled |
0f7fedc3 C |
70 | } |
71 | } |