event.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. AUTH_CODESPACE = "auth-codespace"
  10. def __str__(self) -> str:
  11. """Get the string representation of the endpoint.
  12. Returns:
  13. The path for the endpoint.
  14. """
  15. return f"/{self.value}"
  16. def get_url(self) -> str:
  17. """Get the URL for the endpoint.
  18. Returns:
  19. The full URL for the endpoint.
  20. """
  21. # Import here to avoid circular imports.
  22. from reflex.config import get_config
  23. # Get the API URL from the config.
  24. config = get_config()
  25. url = "".join([config.api_url, str(self)])
  26. # The event endpoint is a websocket.
  27. if self == Endpoint.EVENT:
  28. # Replace the protocol with ws.
  29. url = url.replace("https://", "wss://").replace("http://", "ws://")
  30. # Return the url.
  31. return url
  32. class SocketEvent(SimpleNamespace):
  33. """Socket events sent by the reflex backend API."""
  34. PING = "ping"
  35. EVENT = "event"
  36. def __str__(self) -> str:
  37. """Get the string representation of the event name.
  38. Returns:
  39. The event name string.
  40. """
  41. return str(self.value)
  42. class EventTriggers(SimpleNamespace):
  43. """All trigger names used in Reflex."""
  44. ON_FOCUS = "on_focus"
  45. ON_BLUR = "on_blur"
  46. ON_CANCEL = "on_cancel"
  47. ON_CLICK = "on_click"
  48. ON_CHANGE = "on_change"
  49. ON_CHANGE_END = "on_change_end"
  50. ON_CHANGE_START = "on_change_start"
  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_OPEN_CHANGE = "on_open_change"
  66. ON_OPEN_AUTO_FOCUS = "on_open_auto_focus"
  67. ON_CLOSE_AUTO_FOCUS = "on_close_auto_focus"
  68. ON_FOCUS_OUTSIDE = "on_focus_outside"
  69. ON_ESCAPE_KEY_DOWN = "on_escape_key_down"
  70. ON_POINTER_DOWN_OUTSIDE = "on_pointer_down_outside"
  71. ON_INTERACT_OUTSIDE = "on_interact_outside"
  72. ON_SCROLL = "on_scroll"
  73. ON_SUBMIT = "on_submit"
  74. ON_MOUNT = "on_mount"
  75. ON_UNMOUNT = "on_unmount"
  76. ON_CLEAR_SERVER_ERRORS = "on_clear_server_errors"
  77. ON_VALUE_COMMIT = "on_value_commit"
  78. ON_SELECT = "on_select"