1 import { Injectable } from '@angular/core'
2 import { AuthService } from '@app/core/auth'
3 import { ServerService } from '@app/core/server'
4 import { environment } from '../../../environments/environment'
5 import { PluginService } from '@app/core/plugins/plugin.service'
6 import { ServerConfig, ServerConfigTheme } from '@shared/models'
7 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
8 import { first } from 'rxjs/operators'
11 export class ThemeService {
13 private static KEYS = {
14 LAST_ACTIVE_THEME: 'last_active_theme'
17 private oldThemeName: string
18 private themes: ServerConfigTheme[] = []
20 private themeFromLocalStorage: ServerConfigTheme
21 private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = []
23 private serverConfig: ServerConfig
26 private auth: AuthService,
27 private pluginService: PluginService,
28 private server: ServerService
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 if (this.auth.isLoggedIn()) {
81 const theme = this.auth.getUser().theme
82 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 peertubeLocalStorage.setItem(ThemeService.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme))
116 peertubeLocalStorage.removeItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
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()
131 this.auth.userInformationLoaded
133 .subscribe(() => this.updateCurrentTheme())
136 private loadAndSetFromLocalStorage () {
137 const lastActiveThemeString = peertubeLocalStorage.getItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
138 if (!lastActiveThemeString) return
141 const lastActiveTheme = JSON.parse(lastActiveThemeString)
142 this.themeFromLocalStorage = lastActiveTheme
144 this.injectThemes([ lastActiveTheme ], true)
145 this.updateCurrentTheme()
147 console.error('Cannot parse last active theme.', err)
152 private removeThemePlugins (themeName: string) {
153 const oldTheme = this.getTheme(themeName)
155 console.log('Removing scripts of old theme %s.', themeName)
156 this.pluginService.removePlugin(oldTheme)
160 private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
161 if (!this.themeFromLocalStorage) return
163 const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name)
164 if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) {
165 // Need to remove this theme: we loaded an old version or a theme that does not exist anymore
166 this.removeThemePlugins(this.themeFromLocalStorage.name)
167 this.oldThemeName = undefined
169 const head = this.getHeadElement()
170 for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
171 head.removeChild(htmlLinkElement)
174 this.themeFromLocalStorage = undefined
175 this.themeDOMLinksFromLocalStorage = []
179 private getHeadElement () {
180 return document.getElementsByTagName('head')[0]
183 private getTheme (name: string) {
184 return this.themes.find(t => t.name === name)