Browse Source

factorizing serialization of jstree items

Damien Accorsi 10 years ago
parent
commit
400cedd716
1 changed files with 22 additions and 26 deletions
  1. 22 26
      pboard/pboard/model/serializers.py

+ 22 - 26
pboard/pboard/model/serializers.py View File

@@ -2,22 +2,29 @@
2 2
 import tg
3 3
 from pboard.model import data as pmd
4 4
 
5
+def node_to_dict(node: pmd.PBNode, children_content, new_item_state):
6
+    """
7
+        children_content may be boolean or a list containing json values
8
+    """
9
+    url = tg.url('/document/', dict(node_id=node.node_id)) ## FIXME - 2014-05-27 - Make this more flexible
10
+
11
+    return dict(
12
+        id = node.node_id,
13
+        children = children_content,
14
+        text = node.data_label,
15
+        a_attr = { "href" : url },
16
+        li_attr = { "title": node.data_label },
17
+        type = node.node_type, # this property is understandable by jstree (through "types" plugin)
18
+        state = new_item_state,
19
+        node_status = node.getStatus().getId() # this is not jstree understandable data. This requires a JS 'success' callback
20
+    )
21
+
22
+
5 23
 def PBNodeForMenu(func):
6 24
 
7 25
     def process_item(item: pmd.PBNode):
8 26
         """ convert given item into a dictionnary """
9
-        url = tg.url('/document/', dict(node_id=item.node_id)) ## FIXME - 2014-05-27 - Make this more flexible
10
-        print("########## BEFORE ##########")
11
-        new_item = dict(
12
-            id = item.node_id,
13
-            children = item.getChildNb()>0,
14
-            text = item.data_label,
15
-            # parent = item._oParent.node_id if (item._oParent!=None) else '#',
16
-            a_attr = { "href" : url },
17
-            li_attr = { "title": item.data_label }
18
-        )
19
-        print("########## AFTER ##########")
20
-        return new_item
27
+        return node_to_dict(item, item.getChildNb()>0, None)
21 28
 
22 29
     def pre_serialize(*args, **kws):
23 30
         initial_result = func(*args, **kws)
@@ -43,11 +50,10 @@ def NodeTreeItemForMenu(func):
43 50
         """ convert given item into a dictionnary """
44 51
 
45 52
         item = structure_item.node
46
-        url = tg.url('/document/', dict(node_id=item.node_id)) ## FIXME - 2014-05-27 - Make this more flexible
47 53
         children = []
54
+
48 55
         for child_item in structure_item.children:
49 56
             children.append(process_item(child_item, current_node_id))
50
-        # print("########## BEFORE ##########")
51 57
 
52 58
         children_field_value = None
53 59
         if len(children)>0:
@@ -58,21 +64,11 @@ def NodeTreeItemForMenu(func):
58 64
             children_field_value = False
59 65
 
60 66
         new_item_state = dict(
61
-            opened = len(children)>0,
67
+            opened = item.getChildNb()<=0 or len(children)>0,
62 68
             selected = current_node_id!=None and item.node_id==current_node_id,
63 69
         )
64 70
 
65
-        new_item = dict(
66
-            id = item.node_id,
67
-            children = children_field_value,
68
-            text = item.data_label,
69
-            # parent = item._oParent.node_id if (item._oParent!=None) else '#',
70
-            state = new_item_state,
71
-            a_attr = { "href" : url },
72
-            li_attr = { "title": item.data_label }
73
-        )
74
-        # print("########## AFTER ##########")
75
-        return new_item
71
+        return node_to_dict(item, children_field_value, new_item_state)
76 72
 
77 73
     def pre_serialize(*args, **kws):
78 74
         initial_result = func(*args, **kws)