]>
Commit | Line | Data |
---|---|---|
702785a5 C |
1 | import { Component, forwardRef, Input } from '@angular/core' |
2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' | |
3 | import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser' | |
4 | import { ServerService } from '@app/core' | |
702785a5 C |
5 | |
6 | @Component({ | |
7 | selector: 'my-video-image', | |
8 | styleUrls: [ './video-image.component.scss' ], | |
9 | templateUrl: './video-image.component.html', | |
10 | providers: [ | |
11 | { | |
12 | provide: NG_VALUE_ACCESSOR, | |
13 | useExisting: forwardRef(() => VideoImageComponent), | |
14 | multi: true | |
15 | } | |
16 | ] | |
17 | }) | |
702785a5 C |
18 | export class VideoImageComponent implements ControlValueAccessor { |
19 | @Input() inputLabel: string | |
20 | @Input() inputName: string | |
21 | @Input() previewWidth: string | |
22 | @Input() previewHeight: string | |
23 | ||
24 | imageSrc: SafeResourceUrl | |
25 | ||
26 | private file: Blob | |
27 | ||
28 | constructor ( | |
29 | private sanitizer: DomSanitizer, | |
30 | private serverService: ServerService | |
31 | ) {} | |
32 | ||
33 | get videoImageExtensions () { | |
34 | return this.serverService.getConfig().video.image.extensions.join(',') | |
35 | } | |
36 | ||
37 | get maxVideoImageSize () { | |
38 | return this.serverService.getConfig().video.image.size.max | |
39 | } | |
40 | ||
41 | fileChange (event: any) { | |
42 | if (event.target.files && event.target.files.length) { | |
43 | const [ file ] = event.target.files | |
44 | ||
45 | this.file = file | |
46 | this.propagateChange(this.file) | |
47 | this.updatePreview() | |
48 | } | |
49 | } | |
50 | ||
51 | propagateChange = (_: any) => { /* empty */ } | |
52 | ||
53 | writeValue (file: any) { | |
54 | this.file = file | |
55 | this.updatePreview() | |
56 | } | |
57 | ||
58 | registerOnChange (fn: (_: any) => void) { | |
59 | this.propagateChange = fn | |
60 | } | |
61 | ||
62 | registerOnTouched () { | |
63 | // Unused | |
64 | } | |
65 | ||
66 | private updatePreview () { | |
67 | if (this.file) { | |
68 | const url = URL.createObjectURL(this.file) | |
69 | this.imageSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url) | |
70 | } | |
71 | } | |
72 | } |