1
0

fullcalendar.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from pathlib import Path
  2. from typing import Any, Callable, Dict, List, Optional
  3. from nicegui.element import Element
  4. from nicegui.events import handle_event
  5. class FullCalendar(Element, component='fullcalendar.js'):
  6. def __init__(self, options: Dict[str, Any], on_click: Optional[Callable] = None) -> None:
  7. """FullCalendar
  8. An element that integrates the FullCalendar library (https://fullcalendar.io/) to create an interactive calendar display.
  9. For an example of the FullCalendar library with plugins see https://github.com/dorel14/NiceGui-FullCalendar_more_Options
  10. :param options: dictionary of FullCalendar properties for customization, such as "initialView", "slotMinTime", "slotMaxTime", "allDaySlot", "timeZone", "height", and "events".
  11. :param on_click: callback that is called when a calendar event is clicked.
  12. """
  13. super().__init__()
  14. self.add_resource(Path(__file__).parent / 'lib')
  15. self._props['options'] = options
  16. if on_click:
  17. self.on('click', lambda e: handle_event(on_click, e))
  18. def add_event(self, title: str, start: str, end: str, **kwargs) -> None:
  19. """Add an event to the calendar.
  20. :param title: title of the event
  21. :param start: start time of the event
  22. :param end: end time of the event
  23. """
  24. event_dict = {'title': title, 'start': start, 'end': end, **kwargs}
  25. self._props['options']['events'].append(event_dict)
  26. self.update()
  27. self.run_method('update_calendar')
  28. def remove_event(self, title: str, start: str, end: str) -> None:
  29. """Remove an event from the calendar.
  30. :param title: title of the event
  31. :param start: start time of the event
  32. :param end: end time of the event
  33. """
  34. for event in self._props['options']['events']:
  35. if event['title'] == title and event['start'] == start and event['end'] == end:
  36. self._props['options']['events'].remove(event)
  37. break
  38. self.update()
  39. self.run_method('update_calendar')
  40. @property
  41. def events(self) -> List[Dict]:
  42. """List of events currently displayed in the calendar."""
  43. return self._props['options']['events']