]>
Commit | Line | Data |
---|---|---|
cdeddff1 | 1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' |
27ec473f | 2 | import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser' |
67ed6552 | 3 | import { Notifier, ServerService } from '@app/core' |
cdeddff1 | 4 | import { Account, VideoChannel } from '@app/shared/shared-main' |
0e4ca570 | 5 | import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' |
b4c3c51d | 6 | import { getBytes } from '@root-helpers/bytes' |
52d9f792 C |
7 | |
8 | @Component({ | |
cdeddff1 C |
9 | selector: 'my-actor-avatar-edit', |
10 | templateUrl: './actor-avatar-edit.component.html', | |
11 | styleUrls: [ | |
12 | './actor-image-edit.scss', | |
13 | './actor-avatar-edit.component.scss' | |
14 | ] | |
52d9f792 | 15 | }) |
cdeddff1 | 16 | export class ActorAvatarEditComponent implements OnInit { |
2f5d2ec5 | 17 | @ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement> |
1ea7da81 | 18 | @ViewChild('avatarPopover') avatarPopover: NgbPopover |
52d9f792 C |
19 | |
20 | @Input() actor: VideoChannel | Account | |
cdeddff1 C |
21 | @Input() editable = true |
22 | @Input() displaySubscribers = true | |
23 | @Input() displayUsername = true | |
27ec473f | 24 | @Input() previewImage = false |
52d9f792 C |
25 | |
26 | @Output() avatarChange = new EventEmitter<FormData>() | |
1ea7da81 | 27 | @Output() avatarDelete = new EventEmitter<void>() |
52d9f792 | 28 | |
0e4ca570 C |
29 | avatarFormat = '' |
30 | maxAvatarSize = 0 | |
31 | avatarExtensions = '' | |
32 | ||
27ec473f C |
33 | preview: SafeResourceUrl |
34 | ||
52d9f792 | 35 | constructor ( |
27ec473f | 36 | private sanitizer: DomSanitizer, |
52d9f792 | 37 | private serverService: ServerService, |
66357162 | 38 | private notifier: Notifier |
123f6193 | 39 | ) { } |
52d9f792 | 40 | |
ba430d75 | 41 | ngOnInit (): void { |
2989628b C |
42 | const config = this.serverService.getHTMLConfig() |
43 | ||
44 | this.maxAvatarSize = config.avatar.file.size.max | |
45 | this.avatarExtensions = config.avatar.file.extensions.join(', ') | |
46 | ||
47 | this.avatarFormat = `${$localize`max size`}: 192*192px, ` + | |
48 | `${getBytes(this.maxAvatarSize)} ${$localize`extensions`}: ${this.avatarExtensions}` | |
ba430d75 C |
49 | } |
50 | ||
1ea7da81 RK |
51 | onAvatarChange (input: HTMLInputElement) { |
52 | this.avatarfileInput = new ElementRef(input) | |
53 | ||
9df52d66 | 54 | const avatarfile = this.avatarfileInput.nativeElement.files[0] |
52d9f792 | 55 | if (avatarfile.size > this.maxAvatarSize) { |
1ea7da81 | 56 | this.notifier.error('Error', $localize`This image is too large.`) |
52d9f792 C |
57 | return |
58 | } | |
59 | ||
60 | const formData = new FormData() | |
61 | formData.append('avatarfile', avatarfile) | |
1ea7da81 | 62 | this.avatarPopover?.close() |
52d9f792 | 63 | this.avatarChange.emit(formData) |
27ec473f C |
64 | |
65 | if (this.previewImage) { | |
66 | this.preview = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(avatarfile)) | |
67 | } | |
52d9f792 C |
68 | } |
69 | ||
1ea7da81 | 70 | deleteAvatar () { |
27ec473f | 71 | this.preview = undefined |
1ea7da81 RK |
72 | this.avatarDelete.emit() |
73 | } | |
74 | ||
0e4ca570 | 75 | hasAvatar () { |
27ec473f | 76 | return !!this.preview || !!this.actor.avatar |
1ea7da81 | 77 | } |
deb8b9cd C |
78 | |
79 | isChannel () { | |
80 | return !!(this.actor as VideoChannel).ownerAccount | |
81 | } | |
746018f6 C |
82 | |
83 | getChannel (): VideoChannel { | |
84 | if (this.isChannel()) return this.actor as VideoChannel | |
85 | ||
86 | return undefined | |
87 | } | |
88 | ||
89 | getAccount (): Account { | |
90 | if (this.isChannel()) return undefined | |
91 | ||
92 | return this.actor as Account | |
93 | } | |
52d9f792 | 94 | } |