event.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Event-related constants."""
  2. from enum import Enum
  3. from types import SimpleNamespace
  4. class Endpoint(Enum):
  5. """Endpoints for the reflex backend API."""
  6. PING = "ping"
  7. EVENT = "_event"
  8. UPLOAD = "_upload"
  9. def __str__(self) -> str:
  10. """Get the string representation of the endpoint.
  11. Returns:
  12. The path for the endpoint.
  13. """
  14. return f"/{self.value}"
  15. def get_url(self) -> str:
  16. """Get the URL for the endpoint.
  17. Returns:
  18. The full URL for the endpoint.
  19. """
  20. # Import here to avoid circular imports.
  21. from reflex.config import get_config
  22. # Get the API URL from the config.
  23. config = get_config()
  24. url = "".join([config.api_url, str(self)])
  25. # The event endpoint is a websocket.
  26. if self == Endpoint.EVENT:
  27. # Replace the protocol with ws.
  28. url = url.replace("https://", "wss://").replace("http://", "ws://")
  29. # Return the url.
  30. return url
  31. class SocketEvent(SimpleNamespace):
  32. """Socket events sent by the reflex backend API."""
  33. PING = "ping"
  34. EVENT = "event"
  35. def __str__(self) -> str:
  36. """Get the string representation of the event name.
  37. Returns:
  38. The event name string.
  39. """
  40. return str(self.value)
  41. class EventTriggers(SimpleNamespace):
  42. """All trigger names used in Reflex."""
  43. ON_FOCUS = "on_focus"
  44. ON_BLUR = "on_blur"
  45. ON_CANCEL = "on_cancel"
  46. ON_CLICK = "on_click"
  47. ON_CHANGE = "on_change"
  48. ON_CHANGE_END = "on_change_end"
  49. ON_CHANGE_START = "on_change_start"
  50. ON_CHECKED_CHANGE = "on_checked_change"
  51. ON_COMPLETE = "on_complete"
  52. ON_CONTEXT_MENU = "on_context_menu"
  53. ON_DOUBLE_CLICK = "on_double_click"
  54. ON_DROP = "on_drop"
  55. ON_EDIT = "on_edit"
  56. ON_KEY_DOWN = "on_key_down"
  57. ON_KEY_UP = "on_key_up"
  58. ON_MOUSE_DOWN = "on_mouse_down"
  59. ON_MOUSE_ENTER = "on_mouse_enter"
  60. ON_MOUSE_LEAVE = "on_mouse_leave"
  61. ON_MOUSE_MOVE = "on_mouse_move"
  62. ON_MOUSE_OUT = "on_mouse_out"
  63. ON_MOUSE_OVER = "on_mouse_over"
  64. ON_MOUSE_UP = "on_mouse_up"
  65. ON_SCROLL = "on_scroll"
  66. ON_SUBMIT = "on_submit"
  67. ON_MOUNT = "on_mount"
  68. ON_UNMOUNT = "on_unmount"
  69. ON_CLEAR_SERVER_ERRORS = "on_clear_server_errors"