utils.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package presets
  2. import (
  3. "fmt"
  4. "net/url"
  5. "reflect"
  6. "time"
  7. "github.com/qor5/admin/presets/actions"
  8. . "github.com/qor5/ui/vuetify"
  9. vx "github.com/qor5/ui/vuetifyx"
  10. "github.com/qor5/web"
  11. h "github.com/theplant/htmlgo"
  12. )
  13. func ShowMessage(r *web.EventResponse, msg string, color string) {
  14. if msg == "" {
  15. return
  16. }
  17. if color == "" {
  18. color = "success"
  19. }
  20. web.AppendVarsScripts(r, fmt.Sprintf(
  21. `vars.presetsMessage = { show: true, message: %s, color: %s}`,
  22. h.JSONString(msg), h.JSONString(color)))
  23. }
  24. func EditDeleteRowMenuItemFuncs(mi *ModelInfo, url string, editExtraParams url.Values) []vx.RowMenuItemFunc {
  25. return []vx.RowMenuItemFunc{
  26. editRowMenuItemFunc(mi, url, editExtraParams),
  27. deleteRowMenuItemFunc(mi, url, editExtraParams),
  28. }
  29. }
  30. func editRowMenuItemFunc(mi *ModelInfo, url string, editExtraParams url.Values) vx.RowMenuItemFunc {
  31. return func(obj interface{}, id string, ctx *web.EventContext) h.HTMLComponent {
  32. msgr := MustGetMessages(ctx.R)
  33. if mi.mb.Info().Verifier().Do(PermUpdate).ObjectOn(obj).WithReq(ctx.R).IsAllowed() != nil {
  34. return nil
  35. }
  36. onclick := web.Plaid().
  37. EventFunc(actions.Edit).
  38. Queries(editExtraParams).
  39. Query(ParamID, id).
  40. URL(url)
  41. if IsInDialog(ctx.R.Context()) {
  42. onclick.URL(ctx.R.RequestURI).
  43. Query(ParamOverlay, actions.Dialog).
  44. Query(ParamInDialog, true).
  45. Query(ParamListingQueries, ctx.Queries().Encode())
  46. }
  47. return VListItem(
  48. VListItemIcon(VIcon("edit")),
  49. VListItemTitle(h.Text(msgr.Edit)),
  50. ).Attr("@click", onclick.Go())
  51. }
  52. }
  53. func deleteRowMenuItemFunc(mi *ModelInfo, url string, editExtraParams url.Values) vx.RowMenuItemFunc {
  54. return func(obj interface{}, id string, ctx *web.EventContext) h.HTMLComponent {
  55. msgr := MustGetMessages(ctx.R)
  56. if mi.mb.Info().Verifier().Do(PermDelete).ObjectOn(obj).WithReq(ctx.R).IsAllowed() != nil {
  57. return nil
  58. }
  59. onclick := web.Plaid().
  60. EventFunc(actions.DeleteConfirmation).
  61. Queries(editExtraParams).
  62. Query(ParamID, id).
  63. URL(url)
  64. if IsInDialog(ctx.R.Context()) {
  65. onclick.URL(ctx.R.RequestURI).
  66. Query(ParamOverlay, actions.Dialog).
  67. Query(ParamInDialog, true).
  68. Query(ParamListingQueries, ctx.Queries().Encode())
  69. }
  70. return VListItem(
  71. VListItemIcon(VIcon("delete")),
  72. VListItemTitle(h.Text(msgr.Delete)),
  73. ).Attr("@click", onclick.Go())
  74. }
  75. }
  76. func copyURLWithQueriesRemoved(u *url.URL, qs ...string) *url.URL {
  77. newU, _ := url.Parse(u.String())
  78. newQuery := newU.Query()
  79. for _, k := range qs {
  80. newQuery.Del(k)
  81. }
  82. newU.RawQuery = newQuery.Encode()
  83. return newU
  84. }
  85. func isInDialogFromQuery(ctx *web.EventContext) bool {
  86. return ctx.R.URL.Query().Get(ParamInDialog) == "true"
  87. }
  88. func ptrTime(t time.Time) *time.Time {
  89. return &t
  90. }
  91. type inputElem interface {
  92. *h.HTMLTagBuilder | *VSelectBuilder | *VTextFieldBuilder
  93. }
  94. var (
  95. htmlTagBuilderType = reflect.TypeOf(&h.HTMLTagBuilder{})
  96. )
  97. // InputWithDefaults fills input element with name, value and label.
  98. // For now, input must be h.Input, v.VSelect or v.VTextField.
  99. // This function will be implemented using the FieldContext method
  100. // when golang supports generic method and the current function will be deprecated.
  101. func InputWithDefaults[T inputElem](input T, obj any, field *FieldContext) T {
  102. rv := reflect.ValueOf(input)
  103. rt := rv.Type()
  104. if rt == htmlTagBuilderType {
  105. kvs := web.VFieldName(field.FormKey)
  106. in := make([]reflect.Value, 0, len(kvs))
  107. for i, _ := range kvs {
  108. v := kvs[i]
  109. in = append(in, reflect.ValueOf(v))
  110. }
  111. rv.MethodByName("Attr").Call(in)
  112. rv.MethodByName("Value").Call([]reflect.Value{reflect.ValueOf(field.StringValue(obj))})
  113. } else {
  114. rv.MethodByName("FieldName").Call([]reflect.Value{reflect.ValueOf(field.FormKey)})
  115. rv.MethodByName("Value").Call([]reflect.Value{reflect.ValueOf(field.Value(obj))})
  116. rv.MethodByName("Label").Call([]reflect.Value{reflect.ValueOf(field.Label)})
  117. }
  118. return input
  119. }