list_content_lite.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/pagebuilder"
  10. "github.com/qor5/admin/presets"
  11. "github.com/qor5/admin/richeditor"
  12. v "github.com/qor5/ui/vuetify"
  13. "github.com/qor5/web"
  14. . "github.com/theplant/htmlgo"
  15. "gorm.io/gorm"
  16. )
  17. type ListContentLite struct {
  18. ID uint
  19. AddTopSpace bool
  20. AddBottomSpace bool
  21. AnchorID string
  22. Items ListItemLites
  23. BackgroundColor string
  24. }
  25. type ListItemLites []*ListItemLite
  26. type ListItemLite struct {
  27. Heading string
  28. Text string
  29. }
  30. func (this ListItemLites) Value() (driver.Value, error) {
  31. return json.Marshal(this)
  32. }
  33. func (this *ListItemLites) Scan(value interface{}) error {
  34. switch v := value.(type) {
  35. case string:
  36. return json.Unmarshal([]byte(v), this)
  37. case []byte:
  38. return json.Unmarshal(v, this)
  39. default:
  40. return errors.New("not supported")
  41. }
  42. }
  43. func (*ListContentLite) TableName() string {
  44. return "container_list_content_lite"
  45. }
  46. func RegisterListContentLiteContainer(pb *pagebuilder.Builder, db *gorm.DB) {
  47. vb := pb.RegisterContainer("ListContentLite").
  48. RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
  49. v := obj.(*ListContentLite)
  50. return ListContentLiteBody(v, input)
  51. })
  52. mb := vb.Model(&ListContentLite{})
  53. eb := mb.Editing(
  54. "AddTopSpace", "AddBottomSpace", "AnchorID",
  55. "Items", "BackgroundColor",
  56. )
  57. eb.Field("BackgroundColor").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  58. return v.VAutocomplete().FieldName(field.Name).
  59. Label(field.Label).Value(field.Value(obj)).
  60. Items([]string{White, Grey})
  61. })
  62. fb := pb.GetPresetsBuilder().NewFieldsBuilder(presets.WRITE).Model(&ListItemLite{}).Only("Heading", "Text")
  63. fb.Field("Text").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  64. return richeditor.RichEditor(db, field.FormKey).
  65. Plugins([]string{"alignment", "video", "imageinsert", "fontcolor"}).
  66. Value(obj.(*ListItemLite).Text).Label(field.Label)
  67. })
  68. eb.Field("Items").Nested(fb, &presets.DisplayFieldInSorter{Field: "Heading"})
  69. }
  70. func ListContentLiteBody(data *ListContentLite, input *pagebuilder.RenderInput) (body HTMLComponent) {
  71. body = ContainerWrapper(
  72. fmt.Sprintf(inflection.Plural(strcase.ToKebab("ListContentLite"))+"_%v", data.ID), data.AnchorID, "container-list_content_lite",
  73. data.BackgroundColor, "", "",
  74. "", data.AddTopSpace, data.AddBottomSpace, input.IsEditor, input.IsReadonly, "",
  75. Div(LiteItemsBody(data.Items)).Class("container-wrapper"),
  76. )
  77. return
  78. }
  79. func LiteItemsBody(items []*ListItemLite) HTMLComponent {
  80. itemsDiv := Div().Class("container-list_content_lite-grid")
  81. for _, i := range items {
  82. itemsDiv.AppendChildren(
  83. Div(
  84. H3(i.Heading).Class("container-list_content_lite-heading"),
  85. Div(
  86. RawHTML(i.Text),
  87. ).Class("container-list_content_lite-text"),
  88. ).Class("container-list_content_lite-item"),
  89. )
  90. }
  91. return itemsDiv
  92. }