[treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree treeview-tabs
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 20 Feb 2009 20:48:33 +0100
branchtreeview-tabs
changeset 916 968f00dd9a24
parent 915 d7f85fd197df
child 917 51d5224b1698
[treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
web/data/cubicweb.widgets.js
web/views/treeview.py
--- a/web/data/cubicweb.widgets.js	Fri Feb 20 15:33:43 2009 +0100
+++ b/web/data/cubicweb.widgets.js	Fri Feb 20 20:48:33 2009 +0100
@@ -181,14 +181,6 @@
     }
 }
 
-Widgets.TreeView = defclass("TreeView", null, {
-    __init__: function(wdgnode) {
-	jQuery(wdgnode).treeview({toggle: toggleTree,
-				  prerendered: true
-				 });
-    }
-});
-
 
 /* widget based on SIMILE's timeline widget
  * http://code.google.com/p/simile-widgets/
--- a/web/views/treeview.py	Fri Feb 20 15:33:43 2009 +0100
+++ b/web/views/treeview.py	Fri Feb 20 20:48:33 2009 +0100
@@ -1,38 +1,32 @@
 from logilab.mtconverter import html_escape
-
 from cubicweb.interfaces import ITree
 from cubicweb.common.selectors import implement_interface, yes
 from cubicweb.common.view import EntityView
 
-from cubicweb.web.views.baseviews import OneLineView
-
 class TreeView(EntityView):
     id = 'treeview'
     accepts = ('Any',)
     itemvid = 'treeitemview'
     css_classes = 'treeview widget'
     title = _('tree view')
-    
-    def call(self, subvid=None):
+
+    def call(self, subvid=None, treeid=None):
         if subvid is None and 'subvid' in self.req.form:
             subvid = self.req.form.pop('subvid') # consume it
         if subvid is None:
             subvid = 'oneline'
         self.req.add_css('jquery.treeview.css')
-        self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js', 'cubicweb.widgets.js'))
-        # XXX noautoload is a quick hack to avoid treeview to be rebuilt
-        #     after a json query and avoid double toggling bugs.
-        #     Need to find a way to do that cleanly.
-        if 'noautoload' in self.req.form:
-            self.w(u'<ul class="%s" cubicweb:wdgtype="TreeView">' % self.css_classes)
-        else:
-            self.w(u'<ul class="%s" cubicweb:loadtype="auto" cubicweb:wdgtype="TreeView">'
-                   % self.css_classes)
+        self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js'))
+        # XXX find a way, an id is MANDATORY
+        treeid = 'TREE' #treeid or self.rset.rows[0][0]
+        self.req.html_headers.add_onload(u"""
+             $("#tree-%s").treeview({toggle: toggleTree,
+		                     prerendered: true});""" % treeid)
+        self.w(u'<ul id="tree-%s" class="%s">' % (treeid, self.css_classes))
         for rowidx in xrange(len(self.rset)):
             self.wview(self.itemvid, self.rset, row=rowidx, col=0,
                        vid=subvid, parentvid=self.id)
         self.w(u'</ul>')
-        
 
 class FileTreeView(TreeView):
     """specific version of the treeview to display file trees
@@ -44,8 +38,6 @@
     def call(self, subvid=None):
         super(FileTreeView, self).call(subvid='filetree-oneline')
 
-
-
 class FileItemInnerView(EntityView):
     """inner view used by the TreeItemView instead of oneline view
 
@@ -57,10 +49,10 @@
     def cell_call(self, row, col):
         entity = self.entity(row, col)
         if ITree.is_implemented_by(entity.__class__) and not entity.is_leaf():
-            self.w(u'<div class="folder">%s</div>' % entity.view('oneline'))
+            self.w(u'<div class="folder">%s</div>\n' % entity.view('oneline'))
         else:
             # XXX define specific CSS classes according to mime types
-            self.w(u'<div class="file">%s</div>' % entity.view('oneline'))
+            self.w(u'<div class="file">%s</div>\n' % entity.view('oneline'))
 
 
 class DefaultTreeViewItemView(EntityView):
@@ -68,7 +60,7 @@
     """
     id = 'treeitemview'
     accepts = ('Any',)
-    
+
     def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
         entity = self.entity(row, col)
         itemview = self.view(vid, self.rset, row=row, col=col)
@@ -80,7 +72,7 @@
 
 class TreeViewItemView(EntityView):
     """specific treeitem view for entities which implement ITree
-    
+
     (each item should be exandable if it's not a tree leaf)
     """
     id = 'treeitemview'
@@ -88,33 +80,50 @@
     #     the default treeitem view
     __selectors__ = (implement_interface, yes)
     accepts_interfaces = (ITree,)
-    
+    opening = 'man_a/a1'.split('/')
+
     def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
+        w = self.w
         entity = self.entity(row, col)
-        cssclasses = []
+        liclasses = []
         is_leaf = False
+        is_open = entity.name in self.opening
         if row == len(self.rset) - 1:
             is_leaf = True
         if not hasattr(entity, 'is_leaf') or entity.is_leaf():
-            if is_leaf : cssclasses.append('last')
-            self.w(u'<li class="%s">' % u' '.join(cssclasses))
+            if is_leaf : liclasses.append('last')
+            w(u'<li class="%s">' % u' '.join(liclasses))
         else:
             rql = entity.children_rql() % {'x': entity.eid}
             url = html_escape(self.build_url('json', rql=rql, vid=parentvid,
                                              pageid=self.req.pageid,
                                              subvid=vid,
                                              noautoload=True))
-            cssclasses.append('expandable')
-            divclasses = ['hitarea expandable-hitarea']
-            if is_leaf :
-                cssclasses.append('lastExpandable')
-                divclasses.append('lastExpandable-hitarea')
-            self.w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(cssclasses)))
-            self.w(u'<div class="%s"> </div>' % u' '.join(divclasses))
-                
+            if is_open:
+                liclasses.append('collapsable')
+            else:
+                liclasses.append('expandable')
+            divclasses = ['hitarea']
+            if is_open:
+                divclasses.append('collapsable-hitarea')
+            else:
+                divclasses.append('expandable-hitarea')
+            if is_leaf:
+                liclasses.append('lastExpandable')
+                if not is_open:
+                    divclasses.append('lastExpandable-hitarea')
+            if is_open:
+                w(u'<li class="%s">' % u' '.join(liclasses))
+            else:
+                w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(liclasses)))
+            w(u'<div class="%s"> </div>' % u' '.join(divclasses))
+
             # add empty <ul> because jquery's treeview plugin checks for
             # sublists presence
-            self.w(u'<ul class="placeholder"><li>place holder</li></ul>')
+            if not is_open:
+                w(u'<ul class="placeholder"><li>place holder</li></ul>')
+        # the local node info
         self.wview(vid, self.rset, row=row, col=col)
-        self.w(u'</li>')
-
+        if is_open: # recurse if needed
+            self.wview(parentvid, self.req.execute(rql))
+        w(u'</li>')