]>
Commit | Line | Data |
---|---|---|
5b0ec7cd | 1 | import { Component, Input, OnChanges } from '@angular/core' |
746018f6 C |
2 | import { VideoChannel } from '../shared-main' |
3 | import { Account } from '../shared-main/account/account.model' | |
4 | ||
5 | type ActorInput = { | |
6 | name: string | |
d0800f76 | 7 | avatars: { width: number, url?: string, path: string }[] |
746018f6 C |
8 | url: string |
9 | } | |
10 | ||
d0800f76 | 11 | export 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 | 18 | export class ActorAvatarComponent implements OnChanges { |
9df52d66 C |
19 | private _title: string |
20 | ||
87fdea2f C |
21 | @Input() actor: ActorInput |
22 | @Input() actorType: 'channel' | 'account' | 'unlogged' | |
746018f6 | 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 | |
87fdea2f C |
39 | if (this.isAccount()) return $localize`${this.actor.name} (account page)` |
40 | if (this.isChannel()) return $localize`${this.actor.name} (channel page)` | |
746018f6 C |
41 | |
42 | return '' | |
43 | } | |
44 | ||
5b0ec7cd C |
45 | classes: string[] = [] |
46 | ||
746018f6 | 47 | get alt () { |
87fdea2f C |
48 | if (this.isAccount()) return $localize`Account avatar` |
49 | if (this.isChannel()) return $localize`Channel avatar` | |
746018f6 C |
50 | |
51 | return '' | |
52 | } | |
53 | ||
746018f6 | 54 | get defaultAvatarUrl () { |
87fdea2f | 55 | if (this.isChannel()) return VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber()) |
b713976a | 56 | |
87fdea2f | 57 | // account or unlogged |
b713976a | 58 | return Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber()) |
746018f6 C |
59 | } |
60 | ||
61 | get avatarUrl () { | |
87fdea2f | 62 | if (!this.actor) return '' |
746018f6 | 63 | |
87fdea2f C |
64 | if (this.isAccount()) return Account.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber()) |
65 | if (this.isChannel()) return VideoChannel.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber()) | |
746018f6 | 66 | |
87fdea2f | 67 | return '' |
746018f6 C |
68 | } |
69 | ||
5b0ec7cd C |
70 | ngOnChanges () { |
71 | this.classes = [ 'avatar' ] | |
9df52d66 | 72 | |
87fdea2f C |
73 | if (this.size) { |
74 | this.classes.push(`avatar-${this.size}`) | |
75 | } | |
9df52d66 | 76 | |
87fdea2f C |
77 | if (this.isChannel()) { |
78 | this.classes.push('channel') | |
79 | } else { | |
80 | this.classes.push('account') | |
81 | } | |
9df52d66 | 82 | |
87fdea2f C |
83 | // No avatar, use actor name initial |
84 | if (this.displayActorInitial()) { | |
5b0ec7cd C |
85 | this.classes.push('initial') |
86 | this.classes.push(this.getColorTheme()) | |
9df52d66 | 87 | } |
9df52d66 C |
88 | } |
89 | ||
87fdea2f C |
90 | displayImage () { |
91 | if (this.actorType === 'unlogged') return true | |
92 | ||
93 | return !!(this.actor && this.avatarUrl) | |
94 | } | |
95 | ||
96 | displayActorInitial () { | |
97 | return this.actor && !this.avatarUrl | |
98 | } | |
99 | ||
100 | displayPlaceholder () { | |
101 | return this.actorType !== 'unlogged' && !this.actor | |
102 | } | |
103 | ||
104 | getActorInitial () { | |
105 | const name = this.actor?.name | |
106 | if (!name) return '' | |
107 | ||
108 | return name.slice(0, 1) | |
109 | } | |
110 | ||
111 | private isAccount () { | |
112 | return this.actorType === 'account' | |
113 | } | |
114 | ||
115 | private isChannel () { | |
116 | return this.actorType === 'channel' | |
746018f6 C |
117 | } |
118 | ||
4428ad54 C |
119 | private getSizeNumber () { |
120 | if (this.size) return +this.size | |
121 | ||
122 | return undefined | |
123 | } | |
124 | ||
746018f6 | 125 | private getColorTheme () { |
87fdea2f | 126 | const initialLowercase = this.getActorInitial().toLowerCase() |
f41efa52 | 127 | |
746018f6 C |
128 | // Keep consistency with CSS |
129 | const themes = { | |
f41efa52 | 130 | '0123456789abc': 'blue', |
746018f6 C |
131 | def: 'green', |
132 | ghi: 'purple', | |
133 | jkl: 'gray', | |
134 | mno: 'yellow', | |
135 | pqr: 'orange', | |
1fd61899 | 136 | stvu: 'red', |
746018f6 C |
137 | wxyz: 'dark-blue' |
138 | } | |
139 | ||
140 | const theme = Object.keys(themes) | |
f41efa52 | 141 | .find(chars => chars.includes(initialLowercase)) |
746018f6 | 142 | |
dbf49527 | 143 | return themes[theme] || 'blue' |
746018f6 C |
144 | } |
145 | } |