status.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package views
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sync"
  6. "github.com/qor5/admin/presets"
  7. "github.com/qor5/admin/publish"
  8. "github.com/qor5/admin/utils"
  9. . "github.com/qor5/ui/vuetify"
  10. "github.com/qor5/web"
  11. "github.com/qor5/x/i18n"
  12. h "github.com/theplant/htmlgo"
  13. "gorm.io/gorm"
  14. "gorm.io/gorm/schema"
  15. )
  16. func draftCountFunc(db *gorm.DB) presets.FieldComponentFunc {
  17. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  18. var count int64
  19. modelSchema, err := schema.Parse(obj, &sync.Map{}, db.NamingStrategy)
  20. if err != nil {
  21. return h.Td(h.Text("0"))
  22. }
  23. publish.SetPrimaryKeysConditionWithoutVersion(db.Model((reflect.New(modelSchema.ModelType).Interface())), obj, modelSchema).
  24. Where("status = ?", publish.StatusDraft).Count(&count)
  25. return h.Td(h.Text(fmt.Sprint(count)))
  26. }
  27. }
  28. func onlineFunc(db *gorm.DB) presets.FieldComponentFunc {
  29. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  30. var count int64
  31. modelSchema, err := schema.Parse(obj, &sync.Map{}, db.NamingStrategy)
  32. if err != nil {
  33. return h.Td(h.Text("0"))
  34. }
  35. publish.SetPrimaryKeysConditionWithoutVersion(db.Model((reflect.New(modelSchema.ModelType).Interface())), obj, modelSchema).
  36. Where("status = ?", publish.StatusOnline).Count(&count)
  37. c := h.Text("-")
  38. if count > 0 {
  39. c = VBadge().Color("green")
  40. }
  41. return h.Td(c)
  42. }
  43. }
  44. func StatusListFunc() presets.FieldComponentFunc {
  45. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  46. msgr := i18n.MustGetModuleMessages(ctx.R, I18nPublishKey, Messages_en_US).(*Messages)
  47. if s, ok := obj.(publish.StatusInterface); ok {
  48. return h.Td(VChip(h.Text(GetStatusText(s.GetStatus(), msgr))).Color(GetStatusColor(s.GetStatus())).Dark(true))
  49. }
  50. return nil
  51. }
  52. }
  53. func StatusEditFunc() presets.FieldComponentFunc {
  54. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  55. s, ok := obj.(publish.StatusInterface)
  56. if !ok || s.GetStatus() == "" {
  57. return nil
  58. }
  59. msgr := i18n.MustGetModuleMessages(ctx.R, I18nPublishKey, Messages_en_US).(*Messages)
  60. utilsMsgr := i18n.MustGetModuleMessages(ctx.R, utils.I18nUtilsKey, utils.Messages_en_US).(*utils.Messages)
  61. var btn h.HTMLComponent
  62. switch s.GetStatus() {
  63. case publish.StatusDraft, publish.StatusOffline:
  64. btn = h.Div(
  65. VBtn(msgr.Publish).Attr("@click", fmt.Sprintf(`locals.action="%s";locals.commonConfirmDialog = true`, PublishEvent)),
  66. )
  67. case publish.StatusOnline:
  68. btn = h.Div(
  69. VBtn(msgr.Unpublish).Attr("@click", fmt.Sprintf(`locals.action="%s";locals.commonConfirmDialog = true`, UnpublishEvent)),
  70. VBtn(msgr.Republish).Attr("@click", fmt.Sprintf(`locals.action="%s";locals.commonConfirmDialog = true`, RepublishEvent)),
  71. )
  72. }
  73. paramID := obj.(presets.SlugEncoder).PrimarySlug()
  74. return web.Scope(
  75. VStepper(
  76. VStepperHeader(
  77. VStepperStep(h.Text(msgr.StatusDraft)).Step(0).Complete(s.GetStatus() == publish.StatusDraft),
  78. VDivider(),
  79. VStepperStep(h.Text(msgr.StatusOnline)).Step(0).Complete(s.GetStatus() == publish.StatusOnline),
  80. ),
  81. ),
  82. h.Br(),
  83. btn,
  84. h.Br(),
  85. utils.ConfirmDialog(msgr.Areyousure, web.Plaid().EventFunc(web.Var("locals.action")).
  86. Query(presets.ParamID, paramID).Go(),
  87. utilsMsgr),
  88. ).Init(`{ action: "", commonConfirmDialog: false}`).VSlot("{ locals }")
  89. }
  90. }
  91. func GetStatusColor(status string) string {
  92. switch status {
  93. case publish.StatusDraft:
  94. return "orange"
  95. case publish.StatusOnline:
  96. return "green"
  97. case publish.StatusOffline:
  98. return "grey"
  99. }
  100. return ""
  101. }