]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-actor-image/actor-avatar.component.ts
a4adfd1b73925eceae4706f6a808b34c510854f1
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-actor-image / actor-avatar.component.ts
1 import { Component, Input } from '@angular/core'
2 import { SafeResourceUrl } from '@angular/platform-browser'
3 import { VideoChannel } from '../shared-main'
4 import { Account } from '../shared-main/account/account.model'
5
6 type ActorInput = {
7 name: string
8 avatar?: { url?: string, path: string }
9 url: string
10 }
11
12 export type ActorAvatarSize = '18' | '25' | '32' | '34' | '36' | '40' | '100' | '120'
13
14 @Component({
15 selector: 'my-actor-avatar',
16 styleUrls: [ './actor-avatar.component.scss' ],
17 templateUrl: './actor-avatar.component.html'
18 })
19 export class ActorAvatarComponent {
20 private _title: string
21
22 @Input() account: ActorInput
23 @Input() channel: ActorInput
24
25 @Input() previewImage: SafeResourceUrl
26
27 @Input() size: ActorAvatarSize
28
29 // Use an external link
30 @Input() href: string
31 // Use routerLink
32 @Input() internalHref: string | any[]
33
34 @Input() set title (value) {
35 this._title = value
36 }
37
38 get title () {
39 if (this._title) return this._title
40 if (this.account) return $localize`${this.account.name} (account page)`
41 if (this.channel) return $localize`${this.channel.name} (channel page)`
42
43 return ''
44 }
45
46 get alt () {
47 if (this.account) return $localize`Account avatar`
48 if (this.channel) return $localize`Channel avatar`
49
50 return ''
51 }
52
53 get defaultAvatarUrl () {
54 if (this.channel) return VideoChannel.GET_DEFAULT_AVATAR_URL()
55
56 return Account.GET_DEFAULT_AVATAR_URL()
57 }
58
59 get avatarUrl () {
60 if (this.account) return Account.GET_ACTOR_AVATAR_URL(this.account)
61 if (this.channel) return VideoChannel.GET_ACTOR_AVATAR_URL(this.channel)
62
63 return ''
64 }
65
66 get initial () {
67 const name = this.account?.name
68 if (!name) return ''
69
70 return name.slice(0, 1)
71 }
72
73 getClass (type: 'avatar' | 'initial') {
74 const base = [ 'avatar' ]
75
76 if (this.size) base.push(`avatar-${this.size}`)
77
78 if (this.channel) base.push('channel')
79 else base.push('account')
80
81 if (type === 'initial' && this.initial) {
82 base.push('initial')
83 base.push(this.getColorTheme())
84 }
85
86 return base
87 }
88
89 hasActor () {
90 return !!this.account || !!this.channel
91 }
92
93 private getColorTheme () {
94 // Keep consistency with CSS
95 const themes = {
96 abc: 'blue',
97 def: 'green',
98 ghi: 'purple',
99 jkl: 'gray',
100 mno: 'yellow',
101 pqr: 'orange',
102 stvu: 'red',
103 wxyz: 'dark-blue'
104 }
105
106 const theme = Object.keys(themes)
107 .find(chars => chars.includes(this.initial))
108
109 return themes[theme]
110 }
111 }