From 185cc4dc60b4b55def32f2d4632b18ad23c95b59 Mon Sep 17 00:00:00 2001 From: Wim Brand Date: Fri, 22 Feb 2019 00:04:57 +0100 Subject: [PATCH] Easy form generation. TODO: parse forms --- res/template/account/user_password.html | 83 +++++++++++++++++++ res/template/fragments/form.html | 102 ++++++++++++++++++++++++ webcontroller/forms/form.go | 56 +++++++++++++ webcontroller/template_data.go | 10 ++- webcontroller/user_account.go | 37 +++++++++ webcontroller/web_controller.go | 3 + 6 files changed, 288 insertions(+), 3 deletions(-) create mode 100644 res/template/account/user_password.html create mode 100644 res/template/fragments/form.html create mode 100644 webcontroller/forms/form.go diff --git a/res/template/account/user_password.html b/res/template/account/user_password.html new file mode 100644 index 0000000..f1a9163 --- /dev/null +++ b/res/template/account/user_password.html @@ -0,0 +1,83 @@ +{{define "user_password"}} + + + {{template "meta_tags" "Updating Password"}} + {{template "user_style"}} + + + +
+ {{template "menu" .}} + +

Update Password

+
+
+ + + + + + + + + + + + + + + + + + +
Old Password
New Password
New password verification
Back +
+
+
+ {{template "footer"}} +
+ + + + +{{end}} diff --git a/res/template/fragments/form.html b/res/template/fragments/form.html new file mode 100644 index 0000000..8a662d9 --- /dev/null +++ b/res/template/fragments/form.html @@ -0,0 +1,102 @@ +{{define "form"}} +

{{.Title}}

+ {{.PreFormHTML}} +

+ {{if eq .Submit true}} + {{if eq .SubmitSuccess true}} +
+ {{index .SubmitMessages 0}} +
+ {{else}} +
+ Something went wrong, please correct these errors before continuing:
+ +
+ {{end}} + {{end}} + +
+ + {{range $index, $field := .Fields}} + + + + {{if ne $field.Description ""}} + + + + {{end}} + {{if eq $field.Separator true}} + + {{end}} + + {{end}} + + {{if eq .BackLink ""}} + + {{else}} + + + {{end}} + +
{{$field.Label}} + {{if eq $field.Type "text"}} + + {{else if eq $field.Type "username"}} + + {{else if eq $field.Type "email"}} + + {{else if eq $field.Type "current-password"}} + + {{else if eq $field.Type "new-password"}} + + {{else if eq $field.Type "captcha"}} + +
+ {{end}} +
{{$field.Description}}

