]>
Commit | Line | Data |
---|---|---|
7b992a86 | 1 | import { Component, forwardRef, Input, OnInit } from '@angular/core' |
702785a5 | 2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
702785a5 | 3 | import { ServerService } from '@app/core' |
0ea2f79d | 4 | import { imageToDataURL } from '@root-helpers/images' |
2989628b | 5 | import { HTMLServerConfig } from '@shared/models' |
94676e63 | 6 | import { BytesPipe } from '../shared-main' |
702785a5 C |
7 | |
8 | @Component({ | |
7b992a86 C |
9 | selector: 'my-preview-upload', |
10 | styleUrls: [ './preview-upload.component.scss' ], | |
11 | templateUrl: './preview-upload.component.html', | |
702785a5 C |
12 | providers: [ |
13 | { | |
14 | provide: NG_VALUE_ACCESSOR, | |
7b992a86 | 15 | useExisting: forwardRef(() => PreviewUploadComponent), |
702785a5 C |
16 | multi: true |
17 | } | |
18 | ] | |
19 | }) | |
7b992a86 | 20 | export class PreviewUploadComponent implements OnInit, ControlValueAccessor { |
702785a5 C |
21 | @Input() inputLabel: string |
22 | @Input() inputName: string | |
23 | @Input() previewWidth: string | |
24 | @Input() previewHeight: string | |
25 | ||
0ea2f79d | 26 | imageSrc: string |
7b992a86 | 27 | allowedExtensionsMessage = '' |
6f02515e | 28 | maxSizeText: string |
702785a5 | 29 | |
2989628b | 30 | private serverConfig: HTMLServerConfig |
6f02515e | 31 | private bytesPipe: BytesPipe |
be27ef3b | 32 | private file: Blob |
702785a5 C |
33 | |
34 | constructor ( | |
66357162 | 35 | private serverService: ServerService |
6f02515e RK |
36 | ) { |
37 | this.bytesPipe = new BytesPipe() | |
66357162 | 38 | this.maxSizeText = $localize`max size` |
6f02515e | 39 | } |
702785a5 C |
40 | |
41 | get videoImageExtensions () { | |
ba430d75 | 42 | return this.serverConfig.video.image.extensions |
702785a5 C |
43 | } |
44 | ||
45 | get maxVideoImageSize () { | |
ba430d75 | 46 | return this.serverConfig.video.image.size.max |
702785a5 C |
47 | } |
48 | ||
6f02515e RK |
49 | get maxVideoImageSizeInBytes () { |
50 | return this.bytesPipe.transform(this.maxVideoImageSize) | |
51 | } | |
52 | ||
a2c5cd4a | 53 | getReactiveFileButtonTooltip () { |
851e808d | 54 | return $localize`(extensions: ${this.videoImageExtensions}, ${this.maxSizeText}\: ${this.maxVideoImageSizeInBytes})` |
a2c5cd4a C |
55 | } |
56 | ||
7b992a86 | 57 | ngOnInit () { |
2989628b | 58 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 59 | |
7b992a86 C |
60 | this.allowedExtensionsMessage = this.videoImageExtensions.join(', ') |
61 | } | |
62 | ||
be27ef3b | 63 | onFileChanged (file: Blob) { |
40e87e9e | 64 | this.file = file |
0c237b19 | 65 | |
40e87e9e C |
66 | this.propagateChange(this.file) |
67 | this.updatePreview() | |
702785a5 C |
68 | } |
69 | ||
70 | propagateChange = (_: any) => { /* empty */ } | |
71 | ||
72 | writeValue (file: any) { | |
73 | this.file = file | |
74 | this.updatePreview() | |
75 | } | |
76 | ||
77 | registerOnChange (fn: (_: any) => void) { | |
78 | this.propagateChange = fn | |
79 | } | |
80 | ||
81 | registerOnTouched () { | |
82 | // Unused | |
83 | } | |
84 | ||
85 | private updatePreview () { | |
86 | if (this.file) { | |
0ea2f79d | 87 | imageToDataURL(this.file).then(result => this.imageSrc = result) |
702785a5 C |
88 | } |
89 | } | |
90 | } |