]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-actor-image/actor-avatar.component.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-actor-image / actor-avatar.component.ts
index fdc8268d88b36927af0b85020bd1634cc16e7e86..f1c1aa03f388215b06a849776abb9ef414b4b1a0 100644 (file)
@@ -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,57 +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.channel) return VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
+  ngOnChanges () {
+    this.buildAvatarUrl()
+    this.buildClasses()
+  }
+
+  private buildClasses () {
+    this.classes = [ 'avatar' ]
+
+    if (this.size) {
+      this.classes.push(`avatar-${this.size}`)
+    }
+
+    if (this.isChannel()) {
+      this.classes.push('channel')
+    } else {
+      this.classes.push('account')
+    }
 
-    return Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
+    // No avatar, use actor name initial
+    if (this.displayActorInitial()) {
+      this.classes.push('initial')
+      this.classes.push(this.getColorTheme())
+    }
+  }
+
+  private buildDefaultAvatarUrl () {
+    this.defaultAvatarUrl = this.isChannel()
+      ? VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
+      : Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
   }
 
-  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 buildAvatarUrl () {
+    if (!this.actor) {
+      this.avatarUrl = ''
+      return
+    }
 
-    return ''
+    if (this.isAccount()) {
+      this.avatarUrl = Account.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
+      return
+    }
+
+    if (this.isChannel()) {
+      this.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
+      return
+    }
+
+    this.avatarUrl = ''
   }
 
-  get initial () {
-    const name = this.account?.name
-    if (!name) return ''
+  displayImage () {
+    if (this.actorType === 'unlogged') return true
+    if (this.previewImage) return true
 
-    return name.slice(0, 1)
+    return !!(this.actor && this.avatarUrl)
   }
 
-  ngOnChanges () {
-    this.classes = [ 'avatar' ]
+  displayActorInitial () {
+    return !this.displayImage() && this.actor && !this.avatarUrl
+  }
 
-    if (this.size) this.classes.push(`avatar-${this.size}`)
+  displayPlaceholder () {
+    return this.actorType !== 'unlogged' && !this.actor
+  }
 
-    if (this.channel) this.classes.push('channel')
-    else this.classes.push('account')
+  getActorInitial () {
+    const name = this.actor?.name
+    if (!name) return ''
 
-    if (!this.avatarUrl && this.initial) {
-      this.classes.push('initial')
-      this.classes.push(this.getColorTheme())
-    }
+    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 () {
@@ -96,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'
   }
 }