-
Notifications
You must be signed in to change notification settings - Fork 4
/
page.go
128 lines (117 loc) · 3.67 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
// Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"context"
"fmt"
"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"`
Accounts user.Accounts `json:"accounts,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"`
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(n string) {
for _, icon := range p.Icons {
if icon == n {
return
}
}
p.Icons = append(p.Icons, n)
}
func (p *PageState) TitleString() string {
if p.Title == "" {
return util.AppName
}
return fmt.Sprintf("%s - %s", p.Title, util.AppName)
}
func (p *PageState) User() string {
if len(p.Accounts) == 0 {
return "anonymous"
}
return p.Accounts[0].Email
}
func (p *PageState) Clean(rc *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()
}
}