]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/forms/reactive-file.component.ts
Refractor notification service
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / forms / reactive-file.component.ts
CommitLineData
40e87e9e
C
1import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'
2import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
f8b2c1b4 3import { Notifier } from '@app/core'
40e87e9e
C
4import { I18n } from '@ngx-translate/i18n-polyfill'
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})
18export class ReactiveFileComponent implements OnInit, ControlValueAccessor {
19 @Input() inputLabel: string
20 @Input() inputName: string
21 @Input() extensions: string[] = []
22 @Input() maxFileSize: number
23 @Input() displayFilename = false
24
25 @Output() fileChanged = new EventEmitter<Blob>()
26
27 allowedExtensionsMessage = ''
772d5642 28 fileInputValue: any
40e87e9e
C
29
30 private file: File
31
32 constructor (
f8b2c1b4 33 private notifier: Notifier,
40e87e9e
C
34 private i18n: I18n
35 ) {}
36
37 get filename () {
38 if (!this.file) return ''
39
40 return this.file.name
41 }
42
43 ngOnInit () {
44 this.allowedExtensionsMessage = this.extensions.join(', ')
45 }
46
47 fileChange (event: any) {
48 if (event.target.files && event.target.files.length) {
49 const [ file ] = event.target.files
50
51 if (file.size > this.maxFileSize) {
f8b2c1b4 52 this.notifier.error(this.i18n('This file is too large.'))
40e87e9e
C
53 return
54 }
55
56 this.file = file
57
58 this.propagateChange(this.file)
59 this.fileChanged.emit(this.file)
60 }
61 }
62
63 propagateChange = (_: any) => { /* empty */ }
64
65 writeValue (file: any) {
66 this.file = file
772d5642
C
67
68 if (!this.file) this.fileInputValue = null
40e87e9e
C
69 }
70
71 registerOnChange (fn: (_: any) => void) {
72 this.propagateChange = fn
73 }
74
75 registerOnTouched () {
76 // Unused
77 }
78}