2021-04-07 17:01:29 +02:00
|
|
|
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { Notifier, ServerService } from '@app/core'
|
2021-04-07 17:01:29 +02:00
|
|
|
import { Account, VideoChannel } from '@app/shared/shared-main'
|
2021-01-13 10:52:17 +01:00
|
|
|
import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
|
2020-08-14 17:28:54 +02:00
|
|
|
import { getBytes } from '@root-helpers/bytes'
|
2021-09-08 10:10:51 +02:00
|
|
|
import { imageToDataURL } from '@root-helpers/images'
|
2018-06-29 14:34:04 +02:00
|
|
|
|
|
|
|
@Component({
|
2021-04-07 17:01:29 +02:00
|
|
|
selector: 'my-actor-avatar-edit',
|
|
|
|
templateUrl: './actor-avatar-edit.component.html',
|
|
|
|
styleUrls: [
|
|
|
|
'./actor-image-edit.scss',
|
|
|
|
'./actor-avatar-edit.component.scss'
|
|
|
|
]
|
2018-06-29 14:34:04 +02:00
|
|
|
})
|
2021-04-07 17:01:29 +02:00
|
|
|
export class ActorAvatarEditComponent implements OnInit {
|
2020-02-07 10:00:34 +01:00
|
|
|
@ViewChild('avatarfileInput') avatarfileInput: ElementRef<HTMLInputElement>
|
2021-01-13 09:12:55 +01:00
|
|
|
@ViewChild('avatarPopover') avatarPopover: NgbPopover
|
2018-06-29 14:34:04 +02:00
|
|
|
|
|
|
|
@Input() actor: VideoChannel | Account
|
2021-04-07 17:01:29 +02:00
|
|
|
@Input() editable = true
|
|
|
|
@Input() displaySubscribers = true
|
|
|
|
@Input() displayUsername = true
|
2021-04-08 12:09:54 +02:00
|
|
|
@Input() previewImage = false
|
2018-06-29 14:34:04 +02:00
|
|
|
|
|
|
|
@Output() avatarChange = new EventEmitter<FormData>()
|
2021-01-13 09:12:55 +01:00
|
|
|
@Output() avatarDelete = new EventEmitter<void>()
|
2018-06-29 14:34:04 +02:00
|
|
|
|
2021-01-13 10:52:17 +01:00
|
|
|
avatarFormat = ''
|
|
|
|
maxAvatarSize = 0
|
|
|
|
avatarExtensions = ''
|
|
|
|
|
2021-09-08 10:10:51 +02:00
|
|
|
preview: string
|
2021-04-08 12:09:54 +02:00
|
|
|
|
2018-06-29 14:34:04 +02:00
|
|
|
constructor (
|
|
|
|
private serverService: ServerService,
|
2020-08-12 10:40:04 +02:00
|
|
|
private notifier: Notifier
|
2020-11-25 09:26:31 +01:00
|
|
|
) { }
|
2018-06-29 14:34:04 +02:00
|
|
|
|
2019-12-18 15:31:54 +01:00
|
|
|
ngOnInit (): void {
|
2021-06-04 13:31:41 +02:00
|
|
|
const config = this.serverService.getHTMLConfig()
|
|
|
|
|
|
|
|
this.maxAvatarSize = config.avatar.file.size.max
|
|
|
|
this.avatarExtensions = config.avatar.file.extensions.join(', ')
|
|
|
|
|
|
|
|
this.avatarFormat = `${$localize`max size`}: 192*192px, ` +
|
|
|
|
`${getBytes(this.maxAvatarSize)} ${$localize`extensions`}: ${this.avatarExtensions}`
|
2019-12-18 15:31:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 09:12:55 +01:00
|
|
|
onAvatarChange (input: HTMLInputElement) {
|
|
|
|
this.avatarfileInput = new ElementRef(input)
|
|
|
|
|
2021-08-17 14:42:53 +02:00
|
|
|
const avatarfile = this.avatarfileInput.nativeElement.files[0]
|
2018-06-29 14:34:04 +02:00
|
|
|
if (avatarfile.size > this.maxAvatarSize) {
|
2021-01-13 09:12:55 +01:00
|
|
|
this.notifier.error('Error', $localize`This image is too large.`)
|
2018-06-29 14:34:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('avatarfile', avatarfile)
|
2021-01-13 09:12:55 +01:00
|
|
|
this.avatarPopover?.close()
|
2018-06-29 14:34:04 +02:00
|
|
|
this.avatarChange.emit(formData)
|
2021-04-08 12:09:54 +02:00
|
|
|
|
|
|
|
if (this.previewImage) {
|
2021-09-08 10:10:51 +02:00
|
|
|
imageToDataURL(avatarfile).then(result => this.preview = result)
|
2021-04-08 12:09:54 +02:00
|
|
|
}
|
2018-06-29 14:34:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 09:12:55 +01:00
|
|
|
deleteAvatar () {
|
2021-04-08 12:09:54 +02:00
|
|
|
this.preview = undefined
|
2021-01-13 09:12:55 +01:00
|
|
|
this.avatarDelete.emit()
|
|
|
|
}
|
|
|
|
|
2021-01-13 10:52:17 +01:00
|
|
|
hasAvatar () {
|
2021-04-08 12:09:54 +02:00
|
|
|
return !!this.preview || !!this.actor.avatar
|
2021-01-13 09:12:55 +01:00
|
|
|
}
|
2021-03-29 16:45:35 +02:00
|
|
|
|
|
|
|
isChannel () {
|
|
|
|
return !!(this.actor as VideoChannel).ownerAccount
|
|
|
|
}
|
2021-04-28 11:49:34 +02:00
|
|
|
|
|
|
|
getChannel (): VideoChannel {
|
|
|
|
if (this.isChannel()) return this.actor as VideoChannel
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
getAccount (): Account {
|
|
|
|
if (this.isChannel()) return undefined
|
|
|
|
|
|
|
|
return this.actor as Account
|
|
|
|
}
|
2018-06-29 14:34:04 +02:00
|
|
|
}
|