You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flash.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package middleware
  4. import "net/url"
  5. // flashes enumerates all the flash types
  6. const (
  7. SuccessFlash = "SuccessMsg"
  8. ErrorFlash = "ErrorMsg"
  9. WarnFlash = "WarningMsg"
  10. InfoFlash = "InfoMsg"
  11. )
  12. // FlashNow FIXME:
  13. var FlashNow bool
  14. // Flash represents a one time data transfer between two requests.
  15. type Flash struct {
  16. DataStore ContextDataStore
  17. url.Values
  18. ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
  19. }
  20. func (f *Flash) set(name, msg string, current ...bool) {
  21. if f.Values == nil {
  22. f.Values = make(map[string][]string)
  23. }
  24. isShow := false
  25. if (len(current) == 0 && FlashNow) ||
  26. (len(current) > 0 && current[0]) {
  27. isShow = true
  28. }
  29. if isShow {
  30. f.DataStore.GetData()["Flash"] = f
  31. } else {
  32. f.Set(name, msg)
  33. }
  34. }
  35. // Error sets error message
  36. func (f *Flash) Error(msg string, current ...bool) {
  37. f.ErrorMsg = msg
  38. f.set("error", msg, current...)
  39. }
  40. // Warning sets warning message
  41. func (f *Flash) Warning(msg string, current ...bool) {
  42. f.WarningMsg = msg
  43. f.set("warning", msg, current...)
  44. }
  45. // Info sets info message
  46. func (f *Flash) Info(msg string, current ...bool) {
  47. f.InfoMsg = msg
  48. f.set("info", msg, current...)
  49. }
  50. // Success sets success message
  51. func (f *Flash) Success(msg string, current ...bool) {
  52. f.SuccessMsg = msg
  53. f.set("success", msg, current...)
  54. }