[PATCH 15 of 18 seda] [seda tree / js] Move 'canMoveTo' function to cubes.seda.js

Sylvain Thenault sylvain.thenault at logilab.fr
Mon Dec 11 16:53:47 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1513005319 -3600
#      Mon Dec 11 16:15:19 2017 +0100
# Node ID b1a41946879abf93ebf2cd274524063de0690e08
# Parent  6860d9ed3e19f3c2f42e851055119c7ad0006aa6
[seda tree / js] Move 'canMoveTo' function to cubes.seda.js

since this will soon become even more SEDA dependant.

diff --git a/cubicweb_seda/data/cubes.jqtree.js b/cubicweb_seda/data/cubes.jqtree.js
--- a/cubicweb_seda/data/cubes.jqtree.js
+++ b/cubicweb_seda/data/cubes.jqtree.js
@@ -1,6 +1,6 @@
-// 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 at logilab.fr
 //
 // This program is free software: you can redistribute it and/or modify it under
 // the terms of the GNU Lesser General Public License as published by the Free
 // Software Foundation, either version 2.1 of the License, or (at your option)
@@ -14,11 +14,11 @@
 // You should have received a copy of the GNU Lesser General Public License along
 // with this program. If not, see <http://www.gnu.org/licenses/>.
 
 
 jqtree = {
-    jqTree: function(domid, dragAndDrop) {
+    jqTree: function(domid, dragAndDrop, canMoveTo) {
         var $tree = cw.jqNode(domid);
         // tree display and basic controls.
         $tree.tree({
             dragAndDrop: dragAndDrop,
             autoOpen: 0,  // only open level-0
@@ -28,25 +28,11 @@ jqtree = {
             openedIcon: $('<i class="glyphicon glyphicon-collapse-down"></i>'),
             onCanMove: function(node) {
                 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) {
                 $li.find('.jqtree-title').addClass(node.type);
 
diff --git a/cubicweb_seda/data/cubes.seda.js b/cubicweb_seda/data/cubes.seda.js
--- a/cubicweb_seda/data/cubes.seda.js
+++ b/cubicweb_seda/data/cubes.seda.js
@@ -1,6 +1,6 @@
-// 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 at logilab.fr
 //
 // This program is free software: you can redistribute it and/or modify it under
 // the terms of the GNU Lesser General Public License as published by the Free
 // Software Foundation, either version 2.1 of the License, or (at your option)
@@ -27,6 +27,24 @@ seda = {
             classes[0] = 'icon-list-add';
         }
         $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;
+        }
+    }
 };
diff --git a/cubicweb_seda/views/jqtree.py b/cubicweb_seda/views/jqtree.py
--- a/cubicweb_seda/views/jqtree.py
+++ b/cubicweb_seda/views/jqtree.py
@@ -17,10 +17,11 @@
 """cubicweb-seda views for presenting tree data (using jqTree)."""
 
 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
 
 
@@ -187,6 +188,8 @@ class JQTreeView(EntityView):
         divid = 'jqtree' + str(entity.eid)
         data_url = entity.absolute_url(vid='jqtree.json')
         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)))
diff --git a/cubicweb_seda/views/sedatree.py b/cubicweb_seda/views/sedatree.py
--- a/cubicweb_seda/views/sedatree.py
+++ b/cubicweb_seda/views/sedatree.py
@@ -33,20 +33,25 @@ class ProfileTreeComponent(component.Ent
     order = -1
     title = _('SEDA profile tree')
 
     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)
 
 
 class UnitTreeComponent(ProfileTreeComponent):
     __select__ = ProfileTreeComponent.__select__ & component_unit()
     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):
         return ['SEDAArchiveUnit',
                 'SEDAPhysicalDataObject', 'SEDABinaryDataObject']
@@ -57,12 +62,12 @@ class SimplifiedArchiveTransferIJQTreeAd
 
     def maybe_parent_of(self):
         return ['SEDAArchiveUnit']
 
 
-class ArchiveUnitIJQTreeAdapter(jqtree.IJQTreeAdapter):
-    __select__ = (jqtree.IJQTreeAdapter.__select__
+class ArchiveUnitIJQTreeAdapter(SEDAIJQTreeAdapter):
+    __select__ = (SEDAIJQTreeAdapter.__select__
                   & is_instance('SEDAArchiveUnit'))
 
     def maybe_parent_of(self):
         return [] if self.entity.is_archive_unit_ref else ['SEDAArchiveUnit']
 
@@ -85,20 +90,21 @@ class SimplifiedArchiveUnitIJQTreeAdapte
     def maybe_parent_of(self):
         return [] if self.entity.is_archive_unit_ref else ['SEDAArchiveUnit',
                                                            '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'
 
     def maybe_moved(self):
         return True


More information about the saem-devel mailing list