heading.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package containers
  2. import (
  3. "fmt"
  4. "github.com/iancoleman/strcase"
  5. "github.com/jinzhu/inflection"
  6. "github.com/qor5/admin/pagebuilder"
  7. "github.com/qor5/admin/presets"
  8. "github.com/qor5/admin/richeditor"
  9. "github.com/qor5/ui/vuetify"
  10. "github.com/qor5/web"
  11. . "github.com/theplant/htmlgo"
  12. "gorm.io/gorm"
  13. )
  14. const (
  15. LINK_DISPLAY_OPTION_DESKTOP = "desktop"
  16. LINK_DISPLAY_OPTION_MOBILE = "mobile"
  17. LINK_DISPLAY_OPTION_ALL = "all"
  18. )
  19. var LinkDisplayOptions = []string{LINK_DISPLAY_OPTION_ALL, LINK_DISPLAY_OPTION_DESKTOP, LINK_DISPLAY_OPTION_MOBILE}
  20. type Heading struct {
  21. ID uint
  22. AddTopSpace bool
  23. AddBottomSpace bool
  24. AnchorID string
  25. Heading string
  26. FontColor string
  27. BackgroundColor string
  28. Link string
  29. LinkText string
  30. LinkDisplayOption string
  31. Text string
  32. }
  33. func (*Heading) TableName() string {
  34. return "container_headings"
  35. }
  36. func RegisterHeadingContainer(pb *pagebuilder.Builder, db *gorm.DB) {
  37. vb := pb.RegisterContainer("Heading").
  38. RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
  39. v := obj.(*Heading)
  40. return HeadingBody(v, input)
  41. })
  42. ed := vb.Model(&Heading{}).Editing("AddTopSpace", "AddBottomSpace", "AnchorID", "Heading", "FontColor", "BackgroundColor", "Link", "LinkText", "LinkDisplayOption", "Text")
  43. ed.Field("Text").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  44. return richeditor.RichEditor(db, "Text").Plugins([]string{"alignment", "video", "imageinsert", "fontcolor"}).Value(obj.(*Heading).Text).Label(field.Label)
  45. })
  46. ed.Field("FontColor").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  47. return vuetify.VSelect().
  48. Items(FontColors).
  49. Value(field.Value(obj)).
  50. Label(field.Label).
  51. FieldName(field.FormKey)
  52. })
  53. ed.Field("BackgroundColor").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  54. return vuetify.VSelect().
  55. Items(BackgroundColors).
  56. Value(field.Value(obj)).
  57. Label(field.Label).
  58. FieldName(field.FormKey)
  59. })
  60. ed.Field("LinkDisplayOption").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  61. return vuetify.VSelect().
  62. Items(LinkDisplayOptions).
  63. Value(field.Value(obj)).
  64. Label(field.Label).
  65. FieldName(field.FormKey)
  66. })
  67. }
  68. func HeadingBody(data *Heading, input *pagebuilder.RenderInput) (body HTMLComponent) {
  69. headingBody :=
  70. Div(
  71. Div(
  72. If(data.Heading != "",
  73. If(data.Link != "",
  74. A(H2(data.Heading).Class("container-heading-title")).Class("container-heading-title-link").Href(data.Link),
  75. ),
  76. If(data.Link == "",
  77. H2(data.Heading).Class("container-heading-title"),
  78. ),
  79. ),
  80. If(data.Text != "", Div(RawHTML(data.Text)).Class("container-heading-content")),
  81. ).Class("container-heading-wrap"),
  82. If(data.LinkText != "" && data.Link != "",
  83. Div(
  84. LinkTextWithArrow(data.LinkText, data.Link),
  85. ).Class("container-heading-link").Attr("data-display", data.LinkDisplayOption),
  86. ),
  87. ).Class("container-heading-inner")
  88. body = ContainerWrapper(
  89. fmt.Sprintf(inflection.Plural(strcase.ToKebab("Heading"))+"_%v", data.ID), data.AnchorID, "container-heading", data.BackgroundColor, "", data.FontColor,
  90. "", data.AddTopSpace, data.AddBottomSpace, input.IsEditor, input.IsReadonly, "",
  91. Div(headingBody).Class("container-wrapper"),
  92. )
  93. return
  94. }