event.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_COMPLETE = "on_complete"
  51. ON_CONTEXT_MENU = "on_context_menu"
  52. ON_DOUBLE_CLICK = "on_double_click"
  53. ON_DROP = "on_drop"
  54. ON_EDIT = "on_edit"
  55. ON_KEY_DOWN = "on_key_down"
  56. ON_KEY_UP = "on_key_up"
  57. ON_MOUSE_DOWN = "on_mouse_down"
  58. ON_MOUSE_ENTER = "on_mouse_enter"
  59. ON_MOUSE_LEAVE = "on_mouse_leave"
  60. ON_MOUSE_MOVE = "on_mouse_move"
  61. ON_MOUSE_OUT = "on_mouse_out"
  62. ON_MOUSE_OVER = "on_mouse_over"
  63. ON_MOUSE_UP = "on_mouse_up"
  64. ON_OPEN_CHANGE = "on_open_change"
  65. ON_OPEN_AUTO_FOCUS = "on_open_auto_focus"
  66. ON_CLOSE_AUTO_FOCUS = "on_close_auto_focus"
  67. ON_FOCUS_OUTSIDE = "on_focus_outside"
  68. ON_ESCAPE_KEY_DOWN = "on_escape_key_down"
  69. ON_POINTER_DOWN_OUTSIDE = "on_pointer_down_outside"
  70. ON_INTERACT_OUTSIDE = "on_interact_outside"
  71. ON_SCROLL = "on_scroll"
  72. ON_SUBMIT = "on_submit"
  73. ON_MOUNT = "on_mount"
  74. ON_UNMOUNT = "on_unmount"
  75. ON_CLEAR_SERVER_ERRORS = "on_clear_server_errors"
  76. ON_VALUE_COMMIT = "on_value_commit"
  77. ON_SELECT = "on_select"