+ {{if eq .SubmitRed true}} + + {{else}} + + {{end}} + + Back + + {{if eq .SubmitRed true}} + + {{else}} + + {{end}} +
+
+
+ {{.PostFormHTML}} +
+{{end}} +{{define "form_page"}} + + + + {{template "meta_tags" .Title}} + {{template "user_style" .}} + + + + +
+ {{template "menu" .}} + + {{template "form" .Form}} + + {{template "footer"}} +
+ + {{template "analytics"}} + + +{{end}} diff --git a/webcontroller/forms/form.go b/webcontroller/forms/form.go new file mode 100644 index 0000000..50867d4 --- /dev/null +++ b/webcontroller/forms/form.go @@ -0,0 +1,56 @@ +package forms + +import ( + "html/template" +) + +// Form is a form which can be rendered in HTML and submitted +type Form struct { + Title string // Shown in a large font above the form + PreFormHTML template.HTML // Content to be rendered above the form + + Fields []Field + + BackLink string // Empty for no back link + SubmitLabel string // Label for the submit button + SubmitRed bool // If the submit button should be red or green + + PostFormHTML template.HTML // Content to be rendered below the form + + // Fields to render if the form has been submitted once + Submit bool // If the form has been submitted + SubmitSuccess bool // If the submission was a success + SubmitMessages []string // Messages telling the user the results +} + +// Field is a single input field in a form +type Field struct { + // Used for reading the data. Entered data is POSTed back to the same URL with this name + Name string + // Is entered in the input field by default + DefaultValue string + // Text next to the input field + Label string + // Text below the input field + Description string + // Separates fields with a horizontal rule + Separator bool + + Type FieldType + + // Only used when Type = `captcha` + CaptchaSiteKey string +} + +// FieldType defines the type a form field has and how it should be rendered +type FieldType string + +// Fields which can be in a form +const ( + FieldTypeText FieldType = "text" + FieldTypeUsername FieldType = "username" + FieldTypeEmail FieldType = "email" + FieldTypeOldPassword FieldType = "current-password" + FieldTypeNewPassword FieldType = "new-password" + FieldTypeCaptcha FieldType = "captcha" +) diff --git a/webcontroller/template_data.go b/webcontroller/template_data.go index 8749db7..d89de2f 100644 --- a/webcontroller/template_data.go +++ b/webcontroller/template_data.go @@ -7,6 +7,7 @@ import ( "time" "fornaxian.com/pixeldrain-web/pixelapi" + "fornaxian.com/pixeldrain-web/webcontroller/forms" "github.com/Fornaxian/log" ) @@ -19,12 +20,15 @@ type TemplateData struct { APIEndpoint template.URL PixelAPI *pixelapi.PixelAPI - Other interface{} - URLQuery url.Values - // Only used on file viewer page Title string OGData OGData + + Other interface{} + URLQuery url.Values + + // Only used for pages containing forms + Form forms.Form } func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) *TemplateData { diff --git a/webcontroller/user_account.go b/webcontroller/user_account.go index f83b4eb..df2fd96 100644 --- a/webcontroller/user_account.go +++ b/webcontroller/user_account.go @@ -1,9 +1,11 @@ package webcontroller import ( + "html/template" "net/http" "fornaxian.com/pixeldrain-web/pixelapi" + "fornaxian.com/pixeldrain-web/webcontroller/forms" "github.com/Fornaxian/log" "github.com/julienschmidt/httprouter" ) @@ -54,3 +56,38 @@ func (wc *WebController) serveLogout( http.Redirect(w, r, "/", http.StatusSeeOther) } + +func (wc *WebController) formPassword( + w http.ResponseWriter, + r *http.Request, + p httprouter.Params, +) { + td := wc.newTemplateData(w, r) + td.Form = forms.Form{ + Title: "Test Form", + PreFormHTML: template.HTML("preform"), + Fields: []forms.Field{ + forms.Field{ + Name: "field_1", + DefaultValue: "def val 1", + Label: "Field 1", + Description: "Description of field one", + Separator: false, + Type: forms.FieldTypeUsername, + }, + }, + BackLink: "/", + SubmitLabel: "ayy lmao send", + SubmitRed: false, + PostFormHTML: template.HTML("postform"), + Submit: true, + SubmitSuccess: true, + SubmitMessages: []string{"yay success"}, + } + + err := wc.templates.Get().ExecuteTemplate(w, "form_page", td) + if err != nil { + log.Error("Error executing template '%s': %s", "register", err) + } + +} diff --git a/webcontroller/web_controller.go b/webcontroller/web_controller.go index e55469f..ccf278e 100644 --- a/webcontroller/web_controller.go +++ b/webcontroller/web_controller.go @@ -67,6 +67,9 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon r.GET(p+"/user/files" /* */, wc.serveTemplate("user_files", true)) r.GET(p+"/user/lists" /* */, wc.serveTemplate("user_lists", true)) r.GET(p+"/user/filemanager" /**/, wc.serveTemplate("file_manager", true)) + r.GET(p+"/user/password" /* */, wc.serveTemplate("user_password", true)) + + r.GET(p+"/testform", wc.formPassword) r.NotFound = http.HandlerFunc(wc.serveNotFound)