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 { 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[] = []
24 private auth: AuthService,
25 private pluginService: PluginService,
26 private server: ServerService
30 // Try to load from local storage first, so we don't have to wait network requests
31 this.loadAndSetFromLocalStorage()
33 this.server.configLoaded
35 const themes = this.server.getConfig().theme.registered
37 this.removeThemeFromLocalStorageIfNeeded(themes)
38 this.injectThemes(themes)
40 this.listenUserTheme()
44 private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
47 console.log('Injecting %d themes.', this.themes.length)
49 const head = this.getHeadElement()
51 for (const theme of this.themes) {
52 // Already added this theme?
53 if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue
55 for (const css of theme.css) {
56 const link = document.createElement('link')
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', '')
65 if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link)
67 head.appendChild(link)
72 private getCurrentTheme () {
73 if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
75 if (this.auth.isLoggedIn()) {
76 const theme = this.auth.getUser().theme
77 if (theme !== 'instance-default') return theme
80 return this.server.getConfig().theme.default
83 private loadTheme (name: string) {
84 const links = document.getElementsByTagName('link')
85 for (let i = 0; i < links.length; i++) {
86 const link = links[ i ]
87 if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) {
88 link.disabled = link.getAttribute('title') !== name
93 private updateCurrentTheme () {
94 if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName)
96 const currentTheme = this.getCurrentTheme()
98 console.log('Enabling %s theme.', currentTheme)
100 this.loadTheme(currentTheme)
102 const theme = this.getTheme(currentTheme)
104 console.log('Adding scripts of theme %s.', currentTheme)
105 this.pluginService.addPlugin(theme, true)
107 this.pluginService.reloadLoadedScopes()
109 peertubeLocalStorage.setItem(ThemeService.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme))
111 peertubeLocalStorage.removeItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
114 this.oldThemeName = currentTheme
117 private listenUserTheme () {
118 // We don't need them anymore
119 this.themeFromLocalStorage = undefined
120 this.themeDOMLinksFromLocalStorage = []
122 if (!this.auth.isLoggedIn()) {
123 this.updateCurrentTheme()
126 this.auth.userInformationLoaded
128 .subscribe(() => this.updateCurrentTheme())
131 private loadAndSetFromLocalStorage () {
132 const lastActiveThemeString = peertubeLocalStorage.getItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
133 if (!lastActiveThemeString) return
136 const lastActiveTheme = JSON.parse(lastActiveThemeString)
137 this.themeFromLocalStorage = lastActiveTheme
139 this.injectThemes([ lastActiveTheme ], true)
140 this.updateCurrentTheme()
142 console.error('Cannot parse last active theme.', err)
147 private removeThemePlugins (themeName: string) {
148 const oldTheme = this.getTheme(themeName)
150 console.log('Removing scripts of old theme %s.', themeName)
151 this.pluginService.removePlugin(oldTheme)
155 private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
156 if (!this.themeFromLocalStorage) return
158 const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name)
159 if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) {
160 // Need to remove this theme: we loaded an old version or a theme that does not exist anymore
161 this.removeThemePlugins(this.themeFromLocalStorage.name)
162 this.oldThemeName = undefined
164 const head = this.getHeadElement()
165 for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
166 head.removeChild(htmlLinkElement)
169 this.themeFromLocalStorage = undefined
170 this.themeDOMLinksFromLocalStorage = []
174 private getHeadElement () {
175 return document.getElementsByTagName('head')[0]
178 private getTheme (name: string) {
179 return this.themes.find(t => t.name === name)