1 import { first } from 'rxjs/operators'
2 import { Injectable } from '@angular/core'
3 import { UserLocalStorageKeys } from '@root-helpers/users'
4 import { ServerConfig, 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'
13 export class ThemeService {
15 private oldThemeName: string
16 private themes: ServerConfigTheme[] = []
18 private themeFromLocalStorage: ServerConfigTheme
19 private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = []
21 private serverConfig: ServerConfig
24 private auth: AuthService,
25 private userService: UserService,
26 private pluginService: PluginService,
27 private server: ServerService,
28 private localStorageService: LocalStorageService
32 // Try to load from local storage first, so we don't have to wait network requests
33 this.loadAndSetFromLocalStorage()
35 this.serverConfig = this.server.getTmpConfig()
36 this.server.getConfig()
37 .subscribe(config => {
38 this.serverConfig = config
40 const themes = this.serverConfig.theme.registered
42 this.removeThemeFromLocalStorageIfNeeded(themes)
43 this.injectThemes(themes)
45 this.listenUserTheme()
49 private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
52 console.log('Injecting %d themes.', this.themes.length)
54 const head = this.getHeadElement()
56 for (const theme of this.themes) {
57 // Already added this theme?
58 if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue
60 for (const css of theme.css) {
61 const link = document.createElement('link')
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', '')
70 if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link)
72 head.appendChild(link)
77 private getCurrentTheme () {
78 if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
80 const theme = this.auth.isLoggedIn()
81 ? this.auth.getUser().theme
82 : this.userService.getAnonymousUser().theme
84 if (theme !== 'instance-default') return theme
85 return this.serverConfig.theme.default
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
98 private updateCurrentTheme () {
99 if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName)
101 const currentTheme = this.getCurrentTheme()
103 console.log('Enabling %s theme.', currentTheme)
105 this.loadTheme(currentTheme)
107 const theme = this.getTheme(currentTheme)
109 console.log('Adding scripts of theme %s.', currentTheme)
110 this.pluginService.addPlugin(theme, true)
112 this.pluginService.reloadLoadedScopes()
114 this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false)
116 this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false)
119 this.oldThemeName = currentTheme
122 private listenUserTheme () {
123 // We don't need them anymore
124 this.themeFromLocalStorage = undefined
125 this.themeDOMLinksFromLocalStorage = []
127 if (!this.auth.isLoggedIn()) {
128 this.updateCurrentTheme()
130 this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe(
131 () => this.updateCurrentTheme()
135 this.auth.userInformationLoaded
137 .subscribe(() => this.updateCurrentTheme())
140 private loadAndSetFromLocalStorage () {
141 const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME)
142 if (!lastActiveThemeString) return
145 const lastActiveTheme = JSON.parse(lastActiveThemeString)
146 this.themeFromLocalStorage = lastActiveTheme
148 this.injectThemes([ lastActiveTheme ], true)
149 this.updateCurrentTheme()
151 console.error('Cannot parse last active theme.', err)
156 private removeThemePlugins (themeName: string) {
157 const oldTheme = this.getTheme(themeName)
159 console.log('Removing scripts of old theme %s.', themeName)
160 this.pluginService.removePlugin(oldTheme)
164 private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
165 if (!this.themeFromLocalStorage) return
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
173 const head = this.getHeadElement()
174 for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
175 head.removeChild(htmlLinkElement)
178 this.themeFromLocalStorage = undefined
179 this.themeDOMLinksFromLocalStorage = []
183 private getHeadElement () {
184 return document.getElementsByTagName('head')[0]
187 private getTheme (name: string) {
188 return this.themes.find(t => t.name === name)