image.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package containers
  2. import (
  3. "fmt"
  4. "github.com/iancoleman/strcase"
  5. "github.com/jinzhu/inflection"
  6. "github.com/qor5/admin/media/media_library"
  7. "github.com/qor5/admin/pagebuilder"
  8. "github.com/qor5/admin/presets"
  9. "github.com/qor5/ui/vuetify"
  10. "github.com/qor5/web"
  11. . "github.com/theplant/htmlgo"
  12. "gorm.io/gorm"
  13. )
  14. type ImageContainer struct {
  15. ID uint
  16. AddTopSpace bool
  17. AddBottomSpace bool
  18. AnchorID string
  19. Image media_library.MediaBox `sql:"type:text;"`
  20. BackgroundColor string
  21. TransitionBackgroundColor string
  22. }
  23. func (*ImageContainer) TableName() string {
  24. return "container_images"
  25. }
  26. func RegisterImageContainer(pb *pagebuilder.Builder, db *gorm.DB) {
  27. vb := pb.RegisterContainer("Image").
  28. RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
  29. v := obj.(*ImageContainer)
  30. return ImageContainerBody(v, input)
  31. })
  32. mb := vb.Model(&ImageContainer{}).URIName(inflection.Plural(strcase.ToKebab("Image")))
  33. eb := mb.Editing("AddTopSpace", "AddBottomSpace", "AnchorID", "BackgroundColor", "TransitionBackgroundColor", "Image")
  34. eb.Field("BackgroundColor").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  35. return vuetify.VSelect().
  36. Items([]string{"white", "blue", "grey"}).
  37. Value(field.Value(obj)).
  38. Label(field.Label).
  39. FieldName(field.FormKey)
  40. })
  41. eb.Field("TransitionBackgroundColor").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  42. return vuetify.VSelect().
  43. Items([]string{"white", "blue", "grey"}).
  44. Value(field.Value(obj)).
  45. Label(field.Label).
  46. FieldName(field.FormKey)
  47. })
  48. }
  49. func ImageContainerBody(data *ImageContainer, input *pagebuilder.RenderInput) (body HTMLComponent) {
  50. body = ContainerWrapper(
  51. fmt.Sprintf(inflection.Plural(strcase.ToKebab("Image"))+"_%v", data.ID), data.AnchorID, "container-image",
  52. data.BackgroundColor, data.TransitionBackgroundColor, "",
  53. "", data.AddTopSpace, data.AddBottomSpace, input.IsEditor, input.IsReadonly, "",
  54. Div(
  55. ImageHtml(data.Image),
  56. Div().Class("container-image-corner"),
  57. ).Class("container-wrapper"),
  58. )
  59. return
  60. }