-
Notifications
You must be signed in to change notification settings - Fork 4
/
page.go
151 lines (139 loc) · 4.37 KB
/
page.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"context"
"fmt"
"strings"
"github.com/samber/lo"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller/cmenu"
"projectforge.dev/projectforge/app/lib/filter"
"projectforge.dev/projectforge/app/lib/menu"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/lib/theme"
"projectforge.dev/projectforge/app/lib/user"
"projectforge.dev/projectforge/app/util"
)
const (
DefaultSearchPath = "/search"
DefaultProfilePath = "/profile"
defaultIcon = "app"
)
var (
defaultRootTitleAppend = util.GetEnv("app_display_name_append")
defaultRootTitle = func() string {
if tmp := util.GetEnv("app_display_name"); tmp != "" {
return tmp
}
return util.AppName
}()
)
type PageState struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Method string `json:"method,omitempty"`
URI *fasthttp.URI `json:"-"`
Menu menu.Items `json:"menu,omitempty"`
Breadcrumbs cmenu.Breadcrumbs `json:"breadcrumbs,omitempty"`
Flashes []string `json:"flashes,omitempty"`
Session util.ValueMap `json:"-"`
Profile *user.Profile `json:"profile,omitempty"`
Authed bool `json:"authed,omitempty"`
Admin bool `json:"admin,omitempty"`
Params filter.ParamSet `json:"params,omitempty"`
Icons []string `json:"icons,omitempty"`
RootIcon string `json:"rootIcon,omitempty"`
RootPath string `json:"rootPath,omitempty"`
RootTitle string `json:"rootTitle,omitempty"`
SearchPath string `json:"searchPath,omitempty"`
ProfilePath string `json:"profilePath,omitempty"`
HideMenu bool `json:"hideMenu,omitempty"`
ForceRedirect string `json:"forceRedirect,omitempty"`
HeaderContent string `json:"headerContent,omitempty"`
Browser string `json:"browser,omitempty"`
BrowserVersion string `json:"browserVersion,omitempty"`
OS string `json:"os,omitempty"`
OSVersion string `json:"osVersion,omitempty"`
Platform string `json:"platform,omitempty"`
Data any `json:"data,omitempty"`
Logger util.Logger `json:"-"`
Context context.Context `json:"-"` //nolint:containedctx // properly closed, never directly used
Span *telemetry.Span `json:"-"`
RenderElapsed float64 `json:"renderElapsed,omitempty"`
}
func (p *PageState) AddIcon(keys ...string) {
lo.ForEach(keys, func(k string, _ int) {
if !lo.Contains(p.Icons, k) {
p.Icons = append(p.Icons, k)
}
})
}
func (p *PageState) TitleString() string {
if p.Title == "" {
return util.AppName
}
return fmt.Sprintf("%s - %s", p.Title, util.AppName)
}
func (p *PageState) Username() string {
return p.Profile.Name
}
func (p *PageState) Clean(_ *fasthttp.RequestCtx, as *app.State) error {
if p.Profile != nil && p.Profile.Theme == "" {
p.Profile.Theme = theme.ThemeDefault.Key
}
if p.RootIcon == "" {
p.RootIcon = defaultIcon
}
if p.RootPath == "" {
p.RootPath = "/"
}
if p.RootTitle == "" {
p.RootTitle = defaultRootTitle
}
if defaultRootTitleAppend != "" {
p.RootTitle += " " + defaultRootTitleAppend
}
if p.SearchPath == "" {
p.SearchPath = DefaultSearchPath
}
if p.ProfilePath == "" {
p.ProfilePath = DefaultProfilePath
}
if len(p.Menu) == 0 {
m, data, err := cmenu.MenuFor(p.Context, p.Authed, p.Admin, p.Profile, p.Params, as, p.Logger)
if err != nil {
return err
}
if data != nil && p.Data == nil {
p.Data = data
}
p.Menu = m
}
return nil
}
func (p *PageState) Close() {
if p.Span != nil {
p.Span.Complete()
}
}
func (p *PageState) ClassDecl() string {
var ret []string
if p.Profile.Mode != "" {
ret = append(ret, p.Profile.ModeClass())
}
if p.Browser != "" {
ret = append(ret, "browser-"+p.Browser)
}
if p.OS != "" {
ret = append(ret, "os-"+p.OS)
}
if p.Platform != "" {
ret = append(ret, "platform-"+p.Platform)
}
if len(ret) == 0 {
return ""
}
classes := strings.Join(ret, " ")
return fmt.Sprintf(` class="%s"`, classes)
}