]>
Commit | Line | Data |
---|---|---|
40e87e9e C |
1 | import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core' |
2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' | |
f8b2c1b4 | 3 | import { Notifier } from '@app/core' |
67ed6552 | 4 | import { GlobalIconName } from '@app/shared/shared-icons' |
40e87e9e C |
5 | |
6 | @Component({ | |
7 | selector: 'my-reactive-file', | |
8 | styleUrls: [ './reactive-file.component.scss' ], | |
9 | templateUrl: './reactive-file.component.html', | |
10 | providers: [ | |
11 | { | |
12 | provide: NG_VALUE_ACCESSOR, | |
13 | useExisting: forwardRef(() => ReactiveFileComponent), | |
14 | multi: true | |
15 | } | |
16 | ] | |
17 | }) | |
18 | export class ReactiveFileComponent implements OnInit, ControlValueAccessor { | |
19 | @Input() inputLabel: string | |
20 | @Input() inputName: string | |
21 | @Input() extensions: string[] = [] | |
22 | @Input() maxFileSize: number | |
a2c5cd4a | 23 | |
40e87e9e | 24 | @Input() displayFilename = false |
a2c5cd4a C |
25 | @Input() displayReset = false |
26 | ||
7b992a86 | 27 | @Input() icon: GlobalIconName |
a2c5cd4a | 28 | @Input() buttonTooltip: string |
40e87e9e C |
29 | |
30 | @Output() fileChanged = new EventEmitter<Blob>() | |
31 | ||
32 | allowedExtensionsMessage = '' | |
772d5642 | 33 | fileInputValue: any |
40e87e9e C |
34 | |
35 | private file: File | |
36 | ||
66357162 | 37 | constructor (private notifier: Notifier) { } |
40e87e9e C |
38 | |
39 | get filename () { | |
40 | if (!this.file) return '' | |
41 | ||
42 | return this.file.name | |
43 | } | |
44 | ||
45 | ngOnInit () { | |
46 | this.allowedExtensionsMessage = this.extensions.join(', ') | |
47 | } | |
48 | ||
49 | fileChange (event: any) { | |
9df52d66 | 50 | if (event.target.files?.length) { |
40e87e9e C |
51 | const [ file ] = event.target.files |
52 | ||
53 | if (file.size > this.maxFileSize) { | |
66357162 | 54 | this.notifier.error($localize`This file is too large.`) |
40e87e9e C |
55 | return |
56 | } | |
57 | ||
b5487ff4 | 58 | const extension = '.' + file.name.split('.').pop() |
88cfa3e8 | 59 | if (this.extensions.includes(extension.toLowerCase()) === false) { |
8a2166c9 | 60 | const message = $localize`PeerTube cannot handle this kind of file. Accepted extensions are ${this.allowedExtensionsMessage}.` |
b5487ff4 C |
61 | this.notifier.error(message) |
62 | ||
63 | return | |
64 | } | |
65 | ||
40e87e9e C |
66 | this.file = file |
67 | ||
68 | this.propagateChange(this.file) | |
40e87e9e | 69 | } |
a2c5cd4a C |
70 | this.fileChanged.emit(this.file) |
71 | } | |
72 | ||
73 | reset () { | |
74 | this.writeValue(undefined) | |
75 | this.propagateChange(undefined) | |
76 | this.fileChanged.emit(undefined) | |
40e87e9e C |
77 | } |
78 | ||
79 | propagateChange = (_: any) => { /* empty */ } | |
80 | ||
81 | writeValue (file: any) { | |
82 | this.file = file | |
772d5642 C |
83 | |
84 | if (!this.file) this.fileInputValue = null | |
40e87e9e C |
85 | } |
86 | ||
87 | registerOnChange (fn: (_: any) => void) { | |
88 | this.propagateChange = fn | |
89 | } | |
90 | ||
91 | registerOnTouched () { | |
92 | // Unused | |
93 | } | |
94 | } |