79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
package templates
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
)
|
|
|
|
type AccountLink struct {
|
|
Alert bool
|
|
}
|
|
|
|
type Link struct {
|
|
Href string
|
|
Name string
|
|
}
|
|
|
|
type CopyList struct {
|
|
Domain string
|
|
Reference string
|
|
}
|
|
|
|
type User struct {
|
|
Name string
|
|
CopyList *CopyList
|
|
}
|
|
|
|
type NavCollapse struct {
|
|
User *User
|
|
AccountLink *AccountLink
|
|
Links []Link
|
|
}
|
|
|
|
func DefaultNavCollapse() NavCollapse {
|
|
return NavCollapse{Links: []Link{{Href: "/", Name: "Home"}}}
|
|
}
|
|
|
|
func UserNavCollapse(username string, accountAlert bool) NavCollapse {
|
|
return NavCollapse{
|
|
Links: []Link{{Href: "/", Name: "Home"}},
|
|
User: &User{Name: username},
|
|
AccountLink: &AccountLink{Alert: accountAlert},
|
|
}
|
|
}
|
|
|
|
type InputProps struct {
|
|
Type string `json:",omitempty"`
|
|
Name string
|
|
Required bool `json:",omitempty"`
|
|
Value string
|
|
Error string `json:",omitempty"`
|
|
MinLength uint `json:",omitempty"`
|
|
}
|
|
|
|
func (p *InputProps) Validate() bool {
|
|
if p.Required && p.Value == "" {
|
|
p.Error = fmt.Sprintf("%v is required", p.Name)
|
|
return false
|
|
}
|
|
|
|
value_len := len(p.Value)
|
|
if p.MinLength > 0 && int(p.MinLength) > value_len {
|
|
p.Error = fmt.Sprintf("%v requires at least %v characters (currently %v characters)", p.Name, p.MinLength, value_len)
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
var Template *template.Template
|
|
|
|
func init() {
|
|
Template = load()
|
|
}
|
|
|
|
func load() *template.Template {
|
|
t := template.Must(template.ParseGlob("templates/*.gotmpl"))
|
|
return t
|
|
}
|