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