merge treeview-tabs into default (for 3.1.1)
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 03 Mar 2009 15:06:03 +0100
changeset 999 999198995a53
parent 994 98f19cf755a1 (current diff)
parent 998 9c6ce9d6384f (diff)
child 1000 90705536b7c8
merge treeview-tabs into default (for 3.1.1)
web/views/tabs.py
web/views/treeview.py
--- a/web/views/tabs.py	Mon Mar 02 14:31:40 2009 +0100
+++ b/web/views/tabs.py	Tue Mar 03 15:06:03 2009 +0100
@@ -1,7 +1,7 @@
 """base classes to handle tabbed views
 
 :organization: Logilab
-:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 
@@ -90,6 +90,11 @@
         self.req.add_css('ui.tabs.css')
         self.req.add_js(('ui.core.js', 'ui.tabs.js',
                          'cubicweb.ajax.js', 'cubicweb.tabs.js', 'cubicweb.lazy.js'))
+        # tabbed views do no support concatenation
+        # hence we delegate to the default tab
+        if self.req.form.get('vid') == 'primary':
+            entity.view(default)
+            return
         # prune tabs : not all are to be shown
         tabs = self.prune_tabs(tabs)
         # select a tab
--- a/web/views/treeview.py	Mon Mar 02 14:31:40 2009 +0100
+++ b/web/views/treeview.py	Tue Mar 03 15:06:03 2009 +0100
@@ -1,10 +1,19 @@
+"""Set of tree-building widgets, based on jQuery treeview plugin
+
+:organization: Logilab
+:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
+"""
+__docformat__ = "restructuredtext en"
+import uuid
+
 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
+def treecookiename(treeid):
+    return str('treestate-%s' % treeid)
 
 class TreeView(EntityView):
     id = 'treeview'
@@ -12,27 +21,30 @@
     itemvid = 'treeitemview'
     css_classes = 'treeview widget'
     title = _('tree view')
-    
-    def call(self, subvid=None):
-        if subvid is None and 'subvid' in self.req.form:
-            subvid = self.req.form.pop('subvid') # consume it
+
+    def call(self, subvid=None, treeid=None, initial_load=True):
         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)
+            if 'subvid' in self.req.form:
+                subvid = self.req.form.pop('subvid') # consume it
+            else:
+                subvid = 'oneline'
+        if treeid is None:
+            if 'treeid' in self.req.form:
+                treeid = self.req.form.pop('treeid')
+            else:
+                treeid = uuid.uuid1().hex
+                self.warning('Tree state won\'t be properly restored after next reload')
+        if initial_load:
+            self.req.add_css('jquery.treeview.css')
+            self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js'))
+            self.req.html_headers.add_onload(u"""
+                 jQuery("#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)
+                       vid=subvid, parentvid=self.id, treeid=treeid)
         self.w(u'</ul>')
-        
 
 class FileTreeView(TreeView):
     """specific version of the treeview to display file trees
@@ -41,10 +53,8 @@
     css_classes = 'treeview widget filetree'
     title = _('file tree view')
 
-    def call(self, subvid=None):
-        super(FileTreeView, self).call(subvid='filetree-oneline')
-
-
+    def call(self, subvid=None, treeid=None, initial_load=True):
+        super(FileTreeView, self).call(treeid=treeid, subvid='filetree-oneline', initial_load=initial_load)
 
 class FileItemInnerView(EntityView):
     """inner view used by the TreeItemView instead of oneline view
@@ -57,10 +67,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,8 +78,9 @@
     """
     id = 'treeitemview'
     accepts = ('Any',)
-    
-    def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
+
+    def cell_call(self, row, col, vid='oneline', parentvid='treeview', treeid=None):
+        assert treeid is not None
         entity = self.entity(row, col)
         itemview = self.view(vid, self.rset, row=row, col=col)
         if row == len(self.rset) - 1:
@@ -80,7 +91,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 +99,83 @@
     #     the default treeitem view
     __selectors__ = (implement_interface, yes)
     accepts_interfaces = (ITree,)
-    
-    def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
+
+    def open_state(self, eeid, treeid):
+        cookies = self.req.get_cookie()
+        treestate = cookies.get(treecookiename(treeid))
+        if treestate:
+            return str(eeid) in treestate.value.split(';')
+        return False
+
+    def cell_call(self, row, col, treeid, vid='oneline', parentvid='treeview'):
+        w = self.w
         entity = self.entity(row, col)
-        cssclasses = []
+        liclasses = []
         is_leaf = False
-        if row == len(self.rset) - 1:
-            is_leaf = True
+        is_last = row == len(self.rset) - 1
+        is_open = self.open_state(entity.eid, treeid)
         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_last:
+                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))
-                
+                                             treeid=treeid,
+                                             subvid=vid))
+            divclasses = ['hitarea']
+            if is_open:
+                liclasses.append('collapsable')
+                divclasses.append('collapsable-hitarea')
+            else:
+                liclasses.append('expandable')
+                divclasses.append('closed-hitarea expandable-hitarea')
+            if is_last:
+                if is_open:
+                    liclasses.append('lastCollapsable')
+                    divclasses.append('lastCollapsable-hitarea')
+                else:
+                    liclasses.append('lastExpandable')
+                    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)))
+            if is_leaf:
+                divtail = ''
+            else:
+                divtail = ''' onclick="async_remote_exec('node_clicked', '%s', '%s')"''' % \
+                    (treeid, entity.eid)
+            w(u'<div class="%s"%s></div>' % (u' '.join(divclasses), divtail))
+
             # 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), treeid=treeid, initial_load=False)
+        w(u'</li>')
+
+from logilab.common.decorators import monkeypatch
+from cubicweb.web.views.basecontrollers import JSonController
 
+@monkeypatch(JSonController)
+def js_node_clicked(self, treeid, nodeeid):
+    """add/remove eid in treestate cookie"""
+    cookies = self.req.get_cookie()
+    statename = treecookiename(treeid)
+    treestate = cookies.get(statename)
+    if treestate is None:
+        cookies[statename] = nodeeid
+        self.req.set_cookie(cookies, statename)
+    else:
+        marked = set(filter(None, treestate.value.split(';')))
+        if nodeeid in marked:
+            marked.remove(nodeeid)
+        else:
+            marked.add(nodeeid)
+        cookies[statename] = ';'.join(marked)
+        self.req.set_cookie(cookies, statename)