Coverage for gws-app / gws / base / client / core.py: 63%

75 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-03 10:12 +0100

1from typing import Optional 

2 

3import gws 

4 

5 

6class ElementConfig(gws.ConfigWithAccess): 

7 """GWS client UI element configuration""" 

8 

9 tag: str 

10 """Element tag.""" 

11 before: str = '' 

12 """Insert before this tag.""" 

13 after: str = '' 

14 """Insert after this tag.""" 

15 options: Optional[dict] 

16 """Element options.""" 

17 

18 

19class Config(gws.ConfigWithAccess): 

20 """GWS client configuration""" 

21 

22 options: Optional[dict] 

23 """Client options.""" 

24 elements: Optional[list[ElementConfig]] 

25 """Client UI elements.""" 

26 addElements: Optional[list[ElementConfig]] 

27 """Add elements to the parent element list.""" 

28 removeElements: Optional[list[ElementConfig]] 

29 """Remove elements from the parent element list.""" 

30 

31 

32class ElementProps(gws.Data): 

33 tag: str 

34 

35 

36class Props(gws.Data): 

37 options: Optional[dict] 

38 elements: Optional[list[ElementProps]] 

39 

40 

41class Element(gws.Node): 

42 tag: str 

43 after: str 

44 before: str 

45 options: dict 

46 

47 def configure(self): 

48 self.tag = self.cfg('tag') 

49 self.after = self.cfg('after') 

50 self.before = self.cfg('before') 

51 self.options = self.cfg('options') or {} 

52 

53 def props(self, user): 

54 return gws.Data(tag=self.tag, options=self.options) 

55 

56 

57class Object(gws.Client): 

58 options: dict 

59 elements: list[Element] 

60 

61 def configure(self): 

62 app_client = gws.u.get(self.root.app, 'client') 

63 

64 self.elements = self.create_children(Element, self._get_elements(app_client)) 

65 

66 self.options = gws.u.merge( 

67 app_client.options if app_client else {}, 

68 self.cfg('options')) 

69 

70 def props(self, user): 

71 return Props( 

72 options=self.options, 

73 elements=self.elements, 

74 ) 

75 

76 def _get_elements(self, app_client): 

77 elements = self.cfg('elements') 

78 if elements: 

79 return elements 

80 

81 if not app_client: 

82 return [] 

83 

84 add = self.cfg('addElements', default=[]) 

85 remove = self.cfg('removeElements', default=[]) 

86 elements = list(app_client.elements) 

87 

88 for c in add: 

89 n = self._find_element(elements, c.tag) 

90 if n >= 0: 

91 elements.pop(n) 

92 if c.before: 

93 n = self._find_element(elements, c.before) 

94 if n >= 0: 

95 elements.insert(n, c) 

96 elif c.after: 

97 n = self._find_element(elements, c.after) 

98 if n >= 0: 

99 elements.insert(n + 1, c) 

100 else: 

101 elements.append(c) 

102 

103 remove_tags = [c.tag for c in remove] 

104 return [e for e in elements if e.tag not in remove_tags] 

105 

106 def _find_element(self, elements, tag): 

107 for n, el in enumerate(elements): 

108 if el.tag == tag: 

109 return n 

110 return -1