浏览代码

factorizing serialization of jstree items

Damien Accorsi 11 年前
父节点
当前提交
400cedd716
共有 1 个文件被更改,包括 22 次插入26 次删除
  1. 22 26
      pboard/pboard/model/serializers.py

+ 22 - 26
pboard/pboard/model/serializers.py 查看文件

2
 import tg
2
 import tg
3
 from pboard.model import data as pmd
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
 def PBNodeForMenu(func):
23
 def PBNodeForMenu(func):
6
 
24
 
7
     def process_item(item: pmd.PBNode):
25
     def process_item(item: pmd.PBNode):
8
         """ convert given item into a dictionnary """
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
     def pre_serialize(*args, **kws):
29
     def pre_serialize(*args, **kws):
23
         initial_result = func(*args, **kws)
30
         initial_result = func(*args, **kws)
43
         """ convert given item into a dictionnary """
50
         """ convert given item into a dictionnary """
44
 
51
 
45
         item = structure_item.node
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
         children = []
53
         children = []
54
+
48
         for child_item in structure_item.children:
55
         for child_item in structure_item.children:
49
             children.append(process_item(child_item, current_node_id))
56
             children.append(process_item(child_item, current_node_id))
50
-        # print("########## BEFORE ##########")
51
 
57
 
52
         children_field_value = None
58
         children_field_value = None
53
         if len(children)>0:
59
         if len(children)>0:
58
             children_field_value = False
64
             children_field_value = False
59
 
65
 
60
         new_item_state = dict(
66
         new_item_state = dict(
61
-            opened = len(children)>0,
67
+            opened = item.getChildNb()<=0 or len(children)>0,
62
             selected = current_node_id!=None and item.node_id==current_node_id,
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
     def pre_serialize(*args, **kws):
73
     def pre_serialize(*args, **kws):
78
         initial_result = func(*args, **kws)
74
         initial_result = func(*args, **kws)