group.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from __future__ import annotations
  2. from typing import List
  3. import justpy as jp
  4. from ..binding import active_links, bindable_properties, bindings
  5. from ..globals import view_stack
  6. from .element import Element
  7. class Group(Element):
  8. def __enter__(self):
  9. view_stack.append(self.view)
  10. return self
  11. def __exit__(self, *_):
  12. view_stack.pop()
  13. def tight(self) -> Group:
  14. return self.classes(replace='').style(replace='')
  15. def clear(self):
  16. def collect_components(view: jp.HTMLBaseComponent) -> List[jp.HTMLBaseComponent]:
  17. return view.components + [view for child in view.components for view in collect_components(child)]
  18. components = collect_components(self.view)
  19. active_links[:] = [
  20. (source_obj, source_name, target_obj, target_name, transform)
  21. for source_obj, source_name, target_obj, target_name, transform in active_links
  22. if not (
  23. isinstance(source_obj, jp.HTMLBaseComponent) and source_obj in components or
  24. isinstance(target_obj, jp.HTMLBaseComponent) and target_obj in components or
  25. isinstance(source_obj, Element) and source_obj.view in components or
  26. isinstance(target_obj, Element) and target_obj.view in components
  27. )
  28. ]
  29. for key, binding_list in list(bindings.items()):
  30. binding_list[:] = [
  31. (source_obj, target_obj, target_name, transform)
  32. for source_obj, target_obj, target_name, transform in binding_list
  33. if not (
  34. isinstance(source_obj, jp.HTMLBaseComponent) and source_obj in components or
  35. isinstance(target_obj, jp.HTMLBaseComponent) and target_obj in components or
  36. isinstance(source_obj, Element) and source_obj.view in components or
  37. isinstance(target_obj, Element) and target_obj.view in components
  38. )
  39. ]
  40. if not binding_list:
  41. del bindings[key]
  42. for (obj_id, name), obj in list(bindable_properties.items()):
  43. if isinstance(obj, jp.HTMLBaseComponent) and obj in components or \
  44. isinstance(obj, Element) and obj.view in components:
  45. del bindable_properties[(obj_id, name)]
  46. self.view.delete_components()