Core

/app/controller/cutil/page.go (5.8 KB)

  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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cutil

import (
"context"
"fmt"
"slices"
"strings"
"time"

"github.com/samber/lo"
"github.com/valyala/fasthttp"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/controller/cmenu"
"{{{ .Package }}}/app/lib/filter"
"{{{ .Package }}}/app/lib/menu"
"{{{ .Package }}}/app/lib/telemetry"
"{{{ .Package }}}/app/lib/theme"
"{{{ .Package }}}/app/lib/user"{{{ if .HasUser }}}
dbuser "{{{ .Package }}}/app/user"{{{ end }}}
"{{{ .Package }}}/app/util"
)

const ({{{ if .HasModule "search" }}}
DefaultSearchPath = "/search"{{{ end }}}
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 *fasthttp.URI `json:"-"`
Menu menu.Items `json:"menu,omitempty"`
Breadcrumbs cmenu.Breadcrumbs `json:"breadcrumbs,omitempty"`
Flashes []string `json:"flashes,omitempty"`
Session util.ValueMap `json:"-"`{{{ if .HasUser }}}
User *dbuser.User `json:"user,omitempty"`{{{ end }}}
Profile *user.Profile `json:"profile,omitempty"`{{{ if .HasAccount }}}
Accounts user.Accounts `json:"accounts,omitempty"`{{{ end }}}
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"`{{{ if .HasModule "search" }}}
SearchPath string `json:"searchPath,omitempty"`{{{ end }}}
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"`
Started time.Time `json:"started,omitempty"`
RenderElapsed float64 `json:"renderElapsed,omitempty"`
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)
}{{{ if .HasUser }}}

func (p *PageState) Username() string {
if p.User != nil {
return p.User.Name
}
return p.Profile.Name
}

func (p *PageState) AuthString() string {
n := p.Profile.String()
if p.User != nil {
n = p.User.Name
}
msg := fmt.Sprintf("signed in as %s", n){{{ if .HasAccount }}}
if len(p.Accounts) == 0 {
if n == user.DefaultProfile.Name {
return "click to sign in"
}
return msg
}
return fmt.Sprintf("%s using [%s]", msg, p.Accounts.TitleString()){{{ else }}}
return msg{{{ end }}}
}{{{ else }}}

func (p *PageState) Username() string {
return p.Profile.Name
}{{{ if .HasAccount }}}

func (p *PageState) AuthString() string {
n := p.Profile.String()
msg := fmt.Sprintf("signed in as %s", n)
if len(p.Accounts) == 0 {
if n == user.DefaultProfile.Name {
return "click to sign in"
}
return msg
}
return fmt.Sprintf("%s using [%s]", msg, p.Accounts.TitleString())
}{{{ end }}}{{{ end }}}

func (p *PageState) Clean(_ *fasthttp.RequestCtx, 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 .HasModule "search" }}}
if p.SearchPath == "" {
p.SearchPath = DefaultSearchPath
}{{{ end }}}
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 {
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=%q`, classes)
}