]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-actor-image/actor-avatar.component.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-actor-image / actor-avatar.component.ts
CommitLineData
5b0ec7cd 1import { Component, Input, OnChanges } from '@angular/core'
746018f6
C
2import { VideoChannel } from '../shared-main'
3import { Account } from '../shared-main/account/account.model'
4
5type ActorInput = {
6 name: string
d0800f76 7 avatars: { width: number, url?: string, path: string }[]
746018f6
C
8 url: string
9}
10
d0800f76 11export type ActorAvatarSize = '18' | '25' | '28' | '32' | '34' | '35' | '36' | '40' | '48' | '75' | '80' | '100' | '120'
06ec4bdd 12
746018f6
C
13@Component({
14 selector: 'my-actor-avatar',
15 styleUrls: [ './actor-avatar.component.scss' ],
16 templateUrl: './actor-avatar.component.html'
17})
5b0ec7cd 18export class ActorAvatarComponent implements OnChanges {
9df52d66
C
19 private _title: string
20
746018f6
C
21 @Input() account: ActorInput
22 @Input() channel: ActorInput
23
0ea2f79d 24 @Input() previewImage: string
746018f6 25
4428ad54 26 @Input() size: ActorAvatarSize
746018f6
C
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
746018f6
C
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
5b0ec7cd
C
45 classes: string[] = []
46
746018f6
C
47 get alt () {
48 if (this.account) return $localize`Account avatar`
49 if (this.channel) return $localize`Channel avatar`
50
51 return ''
52 }
53
746018f6 54 get defaultAvatarUrl () {
4428ad54 55 if (this.channel) return VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
b713976a
C
56
57 return Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
746018f6
C
58 }
59
60 get avatarUrl () {
4428ad54
C
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())
746018f6
C
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
5b0ec7cd
C
74 ngOnChanges () {
75 this.classes = [ 'avatar' ]
9df52d66 76
5b0ec7cd 77 if (this.size) this.classes.push(`avatar-${this.size}`)
9df52d66 78
5b0ec7cd
C
79 if (this.channel) this.classes.push('channel')
80 else this.classes.push('account')
9df52d66 81
5b0ec7cd
C
82 if (!this.avatarUrl && this.initial) {
83 this.classes.push('initial')
84 this.classes.push(this.getColorTheme())
9df52d66 85 }
9df52d66
C
86 }
87
746018f6
C
88 hasActor () {
89 return !!this.account || !!this.channel
90 }
91
4428ad54
C
92 private getSizeNumber () {
93 if (this.size) return +this.size
94
95 return undefined
96 }
97
746018f6 98 private getColorTheme () {
f41efa52
C
99 const initialLowercase = this.initial.toLowerCase()
100
746018f6
C
101 // Keep consistency with CSS
102 const themes = {
f41efa52 103 '0123456789abc': 'blue',
746018f6
C
104 def: 'green',
105 ghi: 'purple',
106 jkl: 'gray',
107 mno: 'yellow',
108 pqr: 'orange',
1fd61899 109 stvu: 'red',
746018f6
C
110 wxyz: 'dark-blue'
111 }
112
113 const theme = Object.keys(themes)
f41efa52 114 .find(chars => chars.includes(initialLowercase))
746018f6
C
115
116 return themes[theme]
117 }
118}