X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-actor-image%2Factor-avatar.component.ts;h=f1c1aa03f388215b06a849776abb9ef414b4b1a0;hb=d0fbc9fd0a29c37f3ff9b99030351e90b276fe7d;hp=fa4f1e51f99678d04e2f3650119d93854fa8073c;hpb=5b0ec7cddb1ae6dbd2057f067382866f846b882c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/shared-actor-image/actor-avatar.component.ts b/client/src/app/shared/shared-actor-image/actor-avatar.component.ts index fa4f1e51f..f1c1aa03f 100644 --- a/client/src/app/shared/shared-actor-image/actor-avatar.component.ts +++ b/client/src/app/shared/shared-actor-image/actor-avatar.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, OnChanges } from '@angular/core' +import { Component, Input, OnChanges, OnInit } from '@angular/core' import { VideoChannel } from '../shared-main' import { Account } from '../shared-main/account/account.model' @@ -15,11 +15,11 @@ export type ActorAvatarSize = '18' | '25' | '28' | '32' | '34' | '35' | '36' | ' styleUrls: [ './actor-avatar.component.scss' ], templateUrl: './actor-avatar.component.html' }) -export class ActorAvatarComponent implements OnChanges { +export class ActorAvatarComponent implements OnInit, OnChanges { private _title: string - @Input() account: ActorInput - @Input() channel: ActorInput + @Input() actor: ActorInput + @Input() actorType: 'channel' | 'account' | 'unlogged' @Input() previewImage: string @@ -36,56 +36,101 @@ export class ActorAvatarComponent implements OnChanges { 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)` + if (this.isAccount()) return $localize`${this.actor.name} (account page)` + if (this.isChannel()) return $localize`${this.actor.name} (channel page)` return '' } classes: string[] = [] + defaultAvatarUrl: string + avatarUrl: string - get alt () { - if (this.account) return $localize`Account avatar` - if (this.channel) return $localize`Channel avatar` + ngOnInit () { + this.buildDefaultAvatarUrl() - return '' + this.buildAvatarUrl() + this.buildClasses() } - get defaultAvatarUrl () { - if (this.account) return Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber()) - if (this.channel) return VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber()) + ngOnChanges () { + this.buildAvatarUrl() + this.buildClasses() } - get avatarUrl () { - 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()) + private buildClasses () { + this.classes = [ 'avatar' ] - return '' - } + if (this.size) { + this.classes.push(`avatar-${this.size}`) + } - get initial () { - const name = this.account?.name - if (!name) return '' + if (this.isChannel()) { + this.classes.push('channel') + } else { + this.classes.push('account') + } - return name.slice(0, 1) + // No avatar, use actor name initial + if (this.displayActorInitial()) { + this.classes.push('initial') + this.classes.push(this.getColorTheme()) + } } - ngOnChanges () { - this.classes = [ 'avatar' ] + private buildDefaultAvatarUrl () { + this.defaultAvatarUrl = this.isChannel() + ? VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber()) + : Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber()) + } - if (this.size) this.classes.push(`avatar-${this.size}`) + private buildAvatarUrl () { + if (!this.actor) { + this.avatarUrl = '' + return + } - if (this.channel) this.classes.push('channel') - else this.classes.push('account') + if (this.isAccount()) { + this.avatarUrl = Account.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber()) + return + } - if (!this.avatarUrl && this.initial) { - this.classes.push('initial') - this.classes.push(this.getColorTheme()) + if (this.isChannel()) { + this.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber()) + return } + + this.avatarUrl = '' + } + + displayImage () { + if (this.actorType === 'unlogged') return true + if (this.previewImage) return true + + return !!(this.actor && this.avatarUrl) + } + + displayActorInitial () { + return !this.displayImage() && this.actor && !this.avatarUrl + } + + displayPlaceholder () { + return this.actorType !== 'unlogged' && !this.actor + } + + getActorInitial () { + const name = this.actor?.name + if (!name) return '' + + return name.slice(0, 1) + } + + private isAccount () { + return this.actorType === 'account' } - hasActor () { - return !!this.account || !!this.channel + private isChannel () { + return this.actorType === 'channel' } private getSizeNumber () { @@ -95,23 +140,23 @@ export class ActorAvatarComponent implements OnChanges { } private getColorTheme () { - const initialLowercase = this.initial.toLowerCase() + const initialLowercase = this.getActorInitial().toLowerCase() // Keep consistency with CSS const themes = { '0123456789abc': 'blue', - def: 'green', - ghi: 'purple', - jkl: 'gray', - mno: 'yellow', - pqr: 'orange', - stvu: 'red', - wxyz: 'dark-blue' + 'def': 'green', + 'ghi': 'purple', + 'jkl': 'gray', + 'mno': 'yellow', + 'pqr': 'orange', + 'stvu': 'red', + 'wxyz': 'dark-blue' } const theme = Object.keys(themes) .find(chars => chars.includes(initialLowercase)) - return themes[theme] + return themes[theme] || 'blue' } }