aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/theme/theme.service.ts
blob: 0124880751ec041769dce87fda37eafff1fcf7b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Injectable } from '@angular/core'
import { AuthService } from '@app/core/auth'
import { ServerService } from '@app/core/server'
import { environment } from '../../../environments/environment'
import { PluginService } from '@app/core/plugins/plugin.service'
import { ServerConfigTheme } from '@shared/models'

@Injectable()
export class ThemeService {

  private oldThemeName: string
  private themes: ServerConfigTheme[] = []

  constructor (
    private auth: AuthService,
    private pluginService: PluginService,
    private server: ServerService
  ) {}

  initialize () {
    this.server.configLoaded
        .subscribe(() => {
          this.injectThemes()

          this.listenUserTheme()
        })
  }

  private injectThemes () {
    this.themes = this.server.getConfig().theme.registered

    console.log('Injecting %d themes.', this.themes.length)

    const head = document.getElementsByTagName('head')[0]

    for (const theme of this.themes) {

      for (const css of theme.css) {
        const link = document.createElement('link')

        const href = environment.apiUrl + `/themes/${theme.name}/${theme.version}/css/${css}`
        link.setAttribute('href', href)
        link.setAttribute('rel', 'alternate stylesheet')
        link.setAttribute('type', 'text/css')
        link.setAttribute('title', theme.name)
        link.setAttribute('disabled', '')

        head.appendChild(link)
      }
    }
  }

  private getCurrentTheme () {
    if (this.auth.isLoggedIn()) {
      const theme = this.auth.getUser().theme
      if (theme !== 'instance-default') return theme
    }

    return this.server.getConfig().theme.default
  }

  private loadTheme (name: string) {
    const links = document.getElementsByTagName('link')
    for (let i = 0; i < links.length; i++) {
      const link = links[ i ]
      if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) {
        link.disabled = link.getAttribute('title') !== name
      }
    }
  }

  private updateCurrentTheme () {
    if (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()

    console.log('Enabling %s theme.', currentTheme)

    this.loadTheme(currentTheme)

    const theme = this.getTheme(currentTheme)
    if (theme) {
      console.log('Adding scripts of theme %s.', currentTheme)
      this.pluginService.addPlugin(theme, true)

      this.pluginService.reloadLoadedScopes()
    }

    this.oldThemeName = currentTheme
  }

  private listenUserTheme () {
    if (!this.auth.isLoggedIn()) {
      this.updateCurrentTheme()
    }

    this.auth.userInformationLoaded
      .subscribe(() => this.updateCurrentTheme())
  }

  private getTheme (name: string) {
    return this.themes.find(t => t.name === name)
  }
}