[seda tree / js] Move 'canMoveTo' function to cubes.seda.js
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Dec 2017 16:15:19 +0100
changeset 2910 8a08e1df2e59
parent 2909 7042ff92abcf
child 2911 ef03dc37382c
[seda tree / js] Move 'canMoveTo' function to cubes.seda.js since this will soon become even more SEDA dependant.
cubicweb_seda/data/cubes.jqtree.js
cubicweb_seda/data/cubes.seda.js
cubicweb_seda/views/jqtree.py
cubicweb_seda/views/sedatree.py
--- a/cubicweb_seda/data/cubes.jqtree.js	Mon Dec 11 15:13:00 2017 +0100
+++ b/cubicweb_seda/data/cubes.jqtree.js	Mon Dec 11 16:15:19 2017 +0100
@@ -1,4 +1,4 @@
-// copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+// copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 // contact http://www.logilab.fr -- mailto:contact@logilab.fr
 //
 // This program is free software: you can redistribute it and/or modify it under
@@ -16,7 +16,7 @@
 
 
 jqtree = {
-    jqTree: function(domid, dragAndDrop) {
+    jqTree: function(domid, dragAndDrop, canMoveTo) {
         var $tree = cw.jqNode(domid);
         // tree display and basic controls.
         $tree.tree({
@@ -30,21 +30,7 @@
                 return node.maybeMoved;
             },
             onCanMoveTo: function(moved_node, target_node, position) {
-                if ( target_node.id === undefined ) {
-                    return false;
-                } else if ( position != 'inside' ) {
-                    // moving before/after is not supported
-                    return false;
-                } else {
-                    // avoid moving into the same parent
-                    function isMovedNode(element, index, array) {
-                        return element.id == moved_node.id;
-                    }
-                    if ( target_node.children.some(isMovedNode) ) {
-                        return false;
-                    }
-                    // ensure the new parent target accept the moved node
-                    return target_node.maybeParentOf.indexOf(moved_node.type) !== -1;
+                    return canMoveTo(moved_node, target_node, position);
                 }
             },
             onCreateLi: function(node, $li) {
--- a/cubicweb_seda/data/cubes.seda.js	Mon Dec 11 15:13:00 2017 +0100
+++ b/cubicweb_seda/data/cubes.seda.js	Mon Dec 11 16:15:19 2017 +0100
@@ -1,4 +1,4 @@
-// copyright 2015-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+// copyright 2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 // contact http://www.logilab.fr -- mailto:contact@logilab.fr
 //
 // This program is free software: you can redistribute it and/or modify it under
@@ -29,4 +29,22 @@
         $a.attr('class', classes.join(' '));
     },
 
+    canMoveTo: function(moved_node, target_node, position){
+        if ( target_node.id === undefined ) {
+            return false;
+        } else if ( position != 'inside' ) {
+            // moving before/after is not supported
+            return false;
+        } else {
+            // avoid moving into the same parent
+            function isMovedNode(element, index, array) {
+                return element.id == moved_node.id;
+            }
+            if ( target_node.children.some(isMovedNode) ) {
+                return false;
+            }
+            // ensure the new parent target accept the moved node
+            return target_node.maybeParentOf.indexOf(moved_node.type) !== -1;
+        }
+    }
 };
--- a/cubicweb_seda/views/jqtree.py	Mon Dec 11 15:13:00 2017 +0100
+++ b/cubicweb_seda/views/jqtree.py	Mon Dec 11 16:15:19 2017 +0100
@@ -19,6 +19,7 @@
 from functools import wraps
 
 from cubicweb import tags, uilib
+from cubicweb.utils import JSString
 from cubicweb.view import EntityAdapter, EntityView, View
 from cubicweb.predicates import adaptable, match_form_params
 from cubicweb.web.views import ajaxcontroller, json
@@ -189,4 +190,6 @@
         self.w(tags.div(id=divid, **{'data-url': data_url}))
         mayupdate = entity.e_schema.has_perm(
             self._cw, 'update', eid=entity.eid)
-        self._cw.add_onload(uilib.js.jqtree.jqTree(divid, mayupdate))
+        adapted = entity.cw_adapt_to('IJQTree')
+        self._cw.add_onload(uilib.js.jqtree.jqTree(divid, mayupdate,
+                                                   JSString(adapted.js_can_move_to)))
--- a/cubicweb_seda/views/sedatree.py	Mon Dec 11 15:13:00 2017 +0100
+++ b/cubicweb_seda/views/sedatree.py	Mon Dec 11 16:15:19 2017 +0100
@@ -35,6 +35,7 @@
 
     def render_body(self, w):
         self._cw.add_css('cubes.jqtree.css')
+        self._cw.add_js('cubes.seda.js')
         self.entity.view('jqtree.treeview', w=w)
 
 
@@ -43,8 +44,12 @@
     title = _('Archive unit component tree')
 
 
-class ArchiveTransferIJQTreeAdapter(jqtree.IJQTreeAdapter):
-    __select__ = (jqtree.IJQTreeAdapter.__select__
+class SEDAIJQTreeAdapter(jqtree.IJQTreeAdapter):
+    js_can_move_to = 'seda.canMoveTo'
+
+
+class ArchiveTransferIJQTreeAdapter(SEDAIJQTreeAdapter):
+    __select__ = (SEDAIJQTreeAdapter.__select__
                   & is_instance('SEDAArchiveTransfer'))
 
     def maybe_parent_of(self):
@@ -59,8 +64,8 @@
         return ['SEDAArchiveUnit']
 
 
-class ArchiveUnitIJQTreeAdapter(jqtree.IJQTreeAdapter):
-    __select__ = (jqtree.IJQTreeAdapter.__select__
+class ArchiveUnitIJQTreeAdapter(SEDAIJQTreeAdapter):
+    __select__ = (SEDAIJQTreeAdapter.__select__
                   & is_instance('SEDAArchiveUnit'))
 
     def maybe_parent_of(self):
@@ -87,16 +92,17 @@
                                                            'SEDABinaryDataObject']
 
 
-class DataObjectIJQTreeAdapter(jqtree.IJQTreeAdapter):
-    __select__ = (jqtree.IJQTreeAdapter.__select__
+
+class DataObjectIJQTreeAdapter(SEDAIJQTreeAdapter):
+    __select__ = (SEDAIJQTreeAdapter.__select__
                   & is_instance('SEDABinaryDataObject', 'SEDAPhysicalDataObject'))
 
     def maybe_moved(self):
         return False
 
 
-class SimplifiedBinaryDataObjectIJQTreeAdapter(jqtree.IJQTreeAdapter):
-    __select__ = (jqtree.IJQTreeAdapter.__select__
+class SimplifiedBinaryDataObjectIJQTreeAdapter(SEDAIJQTreeAdapter):
+    __select__ = (SEDAIJQTreeAdapter.__select__
                   & is_instance('SEDABinaryDataObject') & simplified_profile())
     rtype_to_archivetransfer = 'seda_binary_data_object'