-
Notifications
You must be signed in to change notification settings - Fork 4
/
themecatalog.go
106 lines (99 loc) · 3.54 KB
/
themecatalog.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Package clib - Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"fmt"
"net/http"
"strings"
"github.com/muesli/gamut"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/lib/theme"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/views"
"projectforge.dev/projectforge/views/vtheme"
)
func ThemeColor(w http.ResponseWriter, r *http.Request) {
controller.Act("theme.color", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
col, err := cutil.PathString(r, "color", false)
if err != nil {
return "", err
}
col = strings.ToLower(col)
if !strings.HasPrefix(col, "#") {
col = "#" + col
}
th := theme.ColorTheme(col, gamut.Hex(col))
ps.SetTitleAndData(fmt.Sprintf("[%s] Theme", col), th)
ps.DefaultNavIcon = themeIcon
return controller.Render(r, as, &vtheme.Edit{Theme: th, Icon: "app"}, ps, "Themes||/theme", col)
})
}
func ThemeColorEdit(w http.ResponseWriter, r *http.Request) {
controller.Act("theme.color.edit", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
color := r.URL.Query().Get("color")
if color == "" {
return "", errors.New("must provide color in query string")
}
if !strings.HasPrefix(color, "#") {
return "", errors.New("provided color must be a hex string")
}
t := theme.ColorTheme(strings.TrimPrefix(color, "#"), gamut.Hex(color))
ps.SetTitleAndData("Edit theme colors ["+t.Key+"]", t)
ps.DefaultNavIcon = themeIcon
page := &vtheme.Edit{Theme: t, Icon: "app", Exists: as.Themes.FileExists(t.Key)}
return controller.Render(r, as, page, ps, "Themes||/theme", t.Key)
})
}
func ThemePalette(w http.ResponseWriter, r *http.Request) {
controller.Act("theme.palette", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
pal, err := cutil.PathString(r, "palette", false)
if err != nil {
return "", err
}
_, span, _ := telemetry.StartSpan(ps.Context, "theme:load", ps.Logger)
thms, err := theme.PaletteThemes(pal)
span.Complete()
if err != nil {
return "", err
}
ps.SetTitleAndData(fmt.Sprintf("[%s] Themes", pal), thms)
if r.URL.Query().Get("t") == "go" {
ps.Data = strings.Join(lo.Map(thms, func(t *theme.Theme, _ int) string {
return t.ToGo()
}), util.StringDefaultLinebreak)
return controller.Render(r, as, &views.Debug{}, ps, "Themes")
}
ps.DefaultNavIcon = themeIcon
return controller.Render(r, as, &vtheme.Add{Palette: pal, Themes: thms}, ps, "Themes||/theme", "Palette")
})
}
func ThemePaletteEdit(w http.ResponseWriter, r *http.Request) {
controller.Act("theme.palette.edit", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
palette, err := cutil.PathString(r, "palette", false)
if err != nil {
return "", err
}
key, err := cutil.PathString(r, "theme", false)
if err != nil {
return "", err
}
if key == theme.Default.Key {
return controller.FlashAndRedir(false, "Unable to edit default theme", "/theme", ps)
}
themes, err := theme.PaletteThemes(palette)
if err != nil {
return "", err
}
t := themes.Get(key)
if t == nil {
return "", errors.Errorf("invalid theme [%s] for palette [%s]", key, palette)
}
ps.SetTitleAndData("Edit theme palette ["+t.Key+"]", t)
ps.DefaultNavIcon = themeIcon
return controller.Render(r, as, &vtheme.Edit{Theme: t, Icon: "app"}, ps, "Themes||/theme", t.Key)
})
}