web/views/treeview.py
changeset 0 b97547f5f1fa
child 150 1190261a1f13
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 from logilab.mtconverter import html_escape
       
     2 
       
     3 from cubicweb.interfaces import ITree
       
     4 from cubicweb.common.selectors import interface_selector, yes_selector
       
     5 from cubicweb.common.view import EntityView
       
     6 
       
     7 class TreeView(EntityView):
       
     8     id = 'treeview'
       
     9     accepts = ('Any',)
       
    10     
       
    11     def call(self, subvid=None):
       
    12         if subvid is None and 'subvid' in self.req.form:
       
    13             subvid = self.req.form.pop('subvid') # consume it
       
    14         if subvid is None:
       
    15             subvid = 'oneline'
       
    16         self.req.add_css('jquery.treeview.css')
       
    17         self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js', 'cubicweb.widgets.js'))
       
    18         self.w(u'<ul class="treeview widget" cubicweb:loadtype="auto" cubicweb:wdgtype="TreeView">')
       
    19         for rowidx in xrange(len(self.rset)):
       
    20             self.wview('treeitemview', self.rset, row=rowidx, col=0, vid=subvid)
       
    21         self.w(u'</ul>')
       
    22 
       
    23 
       
    24 class DefaultTreeViewItemView(EntityView):
       
    25     """default treeitem view for entities which don't implement ITree
       
    26     """
       
    27     id = 'treeitemview'
       
    28     accepts = ('Any',)
       
    29     
       
    30     def cell_call(self, row, col, vid='oneline'):
       
    31         entity = self.entity(row, col)
       
    32         itemview = self.view(vid, self.rset, row=row, col=col)
       
    33         if row == len(self.rset) - 1:
       
    34             self.w(u'<li class="last">%s</li>' % itemview)
       
    35         else:
       
    36             self.w(u'<li>%s</li>' % itemview)
       
    37 
       
    38 
       
    39 class TreeViewItemView(EntityView):
       
    40     """specific treeitem view for entities which implement ITree
       
    41     
       
    42     (each item should be exandable if it's not a tree leaf)
       
    43     """
       
    44     id = 'treeitemview'
       
    45     # XXX append yes_selector to make sure we get an higher score than
       
    46     #     the default treeitem view
       
    47     __selectors__ = (interface_selector, yes_selector)
       
    48     accepts_interfaces = (ITree,)
       
    49     
       
    50     def cell_call(self, row, col, vid='oneline'):
       
    51         entity = self.entity(row, col)
       
    52         cssclasses = []
       
    53         is_leaf = False
       
    54         if row == len(self.rset) - 1:
       
    55             is_leaf = True
       
    56         if not hasattr(entity, 'is_leaf') or entity.is_leaf():
       
    57             if is_leaf : cssclasses.append('last')
       
    58             self.w(u'<li class="%s">' % u' '.join(cssclasses))
       
    59         else:
       
    60             rql = entity.children_rql() % {'x': entity.eid}
       
    61             url = html_escape(self.build_url('json', rql=rql, vid='treeview',
       
    62                                              pageid=self.req.pageid,
       
    63                                              subvid=vid))
       
    64             cssclasses.append('expandable')
       
    65             divclasses = ['hitarea expandable-hitarea']
       
    66             if is_leaf :
       
    67                 cssclasses.append('lastExpandable')
       
    68                 divclasses.append('lastExpandable-hitarea')
       
    69             self.w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(cssclasses)))
       
    70             self.w(u'<div class="%s"> </div>' % u' '.join(divclasses))
       
    71                 
       
    72             # add empty <ul> because jquery's treeview plugin checks for
       
    73             # sublists presence
       
    74             self.w(u'<ul class="placeholder"><li>place holder</li></ul>')
       
    75         self.wview(vid, self.rset, row=row, col=col)
       
    76         self.w(u'</li>')
       
    77