data_form.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package builder
  2. import (
  3. "html"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  6. )
  7. const (
  8. DFKHidden = iota
  9. DFKText
  10. DFKNumber
  11. DFKEmail
  12. DFKPassword
  13. DFKTextArea
  14. DFKCheckBox
  15. DFKSubmit
  16. DFKMessage
  17. )
  18. type DataFormField struct {
  19. Caption string
  20. Kind int
  21. Name string
  22. Value string
  23. Placeholder string
  24. Hint string
  25. Target string
  26. Required bool
  27. Classes string
  28. Min string
  29. Max string
  30. CallBack func(field *DataFormField) string
  31. }
  32. func DataForm(wrap *wrapper.Wrapper, data []DataFormField) string {
  33. var html_hidden string
  34. var html_element string
  35. var html_message string
  36. var html_button string
  37. for i, field := range data {
  38. if field.Kind == DFKHidden {
  39. if field.CallBack != nil {
  40. html_hidden += field.CallBack(&field)
  41. } else {
  42. html_hidden += `<input type="hidden" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `">`
  43. }
  44. } else if field.Kind != DFKHidden && field.Kind != DFKSubmit && field.Kind != DFKMessage {
  45. if field.CallBack != nil {
  46. html_element += field.CallBack(&field)
  47. } else {
  48. required := ``
  49. if field.Required {
  50. required = ` required`
  51. }
  52. classes := field.Classes
  53. if classes != "" {
  54. classes = " " + classes
  55. }
  56. html_element += `<div class="form-group n` + utils.IntToStr(i) + `">`
  57. html_element += `<div class="row">`
  58. html_element += `<div class="col-md-3">`
  59. if field.Kind != DFKCheckBox {
  60. html_element += `<label for="lbl_` + field.Name + `">` + field.Caption + `</label>`
  61. } else {
  62. html_element += `<label>` + field.Caption + `</label>`
  63. }
  64. html_element += `</div>`
  65. html_element += `<div class="col-md-9">`
  66. html_element += `<div>`
  67. if field.Kind == DFKText {
  68. html_element += `<input class="form-control` + classes + `" type="text" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" `
  69. if field.Min != "" {
  70. html_element += `minlength="` + field.Min + `" `
  71. }
  72. if field.Max != "" {
  73. html_element += `maxlength="` + field.Max + `" `
  74. }
  75. html_element += `placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
  76. } else if field.Kind == DFKNumber {
  77. html_element += `<input class="form-control` + classes + `" type="number" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" `
  78. if field.Min != "" {
  79. html_element += `min="` + field.Min + `" `
  80. }
  81. if field.Max != "" {
  82. html_element += `max="` + field.Max + `" `
  83. }
  84. html_element += `placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
  85. } else if field.Kind == DFKEmail {
  86. 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 + `>`
  87. } else if field.Kind == DFKPassword {
  88. 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 + `>`
  89. } else if field.Kind == DFKTextArea {
  90. html_element += `<textarea class="form-control` + classes + `" id="lbl_` + field.Name + `" name="` + field.Name + `" `
  91. if field.Min != "" {
  92. html_element += `minlength="` + field.Min + `" `
  93. }
  94. if field.Max != "" {
  95. html_element += `maxlength="` + field.Max + `" `
  96. }
  97. html_element += `placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>` + html.EscapeString(field.Value) + `</textarea>`
  98. } else if field.Kind == DFKCheckBox {
  99. checked := ""
  100. if field.Value != "0" {
  101. checked = " checked"
  102. }
  103. 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>`
  104. }
  105. html_element += `</div>`
  106. if field.Hint != "" {
  107. html_element += `<div><small>` + field.Hint + `</small></div>`
  108. }
  109. html_element += `</div>`
  110. html_element += `</div>`
  111. html_element += `</div>`
  112. }
  113. } else if field.Kind == DFKMessage {
  114. if field.CallBack != nil {
  115. html_message += field.CallBack(&field)
  116. } else {
  117. html_message += `<div class="row">`
  118. html_message += `<div class="col-md-3">`
  119. html_message += `</div>`
  120. html_message += `<div class="col-md-9">`
  121. html_message += `<div class="sys-messages"></div>`
  122. html_message += `</div>`
  123. html_message += `</div>`
  124. }
  125. } else if field.Kind == DFKSubmit {
  126. if field.CallBack != nil {
  127. html_button += field.CallBack(&field)
  128. } else {
  129. html_button += `<div class="row d-lg-none">`
  130. html_button += `<div class="col-md-3 d-none d-md-block">`
  131. html_button += `&nbsp;`
  132. html_button += `</div>`
  133. html_button += `<div class="col-md-9">`
  134. html_button += `<div class="pt-3"><button type="submit" class="btn btn-primary" data-target="` + field.Target + `">` + html.EscapeString(field.Value) + `</button></div>`
  135. html_button += `</div>`
  136. html_button += `</div>`
  137. }
  138. }
  139. }
  140. if html_hidden != "" {
  141. html_hidden = `<div class="hidden">` + html_hidden + `</div>`
  142. }
  143. return `<form class="data-form ` + wrap.CurrModule + `-` + wrap.CurrSubModule + ` prev-data-lost" action="/cp/" method="post" autocomplete="off">` +
  144. html_hidden + html_element + html_message + html_button + `</form>`
  145. }