PeerTube/client/src/app/shared/shared-actor-image/actor-avatar.component.ts

119 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-06-15 14:59:25 +02:00
import { Component, Input, OnChanges } from '@angular/core'
2021-04-28 11:49:34 +02:00
import { VideoChannel } from '../shared-main'
import { Account } from '../shared-main/account/account.model'
type ActorInput = {
name: string
avatars: { width: number, url?: string, path: string }[]
2021-04-28 11:49:34 +02:00
url: string
}
export type ActorAvatarSize = '18' | '25' | '28' | '32' | '34' | '35' | '36' | '40' | '48' | '75' | '80' | '100' | '120'
2021-05-05 09:44:24 +02:00
2021-04-28 11:49:34 +02:00
@Component({
selector: 'my-actor-avatar',
styleUrls: [ './actor-avatar.component.scss' ],
templateUrl: './actor-avatar.component.html'
})
2022-06-15 14:59:25 +02:00
export class ActorAvatarComponent implements OnChanges {
2021-08-17 14:42:53 +02:00
private _title: string
2021-04-28 11:49:34 +02:00
@Input() account: ActorInput
@Input() channel: ActorInput
2021-09-08 10:10:51 +02:00
@Input() previewImage: string
2021-04-28 11:49:34 +02:00
2022-05-02 13:46:09 +02:00
@Input() size: ActorAvatarSize
2021-04-28 11:49:34 +02:00
// Use an external link
@Input() href: string
// Use routerLink
@Input() internalHref: string | any[]
@Input() set title (value) {
this._title = value
}
get title () {
if (this._title) return this._title
if (this.account) return $localize`${this.account.name} (account page)`
if (this.channel) return $localize`${this.channel.name} (channel page)`
return ''
}
2022-06-15 14:59:25 +02:00
classes: string[] = []
2021-04-28 11:49:34 +02:00
get alt () {
if (this.account) return $localize`Account avatar`
if (this.channel) return $localize`Channel avatar`
return ''
}
get defaultAvatarUrl () {
2022-05-02 13:46:09 +02:00
if (this.channel) return VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
2022-06-27 09:45:16 +02:00
return Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
2021-04-28 11:49:34 +02:00
}
get avatarUrl () {
2022-05-02 13:46:09 +02:00
if (this.account) return Account.GET_ACTOR_AVATAR_URL(this.account, this.getSizeNumber())
if (this.channel) return VideoChannel.GET_ACTOR_AVATAR_URL(this.channel, this.getSizeNumber())
2021-04-28 11:49:34 +02:00
return ''
}
get initial () {
const name = this.account?.name
if (!name) return ''
return name.slice(0, 1)
}
2022-06-15 14:59:25 +02:00
ngOnChanges () {
this.classes = [ 'avatar' ]
2021-08-17 14:42:53 +02:00
2022-06-15 14:59:25 +02:00
if (this.size) this.classes.push(`avatar-${this.size}`)
2021-08-17 14:42:53 +02:00
2022-06-15 14:59:25 +02:00
if (this.channel) this.classes.push('channel')
else this.classes.push('account')
2021-08-17 14:42:53 +02:00
2022-06-15 14:59:25 +02:00
if (!this.avatarUrl && this.initial) {
this.classes.push('initial')
this.classes.push(this.getColorTheme())
2021-08-17 14:42:53 +02:00
}
}
2021-04-28 11:49:34 +02:00
hasActor () {
return !!this.account || !!this.channel
}
2022-05-02 13:46:09 +02:00
private getSizeNumber () {
if (this.size) return +this.size
return undefined
}
2021-04-28 11:49:34 +02:00
private getColorTheme () {
const initialLowercase = this.initial.toLowerCase()
2021-04-28 11:49:34 +02:00
// Keep consistency with CSS
const themes = {
'0123456789abc': 'blue',
2021-04-28 11:49:34 +02:00
def: 'green',
ghi: 'purple',
jkl: 'gray',
mno: 'yellow',
pqr: 'orange',
stvu: 'red',
2021-04-28 11:49:34 +02:00
wxyz: 'dark-blue'
}
const theme = Object.keys(themes)
.find(chars => chars.includes(initialLowercase))
2021-04-28 11:49:34 +02:00
return themes[theme]
}
}