minor code refactor

This commit is contained in:
Adriano Caloiaro 2025-01-14 08:51:29 -07:00
parent d8da2b787d
commit 131f45366c
No known key found for this signature in database
GPG key ID: C2BC56DE73CE3F75
3 changed files with 35 additions and 54 deletions

33
frm.go
View file

@ -108,19 +108,6 @@ func (f *Frm) ListForms(ctx context.Context, args ListFormsArgs) (forms []Form,
return
}
// URLPath returns paths to frm endpoints
//
// This function takes into account where frm is mounted on an application's router.
// e.g. If frm is mounted with `frmchi.Mount(chiRouter, "/frm", f)` then `Path(ctx, "/forms/100")` returns `/frm/forms/100`
func URLPath(ctx context.Context, path string) string {
base, ok := ctx.Value(internal.CollectorMountPointContextKey).(string)
if !ok {
return "/"
}
urlPath := filepath.Clean(fmt.Sprintf("%s/%s", base, path))
return urlPath
}
// Instance returns the frm instance from the request context
func Instance(ctx context.Context) (i *Frm, err error) {
var ok bool
@ -131,13 +118,16 @@ func Instance(ctx context.Context) (i *Frm, err error) {
return
}
// BuilderBuildPathBase returns the builder /build base URL path
func BuilderBuildPathBase(ctx context.Context) string {
base, ok := ctx.Value(internal.BuilderMountPointContextKey).(string)
// CollectorPath returns paths to frm collector endpoints
//
// It uses the collector's mount point on the router to generate collector paths
func CollectorPath(ctx context.Context, path string) string {
base, ok := ctx.Value(internal.CollectorMountPointContextKey).(string)
if !ok {
return "/"
}
return base
urlPath := filepath.Clean(fmt.Sprintf("%s/%s", base, path))
return urlPath
}
// BuilderPathForm returns the builder URL path for the provided form ID
@ -150,15 +140,6 @@ func BuilderPathForm(ctx context.Context, formID int64) string {
return fmt.Sprintf("%s/%d", base, formID)
}
// BuilderPathFormField returns the URL path for the provided form ID's field
func BuilderPathFormField(ctx context.Context, formID int, fieldID uuid.UUID) string {
i, err := Instance(ctx)
if err != nil {
return ""
}
return fmt.Sprintf("%s/build/%d/fields/%s", i.BuilderMountPoint, formID, fieldID)
}
// CollectorPathForm returns the collector URL path for the provided form ID
func CollectorPathForm(ctx context.Context, formID int64, path ...string) string {
base, ok := ctx.Value(internal.CollectorMountPointContextKey).(string)

View file

@ -126,7 +126,7 @@ templ FormBuilderNav(form frm.Form) {
// SafePath returns mountpoint-aware SafeURL paths for the given path
func SafePath(ctx context.Context, path string) templ.SafeURL {
return templ.SafeURL(frm.URLPath(ctx, path))
return templ.SafeURL(frm.CollectorPath(ctx, path))
}
// head simply provides the <head> element
@ -136,12 +136,12 @@ templ head(pageTitle string) {
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
@loadDependenciesOnce.Once() {
<link href={ frm.URLPath(ctx, "/static/css/styles.css") } rel="stylesheet"/>
<link rel="stylesheet" href={ frm.URLPath(ctx, "/static/css/choices.min.css") } nonce={ templ.GetNonce(ctx) }/>
<script type="text/javascript" src={ frm.URLPath(ctx, "/static/js/htmx.js") } nonce={ templ.GetNonce(ctx) }></script>
<script type="text/javascript" src={ frm.URLPath(ctx, "/static/js/htmx-response-targets.js") } nonce={ templ.GetNonce(ctx) }></script>
<script type="text/javascript" src={ frm.URLPath(ctx, "/static/js/hyperscript.js") } nonce={ templ.GetNonce(ctx) }></script>
<script ytpe="text/javascript" src={ frm.URLPath(ctx, "/static/js/choices.min.js") } nonce={ templ.GetNonce(ctx) }></script>
<link href={ frm.CollectorPath(ctx, "/static/css/styles.css") } rel="stylesheet"/>
<link rel="stylesheet" href={ frm.CollectorPath(ctx, "/static/css/choices.min.css") } nonce={ templ.GetNonce(ctx) }/>
<script type="text/javascript" src={ frm.CollectorPath(ctx, "/static/js/htmx.js") } nonce={ templ.GetNonce(ctx) }></script>
<script type="text/javascript" src={ frm.CollectorPath(ctx, "/static/js/htmx-response-targets.js") } nonce={ templ.GetNonce(ctx) }></script>
<script type="text/javascript" src={ frm.CollectorPath(ctx, "/static/js/hyperscript.js") } nonce={ templ.GetNonce(ctx) }></script>
<script ytpe="text/javascript" src={ frm.CollectorPath(ctx, "/static/js/choices.min.js") } nonce={ templ.GetNonce(ctx) }></script>
<script type="text/javascript" src="https://unpkg.com/external-svg-loader@latest/svg-loader.min.js" nonce={ templ.GetNonce(ctx) } async></script>
<script type="text/javascript" src="https://unpkg.com/sortablejs@latest/Sortable.min.js" nonce={ templ.GetNonce(ctx) }></script>
<script type="text/javascript">
@ -621,7 +621,7 @@ templ fieldLogicConfiguration(form frm.Form, field types.FormField) {
<div id={ fmt.Sprintf("field-%s-logic", field.ID.String()) } class="flex flex-col gap-5 hidden">
<div>
<div
data-hx-get={ frm.URLPath(ctx, fmt.Sprintf("/build/%d/logic_configurator/%s/step3", form.ID, field.ID.String())) }
data-hx-get={ frm.CollectorPath(ctx, fmt.Sprintf("/build/%d/logic_configurator/%s/step3", form.ID, field.ID.String())) }
data-hx-trigger={ LogicConfiguratorTargetFieldSelected }
data-hx-swap="innerHTML"
data-hx-target={ fmt.Sprintf("#logic-field-value-chooser-%s", field.ID.String()) }

View file

@ -369,7 +369,7 @@ func FormBuilderNav(form frm.Form) templ.Component {
// SafePath returns mountpoint-aware SafeURL paths for the given path
func SafePath(ctx context.Context, path string) templ.SafeURL {
return templ.SafeURL(frm.URLPath(ctx, path))
return templ.SafeURL(frm.CollectorPath(ctx, path))
}
// head simply provides the <head> element
@ -441,9 +441,9 @@ func head(pageTitle string) templ.Component {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var19 string
templ_7745c5c3_Var19, templ_7745c5c3_Err = templ.JoinStringErrs(frm.URLPath(ctx, "/static/css/styles.css"))
templ_7745c5c3_Var19, templ_7745c5c3_Err = templ.JoinStringErrs(frm.CollectorPath(ctx, "/static/css/styles.css"))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 139, Col: 58}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 139, Col: 64}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var19))
if templ_7745c5c3_Err != nil {
@ -454,9 +454,9 @@ func head(pageTitle string) templ.Component {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var20 string
templ_7745c5c3_Var20, templ_7745c5c3_Err = templ.JoinStringErrs(frm.URLPath(ctx, "/static/css/choices.min.css"))
templ_7745c5c3_Var20, templ_7745c5c3_Err = templ.JoinStringErrs(frm.CollectorPath(ctx, "/static/css/choices.min.css"))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 140, Col: 80}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 140, Col: 86}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var20))
if templ_7745c5c3_Err != nil {
@ -469,7 +469,7 @@ func head(pageTitle string) templ.Component {
var templ_7745c5c3_Var21 string
templ_7745c5c3_Var21, templ_7745c5c3_Err = templ.JoinStringErrs(templ.GetNonce(ctx))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 140, Col: 110}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 140, Col: 116}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var21))
if templ_7745c5c3_Err != nil {
@ -480,9 +480,9 @@ func head(pageTitle string) templ.Component {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var22 string
templ_7745c5c3_Var22, templ_7745c5c3_Err = templ.JoinStringErrs(frm.URLPath(ctx, "/static/js/htmx.js"))
templ_7745c5c3_Var22, templ_7745c5c3_Err = templ.JoinStringErrs(frm.CollectorPath(ctx, "/static/js/htmx.js"))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 141, Col: 78}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 141, Col: 84}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var22))
if templ_7745c5c3_Err != nil {
@ -495,7 +495,7 @@ func head(pageTitle string) templ.Component {
var templ_7745c5c3_Var23 string
templ_7745c5c3_Var23, templ_7745c5c3_Err = templ.JoinStringErrs(templ.GetNonce(ctx))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 141, Col: 108}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 141, Col: 114}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var23))
if templ_7745c5c3_Err != nil {
@ -506,9 +506,9 @@ func head(pageTitle string) templ.Component {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var24 string
templ_7745c5c3_Var24, templ_7745c5c3_Err = templ.JoinStringErrs(frm.URLPath(ctx, "/static/js/htmx-response-targets.js"))
templ_7745c5c3_Var24, templ_7745c5c3_Err = templ.JoinStringErrs(frm.CollectorPath(ctx, "/static/js/htmx-response-targets.js"))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 142, Col: 95}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 142, Col: 101}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var24))
if templ_7745c5c3_Err != nil {
@ -521,7 +521,7 @@ func head(pageTitle string) templ.Component {
var templ_7745c5c3_Var25 string
templ_7745c5c3_Var25, templ_7745c5c3_Err = templ.JoinStringErrs(templ.GetNonce(ctx))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 142, Col: 125}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 142, Col: 131}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var25))
if templ_7745c5c3_Err != nil {
@ -532,9 +532,9 @@ func head(pageTitle string) templ.Component {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var26 string
templ_7745c5c3_Var26, templ_7745c5c3_Err = templ.JoinStringErrs(frm.URLPath(ctx, "/static/js/hyperscript.js"))
templ_7745c5c3_Var26, templ_7745c5c3_Err = templ.JoinStringErrs(frm.CollectorPath(ctx, "/static/js/hyperscript.js"))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 143, Col: 85}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 143, Col: 91}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var26))
if templ_7745c5c3_Err != nil {
@ -547,7 +547,7 @@ func head(pageTitle string) templ.Component {
var templ_7745c5c3_Var27 string
templ_7745c5c3_Var27, templ_7745c5c3_Err = templ.JoinStringErrs(templ.GetNonce(ctx))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 143, Col: 115}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 143, Col: 121}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var27))
if templ_7745c5c3_Err != nil {
@ -558,9 +558,9 @@ func head(pageTitle string) templ.Component {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var28 string
templ_7745c5c3_Var28, templ_7745c5c3_Err = templ.JoinStringErrs(frm.URLPath(ctx, "/static/js/choices.min.js"))
templ_7745c5c3_Var28, templ_7745c5c3_Err = templ.JoinStringErrs(frm.CollectorPath(ctx, "/static/js/choices.min.js"))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 144, Col: 85}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 144, Col: 91}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var28))
if templ_7745c5c3_Err != nil {
@ -573,7 +573,7 @@ func head(pageTitle string) templ.Component {
var templ_7745c5c3_Var29 string
templ_7745c5c3_Var29, templ_7745c5c3_Err = templ.JoinStringErrs(templ.GetNonce(ctx))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 144, Col: 115}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 144, Col: 121}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var29))
if templ_7745c5c3_Err != nil {
@ -2016,9 +2016,9 @@ func fieldLogicConfiguration(form frm.Form, field types.FormField) templ.Compone
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var92 string
templ_7745c5c3_Var92, templ_7745c5c3_Err = templ.JoinStringErrs(frm.URLPath(ctx, fmt.Sprintf("/build/%d/logic_configurator/%s/step3", form.ID, field.ID.String())))
templ_7745c5c3_Var92, templ_7745c5c3_Err = templ.JoinStringErrs(frm.CollectorPath(ctx, fmt.Sprintf("/build/%d/logic_configurator/%s/step3", form.ID, field.ID.String())))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 624, Col: 116}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/common.templ`, Line: 624, Col: 122}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var92))
if templ_7745c5c3_Err != nil {