[PATCH 1 of 2 saem_ref] Make ConceptSchemes and Concepts adaptable to IJQTree

Frank Bessou frank.bessou at logilab.fr
Thu Sep 27 16:08:02 CEST 2018


# HG changeset patch
# User Frank Bessou <frank.bessou at logilab.fr>
# Date 1538037542 -7200
#      Thu Sep 27 10:39:02 2018 +0200
# Node ID b474ec8a076ba4e20beaf571cd9da26165ec17c5
# Parent  4d3586d07bd4d6489657a1cbbf24e34bbae84839
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r b474ec8a076b
Make ConceptSchemes and Concepts adaptable to IJQTree

We implement needed adapters so that a ConceptScheme can be rendered as
a JQTree: IJQTreeAdapter and ITreeBaseAdapter.

Since Concept is already adaptable to IContained we make it adaptable
to ITreeBase by extending the IContainedToITreeBase adapter.

We have to provide js callback to JQTree adapters which tells if it is
possible the move the Concept to a given node. Since a Concept can
always be moved we make this callback return always true.

diff --git a/cubicweb_saem_ref/data/cubes.saem_ref.js b/cubicweb_saem_ref/data/cubes.saem_ref.js
--- a/cubicweb_saem_ref/data/cubes.saem_ref.js
+++ b/cubicweb_saem_ref/data/cubes.saem_ref.js
@@ -156,6 +156,10 @@ saem = {
             $('#' + refresh_view + eid).loadxhtml(
                 AJAX_BASE_URL, ajaxFuncArgs('view', {eid: eid, vid: refresh_view}));
         });
+    },
+
+    canMoveTo : function(moved_node, target_node, position){
+        return true;
     }
 
 };
diff --git a/cubicweb_saem_ref/views/jqtree.py b/cubicweb_saem_ref/views/jqtree.py
new file mode 100644
--- /dev/null
+++ b/cubicweb_saem_ref/views/jqtree.py
@@ -0,0 +1,77 @@
+# copyright 2018 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)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# 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/>.
+
+"""cubicweb-saem-ref adapters for manipulating trees of entities"""
+
+from cubicweb.view import EntityAdapter
+from cubicweb.predicates import is_instance
+from cubicweb.uilib import js
+
+import cubicweb_seda.views.jqtree as jqtree
+from cubicweb_seda.entities.itree import IContainedToITreeBase
+
+
+class ConceptSchemeJQTreeAdapter(jqtree.IJQTreeAdapter):
+    __select__ = jqtree.IJQTreeAdapter.__select__ & is_instance('ConceptScheme')
+    js_can_move_to = js.saem.canMoveTo
+
+    def maybe_parent_of(self):
+        return ['Concept']
+
+    def maybe_moved(self):
+        return False
+
+
+class ConceptJQTreeAdapter(jqtree.IJQTreeAdapter):
+    __select__ = jqtree.IJQTreeAdapter.__select__ & is_instance('Concept')
+    js_can_move_to = js.saem.canMoveTo
+
+    def maybe_parent_of(self):
+        return ['Concept']
+
+    def maybe_moved(self):
+        return True
+
+    def reparent(self, peid, index):
+        parent = self._cw.entity_from_eid(peid)
+        if parent.eid == self.entity.in_scheme[0].eid:
+            self.entity.cw_set(broader_concept=None)
+            return
+        self.entity.cw_set(broader_concept=[parent])
+
+
+class ConceptSchemeITreeBaseAdapter(EntityAdapter):
+    __regid__ = 'ITreeBase'
+    __select__ = is_instance('ConceptScheme')
+
+    def iterparents(self):
+        return iter([])
+
+    def iterchildren(self):
+        for concept in self.entity.reverse_in_scheme:
+            if not concept.broader_concept:
+                yield concept
+
+    def is_leaf(self):
+        """Returns True if the entity does not have any children."""
+        return not self.entity.reverse_in_scheme_concept
+
+
+class ConceptITreeBaseAdapter(IContainedToITreeBase):
+    __regid__ = 'ITreeBase'
+    __select__ = is_instance('Concept')
+
+    _children_relations = [('broader_concept', 'object')]



More information about the saem-devel mailing list