Core

/app/lib/theme/service.go (1.3 KB)

 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
package theme

import (
"github.com/pkg/errors"

"{{{ .Package }}}/app/util"
)

const KeyNew = "new"

type Service struct {
root string
cache Themes
}

func NewService() *Service {
return &Service{root: "themes"}
}

func (s *Service) All(logger util.Logger) Themes {
s.loadIfNeeded(logger)
return s.cache
}

func (s *Service) Clear() {
s.cache = nil
}

func (s *Service) Get(theme string, logger util.Logger) *Theme {
for _, t := range s.All(logger) {
if t.Key == theme {
return t
}
}
return Default
}

func (s *Service) Save(t *Theme, originalKey string, logger util.Logger) error {
if t.Key == Default.Key {
return errors.New("can't overwrite default theme")
}
if t.Key == "" || t.Key == KeyNew {
t.Key = util.RandomString(12)
}
if originalKey != t.Key {
err := s.Remove(originalKey, logger)
if err != nil {
return err
}
}
s.cache = s.cache.Replace(t)
return nil
}

func (s *Service) Remove(key string, logger util.Logger) error {
s.cache = s.cache.Remove(key)
return nil
}

func (s *Service) FileExists(key string) bool {
return false
}

func (s *Service) loadIfNeeded(logger util.Logger) {
if s.cache == nil {
s.cache = Themes{Default}{{{ if .HasModule "themecatalog" }}}
s.cache = append(s.cache, CatalogThemes...){{{ end }}}
}
}