[seda tree] Skeleton for supporting DnD move to reorder sibling nodes
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Dec 2017 16:28:27 +0100
changeset 2912 1715f15e4651
parent 2911 ef03dc37382c
child 2913 5205b3490f1c
[seda tree] Skeleton for supporting DnD move to reorder sibling nodes by implementing ajax entry point and API on the jqtree adapter.
cubicweb_seda/views/jqtree.py
cubicweb_seda/views/sedatree.py
test/test_views.py
--- a/cubicweb_seda/views/jqtree.py	Mon Dec 11 16:22:05 2017 +0100
+++ b/cubicweb_seda/views/jqtree.py	Mon Dec 11 16:28:27 2017 +0100
@@ -33,6 +33,14 @@
     adapted.reparent(parent_eid, index)
 
 
+@ajaxcontroller.ajaxfunc(output_type='json')
+def jqtree_reorder(self, parent_eid, child_eid, index):
+    parent = self._cw.entity_from_eid(parent_eid)
+    adapted = parent.cw_adapt_to('IJQTree')
+    assert adapted is not None
+    adapted.move_child_at_index(child_eid, index)
+
+
 class IJQTreeAdapter(EntityAdapter):
     """Adapt an entity into a JQTree node."""
     __abstract__ = True
@@ -53,6 +61,10 @@
         """
         raise NotImplementedError
 
+    def move_child_at_index(self, ceid, index):
+        """Move child entity `ceid` at the `index` position."""
+        raise NotImplementedError
+
     def json(self, on_demand=False):
         """Return a JSON dict of an entity."""
         entity = self.entity
--- a/cubicweb_seda/views/sedatree.py	Mon Dec 11 16:22:05 2017 +0100
+++ b/cubicweb_seda/views/sedatree.py	Mon Dec 11 16:28:27 2017 +0100
@@ -20,7 +20,7 @@
 from cubicweb.web import component
 
 from ..entities import simplified_profile, component_unit
-from ..entities.itree import reparent
+from ..entities.itree import move_child_at_index, reparent
 from . import jqtree
 
 
@@ -56,6 +56,9 @@
         return ['SEDAArchiveUnit',
                 'SEDAPhysicalDataObject', 'SEDABinaryDataObject']
 
+    def move_child_at_index(self, ceid, index, reparenting=False):
+        move_child_at_index(self._cw, self.entity.eid, ceid, index, reparenting)
+
 
 class SimplifiedArchiveTransferIJQTreeAdapter(ArchiveTransferIJQTreeAdapter):
     __select__ = ArchiveTransferIJQTreeAdapter.__select__ & simplified_profile()
@@ -83,6 +86,11 @@
                 'cannot re-parent to entity type {0}'.format(parent.cw_etype))
         reparent(self.entity, parent.eid, index)
 
+    def move_child_at_index(self, ceid, index, reparenting=False):
+        move_child_at_index(self._cw,
+                            self.entity.first_level_choice.content_sequence.eid,
+                            ceid, index, reparenting)
+
 
 class SimplifiedArchiveUnitIJQTreeAdapter(ArchiveUnitIJQTreeAdapter):
     __select__ = ArchiveUnitIJQTreeAdapter.__select__ & simplified_profile()
@@ -91,6 +99,15 @@
         return [] if self.entity.is_archive_unit_ref else ['SEDAArchiveUnit',
                                                            'SEDABinaryDataObject']
 
+    def move_child_at_index(self, ceid, index, reparenting=False):
+        # when reordering a data object, we actually have to maintain ordering
+        # of its reference
+        child = self._cw.entity_from_eid(ceid)
+        if child.cw_etype == 'SEDABinaryDataObject':
+            child = child.reverse_seda_data_object_reference_id[0]
+        move_child_at_index(self._cw,
+                            self.entity.first_level_choice.content_sequence.eid,
+                            child.eid, index, reparenting)
 
 
 class DataObjectIJQTreeAdapter(SEDAIJQTreeAdapter):
--- a/test/test_views.py	Mon Dec 11 16:22:05 2017 +0100
+++ b/test/test_views.py	Mon Dec 11 16:28:27 2017 +0100
@@ -576,6 +576,44 @@
             unit_alt_seq.cw_clear_all_caches()
             self.assertFalse(unit_alt_seq.reverse_seda_data_object_reference)
 
+    def test_binarydataobject_reorder_full(self):
+        with self.admin_access.cnx() as cnx:
+            transfer = cnx.entity_from_eid(self.transfer_eid)
+            transfer.cw_set(simplified_profile=False)
+            bdo1 = testutils.create_data_object(transfer)
+            bdo2 = testutils.create_data_object(transfer)
+            cnx.commit()
+
+            transfer.cw_adapt_to('IJQTree').move_child_at_index(bdo2.eid, 0)
+
+            transfer.cw_clear_all_caches()
+            self.assertEqual([x.eid for x in transfer.reverse_seda_binary_data_object],
+                             [bdo2.eid, bdo1.eid])
+
+            bdo2.cw_clear_all_caches()
+            transfer.cw_adapt_to('IJQTree').move_child_at_index(bdo2.eid, 2)
+
+            bdo1.cw_clear_all_caches()
+            bdo2.cw_clear_all_caches()
+
+            transfer.cw_clear_all_caches()
+            self.assertEqual([x.eid for x in transfer.cw_adapt_to('ITreeBase').iterchildren()],
+                             [bdo1.eid, bdo2.eid])
+
+    def test_binarydataobject_reorder_simplified(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)
+            bdo1 = testutils.create_data_object(unit_alt_seq)
+            bdo2 = testutils.create_data_object(unit_alt_seq)
+            cnx.commit()
+
+            unit.cw_adapt_to('IJQTree').move_child_at_index(bdo2.eid, 0)
+
+            transfer.cw_clear_all_caches()
+            self.assertEqual([x.eid for x in unit.cw_adapt_to('ITreeBase').iterchildren()],
+                             [bdo2.eid, bdo1.eid])
+
 
 class FakeForm(object):
     def __init__(self, req, edited_entity):