Browse Source

Builder, data form

Vova Tkach 6 years ago
parent
commit
02512c77ea
2 changed files with 140 additions and 2 deletions
  1. 96 0
      engine/builder/data_form.go
  2. 44 2
      modules/module_users.go

+ 96 - 0
engine/builder/data_form.go

@@ -0,0 +1,96 @@
+package builder
+
+import (
+	"html"
+
+	"golang-fave/engine/wrapper"
+)
+
+const (
+	DFKHidden = iota
+	DFKText
+	DFKEmail
+	DFKPassword
+	DFKTextArea
+	DFKSubmit
+)
+
+type DataFormField struct {
+	Caption     string
+	Kind        int
+	Name        string
+	Value       string
+	Placeholder string
+	Hint        string
+	Target      string
+	Required    bool
+	CallBack    func(field *DataFormField) string
+}
+
+func DataForm(wrap *wrapper.Wrapper, data []DataFormField) string {
+	result := `<form class="data-form" action="/cp/" method="post" autocomplete="off">`
+	result += `<div class="hidden">`
+	for _, field := range data {
+		if field.Kind == DFKHidden {
+			if field.CallBack != nil {
+				result += field.CallBack(&field)
+			} else {
+				result += `<input type="hidden" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `">`
+			}
+		}
+	}
+	result += `</div>`
+	for _, field := range data {
+		if field.Kind != DFKHidden && field.Kind != DFKSubmit {
+			if field.CallBack != nil {
+				result += field.CallBack(&field)
+			} else {
+				required := ``
+				if field.Required {
+					required = ` required`
+				}
+				result += `<div class="form-group">`
+				result += `<div class="row">`
+				result += `<div class="col-3">`
+				result += `<label for="lbl_` + field.Name + `">` + field.Caption + `</label>`
+				result += `</div>`
+				result += `<div class="col-9">`
+				result += `<div>`
+				if field.Kind == DFKText {
+					result += `<input class="form-control" type="text" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
+				} else if field.Kind == DFKEmail {
+					result += `<input class="form-control" type="email" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
+				} else if field.Kind == DFKPassword {
+					result += `<input class="form-control" type="password" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
+				} else if field.Kind == DFKTextArea {
+					result += `<textarea class="form-control" id="lbl_` + field.Name + `" name="` + field.Name + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>` + html.EscapeString(field.Value) + `</textarea>`
+				}
+				result += `</div>`
+				if field.Hint != "" {
+					result += `<div><small>` + field.Hint + `</small></div>`
+				}
+				result += `</div>`
+				result += `</div>`
+				result += `</div>`
+			}
+		}
+	}
+	for _, field := range data {
+		if field.Kind == DFKSubmit {
+			if field.CallBack != nil {
+				result += field.CallBack(&field)
+			} else {
+				result += `<div class="row hidden">`
+				result += `<div class="col-3">`
+				result += `&nbsp;`
+				result += `</div>`
+				result += `<div class="col-9">`
+				result += `<button type="submit" class="btn btn-primary" data-target="` + field.Target + `">` + html.EscapeString(field.Value) + `</button>`
+				result += `</div>`
+				result += `</div>`
+			}
+		}
+	}
+	result += `</form>`
+	return result
+}

+ 44 - 2
modules/module_users.go

@@ -19,7 +19,7 @@ func (this *Modules) RegisterModule_Users() *Module {
 		Icon:   assets.SysSvgIconUser,
 		Sub: &[]MISub{
 			{Mount: "default", Name: "List of Users", Icon: assets.SysSvgIconList},
-			{Mount: "add", Name: "Add New User", Icon: assets.SysSvgIconPlus},
+			{Mount: "modify", Name: "Add New User", Icon: assets.SysSvgIconPlus},
 		},
 	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
 		content := ""
@@ -58,10 +58,52 @@ func (this *Modules) RegisterModule_Users() *Module {
 					assets.SysSvgIconEdit + `</a>` +
 					`<a class="ico" href="#">` + assets.SysSvgIconRemove + `</a>`
 			}, "/cp/users/")
-		} else if wrap.CurrSubModule == "add" {
+		} else if wrap.CurrSubModule == "modify" {
 			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
 				{Name: "Add New User"},
 			})
+			content += builder.DataForm(wrap, []builder.DataFormField{
+				{
+					Kind:  builder.DFKHidden,
+					Name:  "action",
+					Value: "users_edit",
+				},
+				{
+					Kind:  builder.DFKHidden,
+					Name:  "id",
+					Value: "0",
+				},
+				{
+					Kind:    builder.DFKText,
+					Caption: "User first name",
+					Name:    "first_name",
+					Value:   "1",
+				},
+				{
+					Kind:    builder.DFKText,
+					Caption: "User last name",
+					Name:    "last_name",
+					Value:   "2",
+				},
+				{
+					Kind:     builder.DFKEmail,
+					Caption:  "User email",
+					Name:     "email",
+					Value:    "3",
+					Required: true,
+				},
+				{
+					Kind:    builder.DFKPassword,
+					Caption: "User password",
+					Name:    "password",
+					Hint:    "Leave the field blank to not change the password",
+				},
+				{
+					Kind:   builder.DFKSubmit,
+					Value:  "Add",
+					Target: "add-edit-button",
+				},
+			})
 		}
 		return this.getSidebarModules(wrap), content, sidebar
 	})