in_numbers.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package containers
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/iancoleman/strcase"
  8. "github.com/jinzhu/inflection"
  9. "github.com/qor5/admin/presets"
  10. "github.com/qor5/web"
  11. "github.com/qor5/admin/pagebuilder"
  12. . "github.com/theplant/htmlgo"
  13. "gorm.io/gorm"
  14. )
  15. type InNumbers struct {
  16. ID uint
  17. AddTopSpace bool
  18. AddBottomSpace bool
  19. AnchorID string
  20. Heading string
  21. Items InNumbersItems
  22. }
  23. type InNumbersItem struct {
  24. Heading string
  25. Text string
  26. }
  27. func (*InNumbers) TableName() string {
  28. return "container_in_numbers"
  29. }
  30. type InNumbersItems []*InNumbersItem
  31. func (this InNumbersItems) Value() (driver.Value, error) {
  32. return json.Marshal(this)
  33. }
  34. func (this *InNumbersItems) Scan(value interface{}) error {
  35. switch v := value.(type) {
  36. case string:
  37. return json.Unmarshal([]byte(v), this)
  38. case []byte:
  39. return json.Unmarshal(v, this)
  40. default:
  41. return errors.New("not supported")
  42. }
  43. }
  44. func RegisterInNumbersContainer(pb *pagebuilder.Builder, db *gorm.DB) {
  45. vb := pb.RegisterContainer("InNumbers").
  46. RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
  47. v := obj.(*InNumbers)
  48. return InNumbersBody(v, input)
  49. })
  50. mb := vb.Model(&InNumbers{})
  51. eb := mb.Editing("AddTopSpace", "AddBottomSpace", "AnchorID", "Heading", "Items")
  52. fb := pb.GetPresetsBuilder().NewFieldsBuilder(presets.WRITE).Model(&InNumbersItem{}).Only("Heading", "Text")
  53. eb.Field("Items").Nested(fb, &presets.DisplayFieldInSorter{Field: "Heading"})
  54. }
  55. func InNumbersBody(data *InNumbers, input *pagebuilder.RenderInput) (body HTMLComponent) {
  56. body = ContainerWrapper(
  57. fmt.Sprintf(inflection.Plural(strcase.ToKebab("InNumbers"))+"_%v", data.ID), data.AnchorID, "container-in_numbers container-corner",
  58. "", "", "",
  59. "", data.AddTopSpace, data.AddBottomSpace, input.IsEditor, input.IsReadonly, "",
  60. Div(
  61. H2(data.Heading).Class("container-in_numbers-heading"),
  62. InNumbersItemsBody(data.Items),
  63. ).Class("container-wrapper"),
  64. )
  65. return
  66. }
  67. func InNumbersItemsBody(items []*InNumbersItem) HTMLComponent {
  68. inNumbersItemsDiv := Div().Class("container-in_numbers-grid")
  69. for _, i := range items {
  70. inNumbersItemsDiv.AppendChildren(
  71. Div(
  72. Div(
  73. H2(i.Heading).Class("container-in_numbers-item-title"),
  74. Div(Text(i.Text)).Class("container-in_numbers-item-description"),
  75. ).Class("container-in_numbers-inner"),
  76. ).Class("container-in_numbers-item"),
  77. )
  78. }
  79. return inNumbersItemsDiv
  80. }