brand_grid.go 2.5 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/media/media_library"
  10. media_view "github.com/qor5/admin/media/views"
  11. "github.com/qor5/admin/pagebuilder"
  12. "github.com/qor5/admin/presets"
  13. "github.com/qor5/web"
  14. . "github.com/theplant/htmlgo"
  15. "gorm.io/gorm"
  16. )
  17. type BrandGrid struct {
  18. ID uint
  19. AddTopSpace bool
  20. AddBottomSpace bool
  21. AnchorID string
  22. Brands Brands `sql:"type:text;"`
  23. }
  24. type Brand struct {
  25. Image media_library.MediaBox `sql:"type:text;"`
  26. Name string
  27. }
  28. func (*BrandGrid) TableName() string {
  29. return "container_brand_grids"
  30. }
  31. type Brands []*Brand
  32. func (this Brands) Value() (driver.Value, error) {
  33. return json.Marshal(this)
  34. }
  35. func (this *Brands) Scan(value interface{}) error {
  36. switch v := value.(type) {
  37. case string:
  38. return json.Unmarshal([]byte(v), this)
  39. case []byte:
  40. return json.Unmarshal(v, this)
  41. default:
  42. return errors.New("not supported")
  43. }
  44. }
  45. func RegisterBrandGridContainer(pb *pagebuilder.Builder, db *gorm.DB) {
  46. vb := pb.RegisterContainer("BrandGrid").
  47. RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
  48. v := obj.(*BrandGrid)
  49. return BrandGridBody(v, input)
  50. })
  51. mb := vb.Model(&BrandGrid{})
  52. eb := mb.Editing("AddTopSpace", "AddBottomSpace", "AnchorID", "Brands")
  53. fb := pb.GetPresetsBuilder().NewFieldsBuilder(presets.WRITE).Model(&Brand{}).Only("Image", "Name")
  54. fb.Field("Image").WithContextValue(media_view.MediaBoxConfig, &media_library.MediaBoxConfig{
  55. AllowType: "image",
  56. })
  57. eb.Field("Brands").Nested(fb, &presets.DisplayFieldInSorter{Field: "Name"})
  58. }
  59. func BrandGridBody(data *BrandGrid, input *pagebuilder.RenderInput) (body HTMLComponent) {
  60. body = ContainerWrapper(
  61. fmt.Sprintf(inflection.Plural(strcase.ToKebab("BrandGrid"))+"_%v", data.ID), data.AnchorID, "container-brand_grid",
  62. "", "", "",
  63. "", data.AddTopSpace, data.AddBottomSpace, input.IsEditor, input.IsReadonly, "",
  64. Div(
  65. BrandsBody(data.Brands, input),
  66. ).Class("container-wrapper"),
  67. )
  68. return
  69. }
  70. func BrandsBody(brands []*Brand, input *pagebuilder.RenderInput) HTMLComponent {
  71. brandsDiv := Div().Class("container-brand_grid-wrap")
  72. for _, b := range brands {
  73. img := LazyImageHtml(b.Image)
  74. if input.IsEditor {
  75. img = ImageHtml(b.Image)
  76. }
  77. brandsDiv.AppendChildren(
  78. Div(
  79. img,
  80. ).Class("container-brand_grid-item"),
  81. )
  82. }
  83. return brandsDiv
  84. }