[seda tree] Skeleton for allowing to move a node at a specific position of the tree
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Dec 2017 16:22:05 +0100
changeset 2911 ef03dc37382c
parent 2910 8a08e1df2e59
child 2912 1715f15e4651
[seda tree] Skeleton for allowing to move a node at a specific position of the tree by implementing it on the JQTree adapter, but with only hard-coded position in the js for now.
cubicweb_seda/data/cubes.jqtree.js
cubicweb_seda/entities/itree.py
cubicweb_seda/views/jqtree.py
cubicweb_seda/views/sedatree.py
test/test_views.py
--- a/cubicweb_seda/data/cubes.jqtree.js	Mon Dec 11 16:15:19 2017 +0100
+++ b/cubicweb_seda/data/cubes.jqtree.js	Mon Dec 11 16:22:05 2017 +0100
@@ -62,10 +62,9 @@
             'tree.move',
             function(event) {
                 event.preventDefault();
-                // do the move first, and then POST back.
-                event.move_info.do_move();
-                asyncRemoteExec('jqtree_reparent', event.move_info.moved_node.id,
-                                event.move_info.target_node.id);
+                asyncRemoteExec('jqtree_reparent', move.moved_node.id, target_node.id, 0);
+                // do the move after POSTing.
+                move.do_move();
             }
         );
     }
--- a/cubicweb_seda/entities/itree.py	Mon Dec 11 16:15:19 2017 +0100
+++ b/cubicweb_seda/entities/itree.py	Mon Dec 11 16:22:05 2017 +0100
@@ -229,8 +229,10 @@
         {'x': child.eid}).rows
 
 
-def reparent(entity, new_parent_eid):
-    """Move `entity` as a children of `new_parent_eid`.
+def reparent(entity, new_parent_eid, index):
+    """Move `entity` as a children of `new_parent_eid` at `index` position.
     """
     rtype = ETYPE_PARENT_RTYPE[entity.cw_etype]
     entity.cw_set(**{rtype: new_parent_eid})
+    move_child_at_index(entity._cw, new_parent_eid, entity.eid, index,
+                        reparenting=True)
--- a/cubicweb_seda/views/jqtree.py	Mon Dec 11 16:15:19 2017 +0100
+++ b/cubicweb_seda/views/jqtree.py	Mon Dec 11 16:22:05 2017 +0100
@@ -26,11 +26,11 @@
 
 
 @ajaxcontroller.ajaxfunc(output_type='json')
-def jqtree_reparent(self, child_eid, parent_eid):
+def jqtree_reparent(self, child_eid, parent_eid, index):
     child = self._cw.entity_from_eid(child_eid)
     adapted = child.cw_adapt_to('IJQTree')
     assert adapted is not None
-    adapted.reparent(parent_eid)
+    adapted.reparent(parent_eid, index)
 
 
 class IJQTreeAdapter(EntityAdapter):
@@ -48,8 +48,9 @@
         """
         return False
 
-    def reparent(self, peid):
-        """Set entity as child of entity with `peid`."""
+    def reparent(self, peid, index):
+        """Set entity as child of entity with `peid`, at the `index` position.
+        """
         raise NotImplementedError
 
     def json(self, on_demand=False):
--- a/cubicweb_seda/views/sedatree.py	Mon Dec 11 16:15:19 2017 +0100
+++ b/cubicweb_seda/views/sedatree.py	Mon Dec 11 16:22:05 2017 +0100
@@ -74,14 +74,14 @@
     def maybe_moved(self):
         return True
 
-    def reparent(self, peid):
+    def reparent(self, peid, index):
         parent = self._cw.entity_from_eid(peid)
         if parent.cw_etype == 'SEDAArchiveUnit':
             parent = parent.first_level_choice.content_sequence
         else:
             assert parent.cw_etype == 'SEDAArchiveTransfer', (
                 'cannot re-parent to entity type {0}'.format(parent.cw_etype))
-        reparent(self.entity, parent.eid)
+        reparent(self.entity, parent.eid, index)
 
 
 class SimplifiedArchiveUnitIJQTreeAdapter(ArchiveUnitIJQTreeAdapter):
@@ -109,8 +109,8 @@
     def maybe_moved(self):
         return True
 
-    def reparent(self, peid):
+    def reparent(self, peid, index):
         archunit = self._cw.entity_from_eid(peid)
         parent = archunit.first_level_choice.content_sequence
         child = self.entity.reverse_seda_data_object_reference_id[0]
-        reparent(child, parent.eid)
+        reparent(child, parent.eid, index)
--- a/test/test_views.py	Mon Dec 11 16:15:19 2017 +0100
+++ b/test/test_views.py	Mon Dec 11 16:22:05 2017 +0100
@@ -537,24 +537,25 @@
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
             unit2 = testutils.create_archive_unit(unit_alt_seq)[0]
             cnx.commit()
-            unit2.cw_adapt_to('IJQTree').reparent(transfer.eid)
+            unit2.cw_adapt_to('IJQTree').reparent(transfer.eid, 0)
             cnx.commit()
             transfer.cw_clear_all_caches()
             self.assertEqual([x.eid for x in transfer.reverse_seda_archive_unit],
-                             [unit.eid, unit2.eid])
+                             [unit2.eid, unit.eid])
 
     def test_archiveunit_reparent_to_archiveunit(self):
         with self.admin_access.cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)
             unit, _, unit_alt_seq = testutils.create_archive_unit(transfer)
             unit2, _, unit2_alt_seq = testutils.create_archive_unit(transfer)
+            subunit, _, _ = testutils.create_archive_unit(unit2_alt_seq)
             cnx.commit()
             unit.cw_clear_all_caches()
-            unit.cw_adapt_to('IJQTree').reparent(unit2.eid)
+            unit.cw_adapt_to('IJQTree').reparent(unit2.eid, 0)
             cnx.commit()
             unit2_alt_seq.cw_clear_all_caches()
             self.assertEqual([x.eid for x in unit2_alt_seq.reverse_seda_archive_unit],
-                             [unit.eid])
+                             [unit.eid, subunit.eid])
 
     def test_binarydataobject_reparent(self):
         with self.admin_access.cnx() as cnx:
@@ -567,7 +568,7 @@
                                     seda_data_object_reference_id=bdo)
             cnx.commit()
             bdo.cw_clear_all_caches()
-            bdo.cw_adapt_to('IJQTree').reparent(unit2.eid)
+            bdo.cw_adapt_to('IJQTree').reparent(unit2.eid, 0)
             cnx.commit()
             unit2_alt_seq.cw_clear_all_caches()
             self.assertEqual([x.eid for x in unit2_alt_seq.reverse_seda_data_object_reference],