-
Notifications
You must be signed in to change notification settings - Fork 4
/
theme.go
87 lines (75 loc) · 2.36 KB
/
theme.go
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
// Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller"
"projectforge.dev/projectforge/app/controller/csession"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/lib/theme"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/views/vtheme"
)
func ThemeList(rc *fasthttp.RequestCtx) {
controller.Act("theme.list", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
ps.Title = "Themes"
x := as.Themes.All(ps.Logger)
ps.Data = x
return controller.Render(rc, as, &vtheme.List{Themes: x}, ps, "admin", "Themes")
})
}
func ThemeEdit(rc *fasthttp.RequestCtx) {
controller.Act("theme.edit", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
key, err := cutil.RCRequiredString(rc, "key", false)
if err != nil {
return "", err
}
var t *theme.Theme
if key == theme.KeyNew {
t = theme.ThemeDefault.Clone(key)
} else {
t = as.Themes.Get(key, ps.Logger)
}
if t == nil {
return "", errors.Wrapf(err, "no theme found with key [%s]", key)
}
ps.Data = t
ps.Title = "Edit theme [" + t.Key + "]"
page := &vtheme.Edit{Theme: t}
return controller.Render(rc, as, page, ps, "admin", "Themes||/admin/theme", t.Key)
})
}
func ThemeSave(rc *fasthttp.RequestCtx) {
controller.Act("theme.save", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
key, err := cutil.RCRequiredString(rc, "key", false)
if err != nil {
return "", err
}
frm, err := cutil.ParseForm(rc)
if err != nil {
return "", err
}
orig := as.Themes.Get(key, ps.Logger)
newKey, err := frm.GetString("key", false)
if err != nil {
return "", err
}
if newKey == theme.KeyNew {
newKey = util.RandomString(12)
}
l := orig.Light.Clone().ApplyMap(frm, "light-")
d := orig.Dark.Clone().ApplyMap(frm, "dark-")
t := &theme.Theme{Key: newKey, Light: l, Dark: d}
err = as.Themes.Save(t, ps.Logger)
if err != nil {
return "", errors.Wrap(err, "unable to save theme")
}
ps.Profile.Theme = newKey
err = csession.SaveProfile(ps.Profile, rc, ps.Session, ps.Logger)
if err != nil {
return "", err
}
return controller.ReturnToReferrer("saved changes to theme ["+newKey+"]", "/", rc, ps)
})
}