helper.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/ui/vuetify"
  8. "github.com/qor5/web"
  9. "github.com/qor5/x/i18n"
  10. h "github.com/theplant/htmlgo"
  11. "gorm.io/gorm"
  12. )
  13. type contextUserIDKey int
  14. const (
  15. UserIDKey contextUserIDKey = iota
  16. UserKey
  17. )
  18. func GetUserData(ctx *web.EventContext) (userID uint, creator string) {
  19. if ctx.R.Context().Value(UserIDKey) != nil {
  20. userID = ctx.R.Context().Value(UserIDKey).(uint)
  21. }
  22. if ctx.R.Context().Value(UserKey) != nil {
  23. creator = ctx.R.Context().Value(UserKey).(string)
  24. }
  25. return
  26. }
  27. func getNotesTab(ctx *web.EventContext, db *gorm.DB, resourceType string, resourceId string) h.HTMLComponent {
  28. msgr := i18n.MustGetModuleMessages(ctx.R, I18nNoteKey, Messages_en_US).(*Messages)
  29. c := h.Div(
  30. web.Scope(
  31. VCardText(
  32. h.Text(msgr.NewNote),
  33. VRow(VCol(VTextField().Attr(web.VFieldName("Content")...).Clearable(true))),
  34. ),
  35. VCardActions(h.Components(
  36. VSpacer(),
  37. VBtn(presets.MustGetMessages(ctx.R).Create).
  38. Color("primary").
  39. Attr("@click", web.Plaid().
  40. EventFunc(createNoteEvent).
  41. Query("resource_id", resourceId).
  42. Query("resource_type", resourceType).
  43. Go()+";"+web.Plaid().
  44. EventFunc(actions.ReloadList).
  45. Go(),
  46. ),
  47. )),
  48. ).VSlot("{plaidForm}"),
  49. )
  50. var notes []QorNote
  51. db.Where("resource_type = ? and resource_id = ?", resourceType, resourceId).
  52. Order("id DESC").Find(&notes)
  53. var panels []h.HTMLComponent
  54. for _, note := range notes {
  55. panels = append(panels, vuetify.VExpansionPanel(
  56. vuetify.VExpansionPanelHeader(h.Span(fmt.Sprintf("%v - %v", note.Creator, note.CreatedAt.Format("2006-01-02 15:04:05 MST")))),
  57. vuetify.VExpansionPanelContent(h.Text(note.Content)),
  58. ))
  59. }
  60. c.AppendChildren(vuetify.VExpansionPanels(panels...).Attr("style", "padding:10px;"))
  61. return c
  62. }
  63. var AfterCreateFunc = func(db *gorm.DB) (err error) {
  64. return
  65. }