fully implemented timezone list dialog field

This commit is contained in:
2025-10-11 10:32:27 -06:00
parent 5ff7e3e35c
commit 297c1b9157
7 changed files with 56 additions and 8 deletions
+7 -2
View File
@@ -18,6 +18,7 @@ import (
"strings"
"time"
"git.erbosoft.com/amy/amsterdam/config"
"git.erbosoft.com/amy/amsterdam/database"
"gopkg.in/yaml.v3"
)
@@ -173,9 +174,13 @@ func (d *Dialog) Render(ctxt AmContext) (string, any, error) {
switch fld.Type {
case "password": // clear all "password" fields as a security measure
d.Fields[i].Value = ""
case "localelist": // default locale to en-US if we don't have one
case "localelist": // default locale if we don't have one
if d.Fields[i].Value == "" {
d.Fields[i].Value = "en-US"
d.Fields[i].Value = config.GlobalConfig.Defaults.Language
}
case "tzlist": // default timezone if we don't have any
if d.Fields[i].Value == "" {
d.Fields[i].Value = config.GlobalConfig.Defaults.TimeZone
}
}
}
+33 -5
View File
@@ -22,7 +22,6 @@ import (
"sync"
"time"
"unicode"
_ "unsafe" // HACK HACK HACK
"git.erbosoft.com/amy/amsterdam/config"
"github.com/CloudyKit/jet/v6"
@@ -31,6 +30,7 @@ import (
"github.com/biter777/countries"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
"github.com/tkuchiki/go-timezone"
"golang.org/x/text/language"
"golang.org/x/text/language/display"
)
@@ -79,16 +79,42 @@ func internalGetLanguageList() []Language {
slices.SortFunc(cachedLanguageList, func(a Language, b Language) int {
return strings.Compare(a.Name, b.Name)
})
log.Infof("internalGetLanguageList() loaded %d languages", len(cachedLanguageList))
}
return cachedLanguageList
}
// getLanguageList is a wrapper around the list of languages that can be added to the template context.
func getLanguageList(a jet.Arguments) reflect.Value {
rc := internalGetLanguageList()
log.Infof("GetLanguageList() provides %d items", len(rc))
return reflect.ValueOf(rc)
return reflect.ValueOf(internalGetLanguageList())
}
// cachedTimeZoneList is a wrapper around timezone.Timezones() that produces it by timezone name.
var cachedTimeZoneList []string = nil
// timeZoneListMutex controls access to internalGetTimeZoneList.
var timeZoneListMutex sync.Mutex
// internalGetTimeZoneList is a wrapper around TimeZone.TimeZones() that sorts and compacts the list.
func internalGetTimeZoneList() []string {
timeZoneListMutex.Lock()
defer timeZoneListMutex.Unlock()
if cachedTimeZoneList == nil {
timezones := timezone.New().Timezones()
ilist := make([]string, 0, len(timezones)*5)
for k, v := range timezones {
ilist = append(ilist, k)
ilist = append(ilist, v...)
}
slices.Sort(ilist)
cachedTimeZoneList = slices.Compact(ilist)
}
return cachedTimeZoneList
}
// getTimeZoneList is a wrapper around internalGetTimeZoneList that can be added to the template context.
func getTimeZoneList(a jet.Arguments) reflect.Value {
return reflect.ValueOf(internalGetTimeZoneList())
}
// cachedCountryList is the cached country list after sorting.
@@ -224,6 +250,7 @@ func SetupTemplates() {
views.AddGlobal("GlobalConfig", config.GlobalConfig)
views.AddGlobalFunc("GetCountryList", getCountryList)
views.AddGlobalFunc("GetLanguageList", getLanguageList)
views.AddGlobalFunc("GetTimeZoneList", getTimeZoneList)
views.AddGlobalFunc("GetMonthList", getMonthList)
views.AddGlobalFunc("MakeIntRange", makeIntRange)
views.AddGlobalFunc("MakeYearRange", makeYearRange)
@@ -236,6 +263,7 @@ func SetupTemplates() {
// preload the lists in the background
go internalGetCountryList()
go internalGetLanguageList()
go internalGetTimeZoneList()
}
// TemplateRenderer is the Renderer instance set into the Echo context at creation time, to render Jet templates.
+4 -1
View File
@@ -144,9 +144,12 @@
{{ .Caption }}{{ if .Subcaption != "" }} {{ .Subcaption }}{{ end }}
{{ if .Required }}<span class="text-red-600">*</span>{{ end }}
</label>
{{ v := .Value }}
<select id="{{ .Name }}" name="{{ .Name }}" {{ if .Required }}required{{ end }}
class="flex-1 max-w-md px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<option value="XX">Unknown</option>{* TODO *}
{{ range GetTimeZoneList() }}
<option value="{{ . }}" {{ if v == . }}selected{{ end }}>{{ . }}</option>
{{ end }}
</select>
</div>
{{ else if .Type == "date" }}