-
Notifications
You must be signed in to change notification settings - Fork 4
/
page.go
170 lines (156 loc) · 4.95 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Package cutil - Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"context"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/samber/lo"
"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 {
Action string `json:"action,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Method string `json:"method,omitempty"`
URI *url.URL `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"`
DefaultNavIcon string `json:"defaultNavIcon,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"`
DefaultFormat string `json:"defaultFormat,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"`
Started time.Time `json:"started,omitempty"`
RenderElapsed float64 `json:"renderElapsed,omitempty"`
ResponseBytes int64 `json:"responseBytes,omitempty"`
RequestBody []byte `json:"-"`
W *WriteCounter `json:"-"`
Logger util.Logger `json:"-"`
Context context.Context `json:"-"` //nolint:containedctx // properly closed, never directly used
Span *telemetry.Span `json:"-"`
}
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)
}
})
slices.Sort(p.Icons)
}
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(_ *http.Request, as *app.State) error {
if p.Profile != nil && p.Profile.Theme == "" {
p.Profile.Theme = theme.Default.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) LogError(msg string, args ...any) {
p.Logger.Errorf(msg, args...)
}
func (p *PageState) ClassDecl() string {
ret := &util.StringSlice{}
if p.Profile.Mode != "" {
ret.Push(p.Profile.ModeClass())
}
if p.Browser != "" {
ret.Push("browser-" + p.Browser)
}
if p.OS != "" {
ret.Push("os-" + p.OS)
}
if p.Platform != "" {
ret.Push("platform-" + p.Platform)
}
if ret.Empty() {
return ""
}
classes := ret.Join(" ")
return fmt.Sprintf(` class=%q`, classes)
}
func (p *PageState) SetTitleAndData(title string, data any) {
p.Title = title
p.Data = data
}