data_form.go 4.6 KB

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