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