config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package note
  2. import (
  3. "fmt"
  4. "github.com/qor5/admin/presets"
  5. "github.com/qor5/admin/presets/actions"
  6. . "github.com/qor5/ui/vuetify"
  7. "github.com/qor5/web"
  8. "github.com/qor5/x/i18n"
  9. "github.com/sunfmin/reflectutils"
  10. h "github.com/theplant/htmlgo"
  11. "golang.org/x/text/language"
  12. "gorm.io/gorm"
  13. )
  14. const (
  15. I18nNoteKey i18n.ModuleKey = "I18nNoteKey"
  16. createNoteEvent = "note_CreateNoteEvent"
  17. updateUserNoteEvent = "note_UpdateUserNoteEvent"
  18. )
  19. func Configure(db *gorm.DB, pb *presets.Builder, models ...*presets.ModelBuilder) {
  20. if err := db.AutoMigrate(QorNote{}, UserNote{}); err != nil {
  21. panic(err)
  22. }
  23. for _, m := range models {
  24. if m.Info().HasDetailing() {
  25. m.Detailing().AppendTabsPanelFunc(tabsPanel(db, m))
  26. }
  27. m.Editing().AppendTabsPanelFunc(tabsPanel(db, m))
  28. m.RegisterEventFunc(createNoteEvent, createNoteAction(db, m))
  29. m.RegisterEventFunc(updateUserNoteEvent, updateUserNoteAction(db, m))
  30. m.Listing().Field("Notes").ComponentFunc(noteFunc(db, m))
  31. }
  32. pb.I18n().
  33. RegisterForModule(language.English, I18nNoteKey, Messages_en_US).
  34. RegisterForModule(language.SimplifiedChinese, I18nNoteKey, Messages_zh_CN).
  35. RegisterForModule(language.Japanese, I18nNoteKey, Messages_ja_JP)
  36. }
  37. func tabsPanel(db *gorm.DB, mb *presets.ModelBuilder) presets.ObjectComponentFunc {
  38. return func(obj interface{}, ctx *web.EventContext) (c h.HTMLComponent) {
  39. id := ctx.R.FormValue(presets.ParamID)
  40. if len(id) == 0 {
  41. return
  42. }
  43. tn := mb.Info().Label()
  44. notesSection := getNotesTab(ctx, db, tn, id)
  45. msgr := i18n.MustGetModuleMessages(ctx.R, I18nNoteKey, Messages_en_US).(*Messages)
  46. userID, _ := GetUserData(ctx)
  47. count := GetUnreadNotesCount(db, userID, tn, id)
  48. notesTab := VBadge(h.Text(msgr.Notes)).
  49. Attr(":content", "locals.unreadNotesCount").
  50. Attr(":value", "locals.unreadNotesCount").
  51. Color("red")
  52. clickEvent := web.Plaid().
  53. BeforeScript("locals.unreadNotesCount=0").
  54. EventFunc(updateUserNoteEvent).
  55. Query("resource_id", id).
  56. Query("resource_type", tn).
  57. Go() + ";" + web.Plaid().
  58. EventFunc(actions.ReloadList).
  59. Go()
  60. if count == 0 {
  61. clickEvent = ""
  62. }
  63. c = h.Components(
  64. VTab(notesTab).
  65. Attr(web.InitContextLocals, fmt.Sprintf("{unreadNotesCount:%v}", count)).
  66. Attr("@click", clickEvent),
  67. VTabItem(web.Portal().Name("notes-section").Children(notesSection)),
  68. )
  69. return
  70. }
  71. }
  72. func noteFunc(db *gorm.DB, mb *presets.ModelBuilder) presets.FieldComponentFunc {
  73. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (c h.HTMLComponent) {
  74. tn := mb.Info().Label()
  75. id := fmt.Sprint(reflectutils.MustGet(obj, "ID"))
  76. if ps, ok := obj.(interface {
  77. PrimarySlug() string
  78. }); ok {
  79. id = ps.PrimarySlug()
  80. }
  81. latestNote := QorNote{}
  82. db.Model(&QorNote{}).Where("resource_type = ? AND resource_id = ?", tn, id).Order("created_at DESC").First(&latestNote)
  83. content := []rune(latestNote.Content)
  84. result := string(content[:])
  85. if len(content) > 60 {
  86. result = string(content[0:60]) + "..."
  87. }
  88. userID, _ := GetUserData(ctx)
  89. count := GetUnreadNotesCount(db, userID, tn, id)
  90. return h.Td(
  91. h.If(count > 0,
  92. VBadge(h.Text(result)).Content(count).Color("red"),
  93. ).Else(
  94. h.Text(fmt.Sprint(result)),
  95. ),
  96. )
  97. }
  98. }