data_form.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package builder
  2. import (
  3. "html"
  4. "golang-fave/engine/wrapper"
  5. )
  6. const (
  7. DFKHidden = iota
  8. DFKText
  9. DFKEmail
  10. DFKPassword
  11. DFKTextArea
  12. DFKCheckBox
  13. DFKSubmit
  14. DFKMessage
  15. )
  16. type DataFormField struct {
  17. Caption string
  18. Kind int
  19. Name string
  20. Value string
  21. Placeholder string
  22. Hint string
  23. Target string
  24. Required bool
  25. CallBack func(field *DataFormField) string
  26. }
  27. func DataForm(wrap *wrapper.Wrapper, data []DataFormField) string {
  28. var html_hidden string
  29. var html_element string
  30. var html_message string
  31. var html_button string
  32. for _, field := range data {
  33. if field.Kind == DFKHidden {
  34. if field.CallBack != nil {
  35. html_hidden += field.CallBack(&field)
  36. } else {
  37. html_hidden += `<input type="hidden" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `">`
  38. }
  39. } else if field.Kind != DFKHidden && field.Kind != DFKSubmit && field.Kind != DFKMessage {
  40. if field.CallBack != nil {
  41. html_element += field.CallBack(&field)
  42. } else {
  43. required := ``
  44. if field.Required {
  45. required = ` required`
  46. }
  47. html_element += `<div class="form-group">`
  48. html_element += `<div class="row">`
  49. html_element += `<div class="col-md-3">`
  50. if field.Kind != DFKCheckBox {
  51. html_element += `<label for="lbl_` + field.Name + `">` + field.Caption + `</label>`
  52. } else {
  53. html_element += `<label>` + field.Caption + `</label>`
  54. }
  55. html_element += `</div>`
  56. html_element += `<div class="col-md-9">`
  57. html_element += `<div>`
  58. if field.Kind == DFKText {
  59. html_element += `<input class="form-control" type="text" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
  60. } else if field.Kind == DFKEmail {
  61. html_element += `<input class="form-control" type="email" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
  62. } else if field.Kind == DFKPassword {
  63. html_element += `<input class="form-control" type="password" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
  64. } else if field.Kind == DFKTextArea {
  65. html_element += `<textarea class="form-control" id="lbl_` + field.Name + `" name="` + field.Name + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>` + html.EscapeString(field.Value) + `</textarea>`
  66. } else if field.Kind == DFKCheckBox {
  67. checked := ""
  68. if field.Value != "0" {
  69. checked = " checked"
  70. }
  71. html_element += `<div class="checkbox-ios"><input class="form-control" type="checkbox" id="lbl_` + field.Name + `" name="` + field.Name + `" value="1"` + `" autocomplete="off"` + required + checked + `><label for="lbl_` + field.Name + `"></label></div>`
  72. }
  73. html_element += `</div>`
  74. if field.Hint != "" {
  75. html_element += `<div><small>` + field.Hint + `</small></div>`
  76. }
  77. html_element += `</div>`
  78. html_element += `</div>`
  79. html_element += `</div>`
  80. }
  81. } else if field.Kind == DFKMessage {
  82. if field.CallBack != nil {
  83. html_message += field.CallBack(&field)
  84. } else {
  85. html_message += `<div class="row">`
  86. html_message += `<div class="col-md-3">`
  87. html_message += `</div>`
  88. html_message += `<div class="col-md-9">`
  89. html_message += `<div class="sys-messages"></div>`
  90. html_message += `</div>`
  91. html_message += `</div>`
  92. }
  93. } else if field.Kind == DFKSubmit {
  94. if field.CallBack != nil {
  95. html_button += field.CallBack(&field)
  96. } else {
  97. html_button += `<div class="row d-lg-none">`
  98. html_button += `<div class="col-md-3 d-none d-md-block">`
  99. html_button += `&nbsp;`
  100. html_button += `</div>`
  101. html_button += `<div class="col-md-9">`
  102. html_button += `<button type="submit" class="btn btn-primary" data-target="` + field.Target + `">` + html.EscapeString(field.Value) + `</button>`
  103. html_button += `</div>`
  104. html_button += `</div>`
  105. }
  106. }
  107. }
  108. if html_hidden != "" {
  109. html_hidden = `<div class="hidden">` + html_hidden + `</div>`
  110. }
  111. return `<form class="data-form prev-data-lost" action="/cp/" method="post" autocomplete="off">` +
  112. html_hidden + html_element + html_message + html_button + `</form>`
  113. }