bugfixes and stiffening in the dialog rendering pipeline and the login dialog

This commit is contained in:
2025-10-08 13:35:21 -06:00
parent 933d028196
commit af25adc85f
7 changed files with 55 additions and 24 deletions
+1
View File
@@ -200,6 +200,7 @@ func (c *amContext) Scratchpad() map[string]any {
func (c *amContext) SubRender(name string) ([]byte, error) {
view, err := views.GetTemplate(name)
if err != nil {
log.Errorf("unable to load template \"%s\": %v", name, err)
return nil, err
}
buf := new(bytes.Buffer)
+20 -12
View File
@@ -47,6 +47,12 @@ type Dialog struct {
Fields []DialogItem `yaml:"fields"`
}
// VRange is used as a return type for ValueRange.
type VRange struct {
Low int
High int
}
//go:embed dialogs/*
var dialogs embed.FS
@@ -99,14 +105,14 @@ func (fld *DialogItem) IsChecked() bool {
}
// ValueRange returns the minimum and maximum values for an integer field.
func (fld *DialogItem) ValueRange() (int, int) {
func (fld *DialogItem) ValueRange() VRange {
if fld.Type == "integer" && fld.Param != "" {
parms := strings.Split(fld.Param, "-")
low, _ := strconv.Atoi(parms[0])
high, _ := strconv.Atoi(parms[1])
return low, high
return VRange{Low: low, High: high}
}
return -1, -1
return VRange{Low: -1, High: -1}
}
// AsDate returns the value of a date field as a Go date.
@@ -152,10 +158,12 @@ func (d *Dialog) Field(name string) *DialogItem {
*/
func (d *Dialog) Render(ctxt AmContext) (string, any, error) {
required := false
for _, fld := range d.Fields {
for i, fld := range d.Fields {
if fld.Required {
required = true
break
required = true // display the "required" blurb
}
if fld.Type == "password" { // clear all "password" fields as a security measure
d.Fields[i].Value = ""
}
}
ctxt.VarMap().Set("amsterdam_required", required)
@@ -304,12 +312,12 @@ func validateIntegerField(fld *DialogItem) error {
v, err = strconv.Atoi(fld.Value)
if err == nil {
fld.AuxData = v // cache parsed value
lo, hi := fld.ValueRange()
if lo != -1 && hi != -1 {
if v < lo {
return fmt.Errorf("value of field \"%s\" cannot be less than %d", fld.Caption, lo)
} else if v > hi {
return fmt.Errorf("value of field \"%s\" cannot be greater than %d", fld.Caption, hi)
vr := fld.ValueRange()
if vr.Low != -1 && vr.High != -1 {
if v < vr.Low {
return fmt.Errorf("value of field \"%s\" cannot be less than %d", fld.Caption, vr.Low)
} else if v > vr.High {
return fmt.Errorf("value of field \"%s\" cannot be greater than %d", fld.Caption, vr.High)
}
}
} else {
+4 -1
View File
@@ -18,7 +18,7 @@ import (
"git.erbosoft.com/amy/amsterdam/config"
"git.erbosoft.com/amy/amsterdam/database"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
log "github.com/sirupsen/logrus"
)
func sendPageData(ctxt echo.Context, amctxt AmContext, command string, data any) error {
@@ -39,6 +39,9 @@ func sendPageData(ctxt echo.Context, amctxt AmContext, command string, data any)
default:
err = fmt.Errorf("unknown rendering type: %s", command)
}
if err != nil {
log.Errorf("sendPageData() barfed with %v", err)
}
return err
}
+3 -1
View File
@@ -29,6 +29,7 @@ import (
"github.com/CloudyKit/jet/v6/loaders/multi"
"github.com/biter777/countries"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
)
//go:embed views/*
@@ -130,7 +131,7 @@ func makeYearRange(a jet.Arguments) reflect.Value {
}
}
/* CapitalizeString changes the first character of trhe string to a capital.
/* CapitalizeString changes the first character of the string to a capital.
* Parameters:
* s - The string to be capitalized.
* Returns:
@@ -187,6 +188,7 @@ func (r *TemplateRenderer) Render(w io.Writer, name string, data any, c echo.Con
view, err := views.GetTemplate(name)
if err != nil {
log.Errorf("Unable to load template \"%s\": %v", name, err)
return err
}
var vmap jet.VarMap = nil
+5 -3
View File
@@ -6,6 +6,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*}
<!-- BEGIN DIALOG {{ amsterdam_dialog.Name }} -->
<div class="p-4">
<div class="mb-6">
<h1 class="text-blue-800 text-4xl font-bold mb-2">{{ amsterdam_dialog.Title }}</h1>
@@ -78,9 +79,9 @@
{{ if .MaxLength > 0 }}maxlength="{{ .MaxLength }}"{{ end }}
value="{{ .Value }}"
class="flex-1 px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" />
{{ lo, hi := .ValueRange() }}
{{ if lo != -1 && hi != -1 }}
<span class="text-sm">({{ lo }}-{{ hi }})</span>
{{ vr := .ValueRange() }}
{{ if vr.Low != -1 && vr.High != -1 }}
<span class="text-sm">({{ vr.Low }}-{{ vr.High }})</span>
{{ end }}
</div>
{{ else if .Type == "password" }}
@@ -170,3 +171,4 @@
</div>
</form>
</div>
<!-- END DIALOG {{ amsterdam_dialog.Name }} -->