]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/theme/theme.service.ts
Redesign account's channels page
[github/Chocobozzz/PeerTube.git] / client / src / app / core / theme / theme.service.ts
CommitLineData
67ed6552 1import { first } from 'rxjs/operators'
1a00c561 2import { Injectable } from '@angular/core'
a4ff3100
C
3import { UserLocalStorageKeys } from '@root-helpers/users'
4import { ServerConfig, ServerConfigTheme } from '@shared/models'
5import { environment } from '../../../environments/environment'
67ed6552
C
6import { AuthService } from '../auth'
7import { PluginService } from '../plugins/plugin.service'
8import { ServerService } from '../server'
67ed6552 9import { UserService } from '../users/user.service'
a4ff3100 10import { LocalStorageService } from '../wrappers/storage.service'
1a00c561
RK
11
12@Injectable()
13export class ThemeService {
ffb321be
C
14
15 private oldThemeName: string
16 private themes: ServerConfigTheme[] = []
17
a2ffd046
C
18 private themeFromLocalStorage: ServerConfigTheme
19 private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = []
20
ba430d75
C
21 private serverConfig: ServerConfig
22
ffb321be
C
23 constructor (
24 private auth: AuthService,
d3217560 25 private userService: UserService,
ffb321be 26 private pluginService: PluginService,
d3217560
RK
27 private server: ServerService,
28 private localStorageService: LocalStorageService
ffb321be
C
29 ) {}
30
31 initialize () {
a2ffd046
C
32 // Try to load from local storage first, so we don't have to wait network requests
33 this.loadAndSetFromLocalStorage()
34
ba430d75
C
35 this.serverConfig = this.server.getTmpConfig()
36 this.server.getConfig()
37 .subscribe(config => {
38 this.serverConfig = config
39
40 const themes = this.serverConfig.theme.registered
a2ffd046
C
41
42 this.removeThemeFromLocalStorageIfNeeded(themes)
43 this.injectThemes(themes)
ffb321be
C
44
45 this.listenUserTheme()
46 })
47 }
48
a2ffd046
C
49 private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
50 this.themes = themes
ffb321be
C
51
52 console.log('Injecting %d themes.', this.themes.length)
53
a2ffd046 54 const head = this.getHeadElement()
ffb321be
C
55
56 for (const theme of this.themes) {
a2ffd046
C
57 // Already added this theme?
58 if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue
ffb321be
C
59
60 for (const css of theme.css) {
61 const link = document.createElement('link')
62
63 const href = environment.apiUrl + `/themes/${theme.name}/${theme.version}/css/${css}`
64 link.setAttribute('href', href)
65 link.setAttribute('rel', 'alternate stylesheet')
66 link.setAttribute('type', 'text/css')
67 link.setAttribute('title', theme.name)
68 link.setAttribute('disabled', '')
69
a2ffd046
C
70 if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link)
71
ffb321be
C
72 head.appendChild(link)
73 }
74 }
75 }
76
77 private getCurrentTheme () {
a2ffd046
C
78 if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
79
d3217560
RK
80 const theme = this.auth.isLoggedIn()
81 ? this.auth.getUser().theme
82 : this.userService.getAnonymousUser().theme
ffb321be 83
d3217560 84 if (theme !== 'instance-default') return theme
ba430d75 85 return this.serverConfig.theme.default
1a00c561
RK
86 }
87
ffb321be
C
88 private loadTheme (name: string) {
89 const links = document.getElementsByTagName('link')
90 for (let i = 0; i < links.length; i++) {
91 const link = links[ i ]
92 if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) {
93 link.disabled = link.getAttribute('title') !== name
94 }
95 }
96 }
97
98 private updateCurrentTheme () {
a2ffd046 99 if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName)
ffb321be
C
100
101 const currentTheme = this.getCurrentTheme()
102
103 console.log('Enabling %s theme.', currentTheme)
104
105 this.loadTheme(currentTheme)
d00dc28d 106
ffb321be
C
107 const theme = this.getTheme(currentTheme)
108 if (theme) {
109 console.log('Adding scripts of theme %s.', currentTheme)
f0c5e8b6 110 this.pluginService.addPlugin(theme, true)
ffb321be
C
111
112 this.pluginService.reloadLoadedScopes()
a2ffd046 113
a4ff3100 114 this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false)
a2ffd046 115 } else {
a4ff3100 116 this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false)
e3f7f600 117 }
ffb321be
C
118
119 this.oldThemeName = currentTheme
120 }
121
122 private listenUserTheme () {
a2ffd046
C
123 // We don't need them anymore
124 this.themeFromLocalStorage = undefined
125 this.themeDOMLinksFromLocalStorage = []
126
d00dc28d
C
127 if (!this.auth.isLoggedIn()) {
128 this.updateCurrentTheme()
d3217560 129
a4ff3100 130 this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe(
d3217560
RK
131 () => this.updateCurrentTheme()
132 )
d00dc28d
C
133 }
134
ffb321be 135 this.auth.userInformationLoaded
7c93905d 136 .pipe(first())
ffb321be 137 .subscribe(() => this.updateCurrentTheme())
1a00c561
RK
138 }
139
a2ffd046 140 private loadAndSetFromLocalStorage () {
a4ff3100 141 const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME)
a2ffd046
C
142 if (!lastActiveThemeString) return
143
144 try {
145 const lastActiveTheme = JSON.parse(lastActiveThemeString)
146 this.themeFromLocalStorage = lastActiveTheme
147
148 this.injectThemes([ lastActiveTheme ], true)
149 this.updateCurrentTheme()
150 } catch (err) {
151 console.error('Cannot parse last active theme.', err)
152 return
153 }
154 }
155
156 private removeThemePlugins (themeName: string) {
157 const oldTheme = this.getTheme(themeName)
158 if (oldTheme) {
159 console.log('Removing scripts of old theme %s.', themeName)
160 this.pluginService.removePlugin(oldTheme)
161 }
162 }
163
164 private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
165 if (!this.themeFromLocalStorage) return
166
167 const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name)
168 if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) {
169 // Need to remove this theme: we loaded an old version or a theme that does not exist anymore
170 this.removeThemePlugins(this.themeFromLocalStorage.name)
171 this.oldThemeName = undefined
172
173 const head = this.getHeadElement()
174 for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
175 head.removeChild(htmlLinkElement)
176 }
177
178 this.themeFromLocalStorage = undefined
179 this.themeDOMLinksFromLocalStorage = []
180 }
181 }
182
183 private getHeadElement () {
184 return document.getElementsByTagName('head')[0]
185 }
186
ffb321be
C
187 private getTheme (name: string) {
188 return this.themes.find(t => t.name === name)
1a00c561
RK
189 }
190}