detail-info.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package vuetifyx
  2. import (
  3. "context"
  4. "fmt"
  5. v "github.com/qor5/ui/vuetify"
  6. h "github.com/theplant/htmlgo"
  7. )
  8. type DetailInfoBuilder struct {
  9. columns []h.HTMLComponent
  10. classes []string
  11. }
  12. func DetailInfo(columns ...h.HTMLComponent) (r *DetailInfoBuilder) {
  13. r = &DetailInfoBuilder{}
  14. r.columns = columns
  15. return
  16. }
  17. func (b *DetailInfoBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  18. row := v.VRow()
  19. for _, col := range b.columns {
  20. row.AppendChildren(v.VCol(col).Md(true))
  21. }
  22. return v.VContainer(row).Class(b.classes...).MarshalHTML(ctx)
  23. }
  24. func (b *DetailInfoBuilder) Class(v ...string) (r *DetailInfoBuilder) {
  25. b.classes = v
  26. return b
  27. }
  28. type DetailFieldBuilder struct {
  29. label string
  30. labelMinWidth string
  31. icon h.HTMLComponent
  32. children []h.HTMLComponent
  33. }
  34. func DetailField(children ...h.HTMLComponent) (r *DetailFieldBuilder) {
  35. r = &DetailFieldBuilder{
  36. labelMinWidth: "180px",
  37. }
  38. r.Children(children...)
  39. return
  40. }
  41. func (b *DetailFieldBuilder) Children(comps ...h.HTMLComponent) (r *DetailFieldBuilder) {
  42. b.children = comps
  43. return b
  44. }
  45. func (b *DetailFieldBuilder) Label(v string) (r *DetailFieldBuilder) {
  46. b.label = v
  47. return b
  48. }
  49. func (b *DetailFieldBuilder) LabelMinWidth(v string) (r *DetailFieldBuilder) {
  50. b.labelMinWidth = v
  51. return b
  52. }
  53. func (b *DetailFieldBuilder) Icon(v h.HTMLComponent) (r *DetailFieldBuilder) {
  54. b.icon = v
  55. return b
  56. }
  57. func (b *DetailFieldBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  58. ki := h.Tag("div").Children(
  59. h.Tag("label").Text(b.label).Class("blue-grey--text lighten-3").Style(fmt.Sprintf("min-width: %s", b.labelMinWidth)),
  60. ).Class("d-flex pb-2")
  61. if b.icon != nil {
  62. ki.AppendChildren(b.icon)
  63. }
  64. ki.AppendChildren(b.children...)
  65. return ki.MarshalHTML(ctx)
  66. }
  67. type DetailColumnBuilder struct {
  68. key string
  69. children []h.HTMLComponent
  70. header string
  71. }
  72. func DetailColumn(children ...h.HTMLComponent) (r *DetailColumnBuilder) {
  73. r = &DetailColumnBuilder{}
  74. r.Children(children...)
  75. return
  76. }
  77. func (b *DetailColumnBuilder) Children(comps ...h.HTMLComponent) (r *DetailColumnBuilder) {
  78. b.children = comps
  79. return b
  80. }
  81. func (b *DetailColumnBuilder) Header(text string) (r *DetailColumnBuilder) {
  82. b.header = text
  83. return b
  84. }
  85. func (b *DetailColumnBuilder) Append(label string, comp h.HTMLComponent) (r *DetailColumnBuilder) {
  86. b.AppendIcon(label, nil, comp)
  87. return b
  88. }
  89. func (b *DetailColumnBuilder) AppendIcon(label string, icon h.HTMLComponent, comp h.HTMLComponent) (r *DetailColumnBuilder) {
  90. b.children = append(b.children, DetailField(comp).Label(label).Icon(icon))
  91. return b
  92. }
  93. func (b *DetailColumnBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  94. detailInfoBody := h.Tag("div")
  95. if len(b.header) > 0 {
  96. detailInfoBody.AppendChildren(
  97. h.Tag("h5").Text(b.header).Class("subtitle-2 pb-2"),
  98. )
  99. }
  100. detailInfoBody.AppendChildren(b.children...)
  101. return detailInfoBody.MarshalHTML(ctx)
  102. }
  103. type OptionalTextBuilder struct {
  104. text string
  105. zeroLabel string
  106. }
  107. func OptionalText(text string) (r *OptionalTextBuilder) {
  108. r = &OptionalTextBuilder{text: text}
  109. return
  110. }
  111. func (b *OptionalTextBuilder) ZeroLabel(label string) (r *OptionalTextBuilder) {
  112. b.zeroLabel = label
  113. return b
  114. }
  115. func (b *OptionalTextBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  116. var body h.HTMLComponent
  117. if len(b.text) > 0 {
  118. body = h.Tag("span").Text(b.text)
  119. } else {
  120. body = h.Tag("span").Class("grey--text lighten-5").Text(b.zeroLabel)
  121. }
  122. return body.MarshalHTML(ctx)
  123. }