element.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import justpy as jp
  2. from enum import Enum
  3. from binding.binding import BindableProperty
  4. class Element:
  5. wp_stack = []
  6. view_stack = []
  7. visible = BindableProperty
  8. def __init__(self,
  9. view: jp.HTMLBaseComponent,
  10. ):
  11. self.parent_view = self.view_stack[-1]
  12. self.parent_view.add(view)
  13. self.view = view
  14. self.page = self.wp_stack[-1]
  15. self.view.add_page(self.page)
  16. self.visible = True
  17. @property
  18. def visible(self):
  19. return self.visible_
  20. @visible.setter
  21. def visible(self, visible: bool):
  22. self.visible_ = visible
  23. (self.view.remove_class if self.visible_ else self.view.set_class)('hidden')
  24. def bind_visibility_to(self, target, forward=lambda x: x):
  25. self.visible.bind_to(target, forward=forward, nesting=1)
  26. return self
  27. def bind_visibility_from(self, target, backward=lambda x: x, *, value=None):
  28. if value is not None:
  29. def backward(x): return x == value
  30. self.visible.bind_from(target, backward=backward, nesting=1)
  31. return self
  32. def bind_visibility(self, target, forward=lambda x: x, backward=None, *, value=None):
  33. if value is not None:
  34. def backward(x): return x == value
  35. self.visible.bind(target, forward=forward, backward=backward, nesting=1)
  36. return self
  37. def classes(self, add: str = '', *, remove: str = '', replace: str = ''):
  38. '''HTML classes to modify the look of the element.
  39. Every class in the `remove` parameter will be removed from the element.
  40. Classes are seperated with a blank space.
  41. This can be helpful if the predefined classes by NiceGUI are not wanted in a particular styling.
  42. '''
  43. class_list = [] if replace else self.view.classes.split()
  44. class_list = [c for c in class_list if c not in remove]
  45. class_list += add.split()
  46. class_list += replace.split()
  47. self.view.classes = ' '.join(class_list)
  48. return self
  49. def style(self, add: str = '', *, remove: str = '', replace: str = ''):
  50. '''CSS style sheet definitions to modify the look of the element.
  51. Every style in the `remove` parameter will be removed from the element.
  52. Styles are seperated with a semicolon.
  53. This can be helpful if the predefined style sheet definitions by NiceGUI are not wanted in a particular styling.
  54. '''
  55. style_list = [] if replace else self.view.style.split(';')
  56. style_list = [c for c in style_list if c not in remove.split(';')]
  57. style_list += add.split(';')
  58. style_list += replace.split(';')
  59. self.view.style = ';'.join(style_list)
  60. return self
  61. def props(self, add: str = '', *, remove: str = '', replace: str = ''):
  62. '''Quasar props https://quasar.dev/vue-components/button#design to modify the look of the element.
  63. Boolean props will automatically activated if they appear in the list of the `add` property.
  64. Props are seperated with a blank space.
  65. Every prop passed to the `remove` parameter will be removed from the element.
  66. This can be helpful if the predefined props by NiceGUI are not wanted in a particular styling.
  67. '''
  68. for prop in remove.split() + replace.split():
  69. setattr(self.view, prop.split('=')[0], None)
  70. for prop in add.split() + replace.split():
  71. if '=' in prop:
  72. setattr(self.view, *prop.split('='))
  73. else:
  74. setattr(self.view, prop, True)
  75. return self
  76. class Design(Enum):
  77. default = 1
  78. plain = 2