]>
Commit | Line | Data |
---|---|---|
1 | import { first } from 'rxjs/operators' | |
2 | import { Injectable } from '@angular/core' | |
3 | import { UserLocalStorageKeys } from '@root-helpers/users' | |
4 | import { HTMLServerConfig, ServerConfigTheme } from '@shared/models' | |
5 | import { environment } from '../../../environments/environment' | |
6 | import { AuthService } from '../auth' | |
7 | import { PluginService } from '../plugins/plugin.service' | |
8 | import { ServerService } from '../server' | |
9 | import { UserService } from '../users/user.service' | |
10 | import { LocalStorageService } from '../wrappers/storage.service' | |
11 | ||
12 | @Injectable() | |
13 | export class ThemeService { | |
14 | ||
15 | private oldThemeName: string | |
16 | private themes: ServerConfigTheme[] = [] | |
17 | ||
18 | private themeFromLocalStorage: ServerConfigTheme | |
19 | private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = [] | |
20 | ||
21 | private serverConfig: HTMLServerConfig | |
22 | ||
23 | constructor ( | |
24 | private auth: AuthService, | |
25 | private userService: UserService, | |
26 | private pluginService: PluginService, | |
27 | private server: ServerService, | |
28 | private localStorageService: LocalStorageService | |
29 | ) {} | |
30 | ||
31 | initialize () { | |
32 | // Try to load from local storage first, so we don't have to wait network requests | |
33 | this.loadAndSetFromLocalStorage() | |
34 | ||
35 | this.serverConfig = this.server.getHTMLConfig() | |
36 | const themes = this.serverConfig.theme.registered | |
37 | ||
38 | this.removeThemeFromLocalStorageIfNeeded(themes) | |
39 | this.injectThemes(themes) | |
40 | ||
41 | this.listenUserTheme() | |
42 | } | |
43 | ||
44 | private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) { | |
45 | this.themes = themes | |
46 | ||
47 | console.log('Injecting %d themes.', this.themes.length) | |
48 | ||
49 | const head = this.getHeadElement() | |
50 | ||
51 | for (const theme of this.themes) { | |
52 | // Already added this theme? | |
53 | if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue | |
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 | ||
65 | if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link) | |
66 | ||
67 | head.appendChild(link) | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | private getCurrentTheme () { | |
73 | if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name | |
74 | ||
75 | const theme = this.auth.isLoggedIn() | |
76 | ? this.auth.getUser().theme | |
77 | : this.userService.getAnonymousUser().theme | |
78 | ||
79 | if (theme !== 'instance-default') return theme | |
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 | |
93 | } | |
94 | ||
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 () { | |
106 | if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName) | |
107 | ||
108 | const currentTheme = this.getCurrentTheme() | |
109 | ||
110 | console.log('Enabling %s theme.', currentTheme) | |
111 | ||
112 | this.loadTheme(currentTheme) | |
113 | ||
114 | const theme = this.getTheme(currentTheme) | |
115 | if (theme) { | |
116 | console.log('Adding scripts of theme %s.', currentTheme) | |
117 | this.pluginService.addPlugin(theme, true) | |
118 | ||
119 | this.pluginService.reloadLoadedScopes() | |
120 | ||
121 | this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false) | |
122 | } else { | |
123 | this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false) | |
124 | } | |
125 | ||
126 | this.oldThemeName = currentTheme | |
127 | } | |
128 | ||
129 | private listenUserTheme () { | |
130 | // We don't need them anymore | |
131 | this.themeFromLocalStorage = undefined | |
132 | this.themeDOMLinksFromLocalStorage = [] | |
133 | ||
134 | if (!this.auth.isLoggedIn()) { | |
135 | this.updateCurrentTheme() | |
136 | ||
137 | this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe( | |
138 | () => this.updateCurrentTheme() | |
139 | ) | |
140 | } | |
141 | ||
142 | this.auth.userInformationLoaded | |
143 | .pipe(first()) | |
144 | .subscribe(() => this.updateCurrentTheme()) | |
145 | } | |
146 | ||
147 | private loadAndSetFromLocalStorage () { | |
148 | const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME) | |
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 | ||
194 | private getTheme (name: string) { | |
195 | return this.themes.find(t => t.name === name) | |
196 | } | |
197 | } |