aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-forms/reactive-file.component.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-06-23 14:10:17 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-06-23 16:00:49 +0200
commit67ed6552b831df66713bac9e672738796128d33f (patch)
tree59c97d41e0b49d75a90aa3de987968ab9b1ff447 /client/src/app/shared/shared-forms/reactive-file.component.ts
parent0c4bacbff53bc732f5a2677d62a6ead7752e2405 (diff)
downloadPeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.gz
PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.zst
PeerTube-67ed6552b831df66713bac9e672738796128d33f.zip
Reorganize client shared modules
Diffstat (limited to 'client/src/app/shared/shared-forms/reactive-file.component.ts')
-rw-r--r--client/src/app/shared/shared-forms/reactive-file.component.ts91
1 files changed, 91 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-forms/reactive-file.component.ts b/client/src/app/shared/shared-forms/reactive-file.component.ts
new file mode 100644
index 000000000..9ebf487ce
--- /dev/null
+++ b/client/src/app/shared/shared-forms/reactive-file.component.ts
@@ -0,0 +1,91 @@
1import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'
2import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3import { Notifier } from '@app/core'
4import { GlobalIconName } from '@app/shared/shared-icons'
5import { I18n } from '@ngx-translate/i18n-polyfill'
6
7@Component({
8 selector: 'my-reactive-file',
9 styleUrls: [ './reactive-file.component.scss' ],
10 templateUrl: './reactive-file.component.html',
11 providers: [
12 {
13 provide: NG_VALUE_ACCESSOR,
14 useExisting: forwardRef(() => ReactiveFileComponent),
15 multi: true
16 }
17 ]
18})
19export class ReactiveFileComponent implements OnInit, ControlValueAccessor {
20 @Input() inputLabel: string
21 @Input() inputName: string
22 @Input() extensions: string[] = []
23 @Input() maxFileSize: number
24 @Input() displayFilename = false
25 @Input() icon: GlobalIconName
26
27 @Output() fileChanged = new EventEmitter<Blob>()
28
29 allowedExtensionsMessage = ''
30 fileInputValue: any
31
32 private file: File
33
34 constructor (
35 private notifier: Notifier,
36 private i18n: I18n
37 ) {}
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) {
50 if (event.target.files && event.target.files.length) {
51 const [ file ] = event.target.files
52
53 if (file.size > this.maxFileSize) {
54 this.notifier.error(this.i18n('This file is too large.'))
55 return
56 }
57
58 const extension = '.' + file.name.split('.').pop()
59 if (this.extensions.includes(extension) === false) {
60 const message = this.i18n(
61 'PeerTube cannot handle this kind of file. Accepted extensions are {{extensions}}.',
62 { extensions: this.allowedExtensionsMessage }
63 )
64 this.notifier.error(message)
65
66 return
67 }
68
69 this.file = file
70
71 this.propagateChange(this.file)
72 this.fileChanged.emit(this.file)
73 }
74 }
75
76 propagateChange = (_: any) => { /* empty */ }
77
78 writeValue (file: any) {
79 this.file = file
80
81 if (!this.file) this.fileInputValue = null
82 }
83
84 registerOnChange (fn: (_: any) => void) {
85 this.propagateChange = fn
86 }
87
88 registerOnTouched () {
89 // Unused
90 }
91}