card.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package vuetifyx
  2. import (
  3. "context"
  4. v "github.com/qor5/ui/vuetify"
  5. h "github.com/theplant/htmlgo"
  6. )
  7. type CardBuilder struct {
  8. children []h.HTMLComponent
  9. systemBar []h.HTMLComponent
  10. header []h.HTMLComponent
  11. actions []h.HTMLComponent
  12. classNames []string
  13. outlined bool
  14. }
  15. func Card(children ...h.HTMLComponent) (r *CardBuilder) {
  16. r = &CardBuilder{}
  17. r.Children(children...)
  18. return
  19. }
  20. func (b *CardBuilder) Children(comps ...h.HTMLComponent) (r *CardBuilder) {
  21. b.children = comps
  22. return b
  23. }
  24. func (b *CardBuilder) Actions(actions ...h.HTMLComponent) (r *CardBuilder) {
  25. b.actions = actions
  26. return b
  27. }
  28. func (b *CardBuilder) Header(header ...h.HTMLComponent) (r *CardBuilder) {
  29. b.header = header
  30. return b
  31. }
  32. func (b *CardBuilder) HeaderTitle(title string) (r *CardBuilder) {
  33. b.header = []h.HTMLComponent{h.Text(title)}
  34. return b
  35. }
  36. func (b *CardBuilder) SystemBar(systemBar ...h.HTMLComponent) (r *CardBuilder) {
  37. b.systemBar = systemBar
  38. return b
  39. }
  40. func (b *CardBuilder) Class(names ...string) (r *CardBuilder) {
  41. b.classNames = names
  42. return b
  43. }
  44. func (b *CardBuilder) Outlined(v bool) (r *CardBuilder) {
  45. b.outlined = v
  46. return b
  47. }
  48. func (b *CardBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  49. var sb h.HTMLComponent
  50. var hr h.HTMLComponent
  51. if len(b.systemBar) > 0 {
  52. sb = v.VSystemBar(b.systemBar...).Class("mx-2 pt-4").Color("white").Height(32)
  53. }
  54. if len(b.children) > 0 {
  55. empty := true
  56. for _, c := range b.children {
  57. if c != nil {
  58. empty = false
  59. }
  60. }
  61. if !empty {
  62. hr = v.VDivider()
  63. }
  64. }
  65. return v.VCard(
  66. sb,
  67. v.VToolbar(
  68. v.VToolbarTitle("").Children(b.header...),
  69. v.VSpacer(),
  70. ).Flat(true).AppendChildren(b.actions...),
  71. hr,
  72. ).Outlined(b.outlined).Class(b.classNames...).AppendChildren(b.children...).MarshalHTML(ctx)
  73. }