web/views/tabs.py
changeset 1997 554eb4dd533d
parent 1977 606923dff11b
child 2058 7ef12c03447c
child 2258 79bc598c6411
equal deleted inserted replaced
1996:2490e18fd3c8 1997:554eb4dd533d
    12 
    12 
    13 from cubicweb import NoSelectableObject, role
    13 from cubicweb import NoSelectableObject, role
    14 from cubicweb.selectors import partial_has_related_entities
    14 from cubicweb.selectors import partial_has_related_entities
    15 from cubicweb.view import EntityView
    15 from cubicweb.view import EntityView
    16 from cubicweb.common import tags, uilib
    16 from cubicweb.common import tags, uilib
    17 
    17 from cubicweb.utils import make_uid
    18 
    18 
    19 class LazyViewMixin(object):
    19 class LazyViewMixin(object):
    20     """provides two convenience methods for the tab machinery
    20     """provides two convenience methods for the tab machinery
    21     can also be used to lazy-load arbitrary views
    21     can also be used to lazy-load arbitrary views
    22     caveat : lazyview is not recursive, i.e : you can't (successfully)
    22     caveat : lazyview is not recursive, i.e : you can't (successfully)
    90                 selected_tabs.append(tab)
    90                 selected_tabs.append(tab)
    91             except NoSelectableObject:
    91             except NoSelectableObject:
    92                 continue
    92                 continue
    93         return selected_tabs
    93         return selected_tabs
    94 
    94 
    95     def render_tabs(self, tabs, default, entity):
    95     def render_tabs(self, tabs, default, entity=None):
    96         # tabbed views do no support concatenation
    96         # tabbed views do no support concatenation
    97         # hence we delegate to the default tab if there is more than on entity
    97         # hence we delegate to the default tab if there is more than on entity
    98         # in the result set
    98         # in the result set
    99         if len(self.rset) > 1:
    99         if entity and len(self.rset) > 1:
   100             entity.view(default, w=self.w)
   100             entity.view(default, w=self.w)
   101             return
   101             return
   102         # XXX (syt) fix below add been introduced at some point to fix something
   102         # XXX (syt) fix below add been introduced at some point to fix something
   103         # (http://intranet.logilab.fr/jpl/ticket/32174 ?) but this is not a clean
   103         # (http://intranet.logilab.fr/jpl/ticket/32174 ?) but this is not a clean
   104         # way. We must not consider form['rql'] here since it introduces some
   104         # way. We must not consider form['rql'] here since it introduces some
   116         tabs = self.prune_tabs(tabs)
   116         tabs = self.prune_tabs(tabs)
   117         # select a tab
   117         # select a tab
   118         active_tab = self.active_tab(tabs, default)
   118         active_tab = self.active_tab(tabs, default)
   119         # build the html structure
   119         # build the html structure
   120         w = self.w
   120         w = self.w
   121         w(u'<div id="entity-tabs-%s">' % entity.eid)
   121         if entity:
       
   122             w(u'<div id="entity-tabs-%s">' % entity.eid)
       
   123         else:
       
   124             uid = make_uid('tab')
       
   125             w(u'<div id="entity-tabs-%s">' % uid)
   122         w(u'<ul>')
   126         w(u'<ul>')
   123         for tab in tabs:
   127         for tab in tabs:
   124             w(u'<li>')
   128             w(u'<li>')
   125             w(u'<a href="#as-%s">' % tab)
   129             w(u'<a href="#as-%s">' % tab)
   126             w(u'<span onclick="set_tab(\'%s\', \'%s\')">' % (tab, self.cookie_name))
   130             w(u'<span onclick="set_tab(\'%s\', \'%s\')">' % (tab, self.cookie_name))
   130             w(u'</li>')
   134             w(u'</li>')
   131         w(u'</ul>')
   135         w(u'</ul>')
   132         w(u'</div>')
   136         w(u'</div>')
   133         for tab in tabs:
   137         for tab in tabs:
   134             w(u'<div id="as-%s">' % tab)
   138             w(u'<div id="as-%s">' % tab)
   135             self.lazyview(tab, eid=entity.eid)
   139             if entity:
       
   140                 self.lazyview(tab, eid=entity.eid)
       
   141             else:
       
   142                 self.lazyview(tab, static=True)
   136             w(u'</div>')
   143             w(u'</div>')
   137         # call the set_tab() JS function *after* each tab is generated
   144         # call the set_tab() JS function *after* each tab is generated
   138         # because the callback binding needs to be done before
   145         # because the callback binding needs to be done before
   139         self.req.html_headers.add_onload(u"""
   146         self.req.html_headers.add_onload(u"""
   140    jQuery('#entity-tabs-%(eeid)s > ul').tabs( { selected: %(tabindex)s });
   147    jQuery('#entity-tabs-%(eeid)s > ul').tabs( { selected: %(tabindex)s });
   141    set_tab('%(vid)s', '%(cookiename)s');
   148    set_tab('%(vid)s', '%(cookiename)s');
   142  """ % {'tabindex'   : tabs.index(active_tab),
   149  """ % {'tabindex'   : tabs.index(active_tab),
   143         'vid'        : active_tab,
   150         'vid'        : active_tab,
   144         'eeid'       : entity.eid,
   151         'eeid'       : (entity and entity.eid or uid),
   145         'cookiename' : self.cookie_name})
   152         'cookiename' : self.cookie_name})
   146 
   153 
   147 
   154 
   148 class EntityRelationView(EntityView):
   155 class EntityRelationView(EntityView):
   149     """view displaying entity related stuff.
   156     """view displaying entity related stuff.