Speedup theme injection
This commit is contained in:
parent
ce78f1f6f0
commit
a2ffd046d6
|
@ -71,6 +71,9 @@ export class AppComponent implements OnInit {
|
||||||
ngOnInit () {
|
ngOnInit () {
|
||||||
document.getElementById('incompatible-browser').className += ' browser-ok'
|
document.getElementById('incompatible-browser').className += ' browser-ok'
|
||||||
|
|
||||||
|
this.loadPlugins()
|
||||||
|
this.themeService.initialize()
|
||||||
|
|
||||||
this.authService.loadClientCredentials()
|
this.authService.loadClientCredentials()
|
||||||
|
|
||||||
if (this.isUserLoggedIn()) {
|
if (this.isUserLoggedIn()) {
|
||||||
|
@ -86,9 +89,6 @@ export class AppComponent implements OnInit {
|
||||||
this.serverService.loadVideoPrivacies()
|
this.serverService.loadVideoPrivacies()
|
||||||
this.serverService.loadVideoPlaylistPrivacies()
|
this.serverService.loadVideoPlaylistPrivacies()
|
||||||
|
|
||||||
this.loadPlugins()
|
|
||||||
this.themeService.initialize()
|
|
||||||
|
|
||||||
// Do not display menu on small screens
|
// Do not display menu on small screens
|
||||||
if (this.screenService.isInSmallView()) {
|
if (this.screenService.isInSmallView()) {
|
||||||
this.isMenuDisplayed = false
|
this.isMenuDisplayed = false
|
||||||
|
|
|
@ -4,13 +4,21 @@ import { ServerService } from '@app/core/server'
|
||||||
import { environment } from '../../../environments/environment'
|
import { environment } from '../../../environments/environment'
|
||||||
import { PluginService } from '@app/core/plugins/plugin.service'
|
import { PluginService } from '@app/core/plugins/plugin.service'
|
||||||
import { ServerConfigTheme } from '@shared/models'
|
import { ServerConfigTheme } from '@shared/models'
|
||||||
|
import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ThemeService {
|
export class ThemeService {
|
||||||
|
|
||||||
|
private static KEYS = {
|
||||||
|
LAST_ACTIVE_THEME: 'last_active_theme'
|
||||||
|
}
|
||||||
|
|
||||||
private oldThemeName: string
|
private oldThemeName: string
|
||||||
private themes: ServerConfigTheme[] = []
|
private themes: ServerConfigTheme[] = []
|
||||||
|
|
||||||
|
private themeFromLocalStorage: ServerConfigTheme
|
||||||
|
private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = []
|
||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
private auth: AuthService,
|
private auth: AuthService,
|
||||||
private pluginService: PluginService,
|
private pluginService: PluginService,
|
||||||
|
@ -18,22 +26,30 @@ export class ThemeService {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
initialize () {
|
initialize () {
|
||||||
|
// Try to load from local storage first, so we don't have to wait network requests
|
||||||
|
this.loadAndSetFromLocalStorage()
|
||||||
|
|
||||||
this.server.configLoaded
|
this.server.configLoaded
|
||||||
.subscribe(() => {
|
.subscribe(() => {
|
||||||
this.injectThemes()
|
const themes = this.server.getConfig().theme.registered
|
||||||
|
|
||||||
|
this.removeThemeFromLocalStorageIfNeeded(themes)
|
||||||
|
this.injectThemes(themes)
|
||||||
|
|
||||||
this.listenUserTheme()
|
this.listenUserTheme()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private injectThemes () {
|
private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
|
||||||
this.themes = this.server.getConfig().theme.registered
|
this.themes = themes
|
||||||
|
|
||||||
console.log('Injecting %d themes.', this.themes.length)
|
console.log('Injecting %d themes.', this.themes.length)
|
||||||
|
|
||||||
const head = document.getElementsByTagName('head')[0]
|
const head = this.getHeadElement()
|
||||||
|
|
||||||
for (const theme of this.themes) {
|
for (const theme of this.themes) {
|
||||||
|
// Already added this theme?
|
||||||
|
if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue
|
||||||
|
|
||||||
for (const css of theme.css) {
|
for (const css of theme.css) {
|
||||||
const link = document.createElement('link')
|
const link = document.createElement('link')
|
||||||
|
@ -45,12 +61,16 @@ export class ThemeService {
|
||||||
link.setAttribute('title', theme.name)
|
link.setAttribute('title', theme.name)
|
||||||
link.setAttribute('disabled', '')
|
link.setAttribute('disabled', '')
|
||||||
|
|
||||||
|
if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link)
|
||||||
|
|
||||||
head.appendChild(link)
|
head.appendChild(link)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getCurrentTheme () {
|
private getCurrentTheme () {
|
||||||
|
if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
|
||||||
|
|
||||||
if (this.auth.isLoggedIn()) {
|
if (this.auth.isLoggedIn()) {
|
||||||
const theme = this.auth.getUser().theme
|
const theme = this.auth.getUser().theme
|
||||||
if (theme !== 'instance-default') return theme
|
if (theme !== 'instance-default') return theme
|
||||||
|
@ -70,13 +90,7 @@ export class ThemeService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateCurrentTheme () {
|
private updateCurrentTheme () {
|
||||||
if (this.oldThemeName) {
|
if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName)
|
||||||
const oldTheme = this.getTheme(this.oldThemeName)
|
|
||||||
if (oldTheme) {
|
|
||||||
console.log('Removing scripts of old theme %s.', this.oldThemeName)
|
|
||||||
this.pluginService.removePlugin(oldTheme)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentTheme = this.getCurrentTheme()
|
const currentTheme = this.getCurrentTheme()
|
||||||
|
|
||||||
|
@ -90,12 +104,20 @@ export class ThemeService {
|
||||||
this.pluginService.addPlugin(theme, true)
|
this.pluginService.addPlugin(theme, true)
|
||||||
|
|
||||||
this.pluginService.reloadLoadedScopes()
|
this.pluginService.reloadLoadedScopes()
|
||||||
|
|
||||||
|
peertubeLocalStorage.setItem(ThemeService.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme))
|
||||||
|
} else {
|
||||||
|
peertubeLocalStorage.removeItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.oldThemeName = currentTheme
|
this.oldThemeName = currentTheme
|
||||||
}
|
}
|
||||||
|
|
||||||
private listenUserTheme () {
|
private listenUserTheme () {
|
||||||
|
// We don't need them anymore
|
||||||
|
this.themeFromLocalStorage = undefined
|
||||||
|
this.themeDOMLinksFromLocalStorage = []
|
||||||
|
|
||||||
if (!this.auth.isLoggedIn()) {
|
if (!this.auth.isLoggedIn()) {
|
||||||
this.updateCurrentTheme()
|
this.updateCurrentTheme()
|
||||||
}
|
}
|
||||||
|
@ -104,6 +126,53 @@ export class ThemeService {
|
||||||
.subscribe(() => this.updateCurrentTheme())
|
.subscribe(() => this.updateCurrentTheme())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private loadAndSetFromLocalStorage () {
|
||||||
|
const lastActiveThemeString = peertubeLocalStorage.getItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
|
||||||
|
if (!lastActiveThemeString) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
const lastActiveTheme = JSON.parse(lastActiveThemeString)
|
||||||
|
this.themeFromLocalStorage = lastActiveTheme
|
||||||
|
|
||||||
|
this.injectThemes([ lastActiveTheme ], true)
|
||||||
|
this.updateCurrentTheme()
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Cannot parse last active theme.', err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private removeThemePlugins (themeName: string) {
|
||||||
|
const oldTheme = this.getTheme(themeName)
|
||||||
|
if (oldTheme) {
|
||||||
|
console.log('Removing scripts of old theme %s.', themeName)
|
||||||
|
this.pluginService.removePlugin(oldTheme)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) {
|
||||||
|
if (!this.themeFromLocalStorage) return
|
||||||
|
|
||||||
|
const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name)
|
||||||
|
if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) {
|
||||||
|
// Need to remove this theme: we loaded an old version or a theme that does not exist anymore
|
||||||
|
this.removeThemePlugins(this.themeFromLocalStorage.name)
|
||||||
|
this.oldThemeName = undefined
|
||||||
|
|
||||||
|
const head = this.getHeadElement()
|
||||||
|
for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) {
|
||||||
|
head.removeChild(htmlLinkElement)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.themeFromLocalStorage = undefined
|
||||||
|
this.themeDOMLinksFromLocalStorage = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getHeadElement () {
|
||||||
|
return document.getElementsByTagName('head')[0]
|
||||||
|
}
|
||||||
|
|
||||||
private getTheme (name: string) {
|
private getTheme (name: string) {
|
||||||
return this.themes.find(t => t.name === name)
|
return this.themes.find(t => t.name === name)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue