]>
Commit | Line | Data |
---|---|---|
cdeddff1 | 1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' |
67ed6552 | 2 | import { Notifier, ServerService } from '@app/core' |
cdeddff1 | 3 | import { Account, VideoChannel } from '@app/shared/shared-main' |
0e4ca570 | 4 | import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' |
b4c3c51d | 5 | import { getBytes } from '@root-helpers/bytes' |
0ea2f79d | 6 | import { imageToDataURL } from '@root-helpers/images' |
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 | ||
0ea2f79d | 33 | preview: string |
27ec473f | 34 | |
52d9f792 | 35 | constructor ( |
52d9f792 | 36 | private serverService: ServerService, |
66357162 | 37 | private notifier: Notifier |
123f6193 | 38 | ) { } |
52d9f792 | 39 | |
ba430d75 | 40 | ngOnInit (): void { |
2989628b C |
41 | const config = this.serverService.getHTMLConfig() |
42 | ||
43 | this.maxAvatarSize = config.avatar.file.size.max | |
44 | this.avatarExtensions = config.avatar.file.extensions.join(', ') | |
45 | ||
46 | this.avatarFormat = `${$localize`max size`}: 192*192px, ` + | |
47 | `${getBytes(this.maxAvatarSize)} ${$localize`extensions`}: ${this.avatarExtensions}` | |
ba430d75 C |
48 | } |
49 | ||
1ea7da81 RK |
50 | onAvatarChange (input: HTMLInputElement) { |
51 | this.avatarfileInput = new ElementRef(input) | |
52 | ||
9df52d66 | 53 | const avatarfile = this.avatarfileInput.nativeElement.files[0] |
52d9f792 | 54 | if (avatarfile.size > this.maxAvatarSize) { |
1ea7da81 | 55 | this.notifier.error('Error', $localize`This image is too large.`) |
52d9f792 C |
56 | return |
57 | } | |
58 | ||
59 | const formData = new FormData() | |
60 | formData.append('avatarfile', avatarfile) | |
1ea7da81 | 61 | this.avatarPopover?.close() |
52d9f792 | 62 | this.avatarChange.emit(formData) |
27ec473f C |
63 | |
64 | if (this.previewImage) { | |
0ea2f79d | 65 | imageToDataURL(avatarfile).then(result => this.preview = result) |
27ec473f | 66 | } |
52d9f792 C |
67 | } |
68 | ||
1ea7da81 | 69 | deleteAvatar () { |
27ec473f | 70 | this.preview = undefined |
1ea7da81 RK |
71 | this.avatarDelete.emit() |
72 | } | |
73 | ||
0e4ca570 | 74 | hasAvatar () { |
27ec473f | 75 | return !!this.preview || !!this.actor.avatar |
1ea7da81 | 76 | } |
deb8b9cd C |
77 | |
78 | isChannel () { | |
79 | return !!(this.actor as VideoChannel).ownerAccount | |
80 | } | |
746018f6 C |
81 | |
82 | getChannel (): VideoChannel { | |
83 | if (this.isChannel()) return this.actor as VideoChannel | |
84 | ||
85 | return undefined | |
86 | } | |
87 | ||
88 | getAccount (): Account { | |
89 | if (this.isChannel()) return undefined | |
90 | ||
91 | return this.actor as Account | |
92 | } | |
52d9f792 | 93 | } |