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