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