]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/theme/theme.service.ts
Use HTML config when possible
[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 3import { UserLocalStorageKeys } from '@root-helpers/users'
2989628b 4import { HTMLServerConfig, ServerConfigTheme } from '@shared/models'
a4ff3100 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
2989628b 21 private serverConfig: HTMLServerConfig
ba430d75 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
2989628b
C
35 this.serverConfig = this.server.getHTMLConfig()
36 const themes = this.serverConfig.theme.registered
ba430d75 37
2989628b
C
38 this.removeThemeFromLocalStorageIfNeeded(themes)
39 this.injectThemes(themes)
a2ffd046 40
2989628b 41 this.listenUserTheme()
ffb321be
C
42 }
43
a2ffd046
C
44 private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
45 this.themes = themes
ffb321be
C
46
47 console.log('Injecting %d themes.', this.themes.length)
48
a2ffd046 49 const head = this.getHeadElement()
ffb321be
C
50
51 for (const theme of this.themes) {
a2ffd046
C
52 // Already added this theme?
53 if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue
ffb321be
C
54
55 for (const css of theme.css) {
56 const link = document.createElement('link')
57
58 const href = environment.apiUrl + `/themes/${theme.name}/${theme.version}/css/${css}`
59 link.setAttribute('href', href)
60 link.setAttribute('rel', 'alternate stylesheet')
61 link.setAttribute('type', 'text/css')
62 link.setAttribute('title', theme.name)
63 link.setAttribute('disabled', '')
64
a2ffd046
C
65 if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link)
66
ffb321be
C
67 head.appendChild(link)
68 }
69 }
70 }
71
72 private getCurrentTheme () {
a2ffd046
C
73 if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
74
d3217560
RK
75 const theme = this.auth.isLoggedIn()
76 ? this.auth.getUser().theme
77 : this.userService.getAnonymousUser().theme
ffb321be 78
d3217560 79 if (theme !== 'instance-default') return theme
5c48aa8c
C
80
81 const instanceTheme = this.serverConfig.theme.default
82 if (instanceTheme !== 'default') return instanceTheme
83
84 // Default to dark theme if available and wanted by the user
85 if (
86 this.themes.find(t => t.name === 'dark') &&
87 window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
88 ) {
89 return 'dark'
90 }
91
92 return instanceTheme
1a00c561
RK
93 }
94
ffb321be
C
95 private loadTheme (name: string) {
96 const links = document.getElementsByTagName('link')
97 for (let i = 0; i < links.length; i++) {
98 const link = links[ i ]
99 if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) {
100 link.disabled = link.getAttribute('title') !== name
101 }
102 }
103 }
104
105 private updateCurrentTheme () {
a2ffd046 106 if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName)
ffb321be
C
107
108 const currentTheme = this.getCurrentTheme()
109
110 console.log('Enabling %s theme.', currentTheme)
111
112 this.loadTheme(currentTheme)
d00dc28d 113
ffb321be
C
114 const theme = this.getTheme(currentTheme)
115 if (theme) {
116 console.log('Adding scripts of theme %s.', currentTheme)
f0c5e8b6 117 this.pluginService.addPlugin(theme, true)
ffb321be
C
118
119 this.pluginService.reloadLoadedScopes()
a2ffd046 120
a4ff3100 121 this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false)
a2ffd046 122 } else {
a4ff3100 123 this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false)
e3f7f600 124 }
ffb321be
C
125
126 this.oldThemeName = currentTheme
127 }
128
129 private listenUserTheme () {
a2ffd046
C
130 // We don't need them anymore
131 this.themeFromLocalStorage = undefined
132 this.themeDOMLinksFromLocalStorage = []
133
d00dc28d
C
134 if (!this.auth.isLoggedIn()) {
135 this.updateCurrentTheme()
d3217560 136
a4ff3100 137 this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe(
d3217560
RK
138 () => this.updateCurrentTheme()
139 )
d00dc28d
C
140 }
141
ffb321be 142 this.auth.userInformationLoaded
7c93905d 143 .pipe(first())
ffb321be 144 .subscribe(() => this.updateCurrentTheme())
1a00c561
RK
145 }
146
a2ffd046 147 private loadAndSetFromLocalStorage () {
a4ff3100 148 const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME)
a2ffd046
C
149 if (!lastActiveThemeString) return
150
151 try {
152 const lastActiveTheme = JSON.parse(lastActiveThemeString)
153 this.themeFromLocalStorage = lastActiveTheme
154
155 this.injectThemes([ lastActiveTheme ], true)
156 this.updateCurrentTheme()
157 } catch (err) {
158 console.error('Cannot parse last active theme.', err)
159 return
160 }
161 }
162
163 private removeThemePlugins (themeName: string) {
164 const oldTheme = this.getTheme(themeName)
165 if (oldTheme) {
166 console.log('Removing scripts of old theme %s.', themeName)
167 this.pluginService.removePlugin(oldTheme)
168 }
169 }
170
171 private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
172 if (!this.themeFromLocalStorage) return
173
174 const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name)
175 if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) {
176 // Need to remove this theme: we loaded an old version or a theme that does not exist anymore
177 this.removeThemePlugins(this.themeFromLocalStorage.name)
178 this.oldThemeName = undefined
179
180 const head = this.getHeadElement()
181 for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
182 head.removeChild(htmlLinkElement)
183 }
184
185 this.themeFromLocalStorage = undefined
186 this.themeDOMLinksFromLocalStorage = []
187 }
188 }
189
190 private getHeadElement () {
191 return document.getElementsByTagName('head')[0]
192 }
193
ffb321be
C
194 private getTheme (name: string) {
195 return this.themes.find(t => t.name === name)
1a00c561
RK
196 }
197}