diff -r 4328bcbd748e -r 7c184924d492 web/views/treeview.py
--- a/web/views/treeview.py Mon Feb 23 11:30:38 2009 +0100
+++ b/web/views/treeview.py Mon Feb 23 12:12:59 2009 +0100
@@ -1,32 +1,38 @@
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, treeid=None):
+
+ def call(self, subvid=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'))
- # 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'
' % (treeid, self.css_classes))
+ 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'' % self.css_classes)
+ else:
+ self.w(u''
+ % 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'
')
+
class FileTreeView(TreeView):
"""specific version of the treeview to display file trees
@@ -38,6 +44,8 @@
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
@@ -49,10 +57,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'%s
\n' % entity.view('oneline'))
+ self.w(u'%s
' % entity.view('oneline'))
else:
# XXX define specific CSS classes according to mime types
- self.w(u'%s
\n' % entity.view('oneline'))
+ self.w(u'%s
' % entity.view('oneline'))
class DefaultTreeViewItemView(EntityView):
@@ -60,7 +68,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)
@@ -70,14 +78,9 @@
self.w(u'- %s
' % itemview)
-class TreeStateMixin(object):
-
- def open_state(self):
- raise NotImplementedError
-
-class TreeViewItemView(EntityView, TreeStateMixin):
+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'
@@ -85,80 +88,33 @@
# the default treeitem view
__selectors__ = (implement_interface, yes)
accepts_interfaces = (ITree,)
-
- def open_state(self):
- """implements TreeState mixin"""
- return ()
-
+
def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
- w = self.w
entity = self.entity(row, col)
- liclasses = []
+ cssclasses = []
is_leaf = False
- is_open = str(entity.eid) in self.open_state()
if row == len(self.rset) - 1:
is_leaf = True
if not hasattr(entity, 'is_leaf') or entity.is_leaf():
- if is_leaf : liclasses.append('last')
- w(u'- ' % u' '.join(liclasses))
+ if is_leaf : cssclasses.append('last')
+ self.w(u'
- ' % u' '.join(cssclasses))
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))
- 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'
- ' % u' '.join(liclasses))
- else:
- w(u'
- ' % (url, u' '.join(liclasses)))
- if is_leaf:
- divtail = ''
- else:
- divtail = ''' onclick="async_remote_exec('node_clicked', %s)"''' % entity.eid
- w(u'' % (u' '.join(divclasses), divtail))
-
+ cssclasses.append('expandable')
+ divclasses = ['hitarea expandable-hitarea']
+ if is_leaf :
+ cssclasses.append('lastExpandable')
+ divclasses.append('lastExpandable-hitarea')
+ self.w(u'
- ' % (url, u' '.join(cssclasses)))
+ self.w(u'
' % u' '.join(divclasses))
+
# add empty because jquery's treeview plugin checks for
# sublists presence
- if not is_open:
- w(u'')
- # the local node info
+ self.w(u'')
self.wview(vid, self.rset, row=row, col=col)
- if is_open: # recurse if needed
- self.wview(parentvid, self.req.execute(rql))
- w(u'
')
-
-from logilab.common.decorators import monkeypatch
-from cubicweb.web.views.basecontrollers import JSonController
+ self.w(u'')
-@monkeypatch(JSonController)
-def js_node_clicked(self, eid):
- """add/remove eid in treestate cookie
- XXX this deals with one tree per page
- also check the treeid issue above
- """
- cookies = self.req.get_cookie()
- treestate = cookies.get('treestate')
- if treestate is None:
- cookies['treestate'] = str(eid)
- self.req.set_cookie(cookies, 'treestate')
- else:
- marked = set(treestate.value.split(';'))
- if eid in marked:
- marked.remove(eid)
- else:
- marked.add(eid)
- cookies['treestate'] = ';'.join(str(x) for x in marked)
- self.req.set_cookie(cookies, 'treestate')