web/views/tabs.py
branchtls-sprint
changeset 1006 92a0601b2523
parent 985 6a25c58a1c23
parent 1000 90705536b7c8
child 1014 4792a1bb72a9
equal deleted inserted replaced
1005:09ba3cb2a440 1006:92a0601b2523
    30   jQuery('#lazy-%(vid)s').bind('%(event)s', function(event) {
    30   jQuery('#lazy-%(vid)s').bind('%(event)s', function(event) {
    31      load_now('#lazy-%(vid)s', '#%(vid)s-hole', %(reloadable)s);
    31      load_now('#lazy-%(vid)s', '#%(vid)s-hole', %(reloadable)s);
    32   });""" % {'event': 'load_%s' % vid, 'vid': vid,
    32   });""" % {'event': 'load_%s' % vid, 'vid': vid,
    33             'reloadable' : str(reloadable).lower()})
    33             'reloadable' : str(reloadable).lower()})
    34 
    34 
    35     def lazyview(self, vid, eid=None, reloadable=False, show_spinbox=True, w=None):
    35     def lazyview(self, vid, rql=None, eid=None, rset=None, static=False,
       
    36                  reloadable=False, show_spinbox=True, w=None):
    36         """a lazy version of wview
    37         """a lazy version of wview
    37         first version only support lazy viewing for an entity at a time
    38         first version only support lazy viewing for an entity at a time
    38         """
    39         """
       
    40         assert rql or eid or rset or static, \
       
    41             'lazyview wants at least : rql, or an eid, or an rset -- or call it with static=True'
    39         w = w or self.w
    42         w = w or self.w
    40         self.req.add_js('cubicweb.lazy.js')
    43         self.req.add_js('cubicweb.lazy.js')
    41         urlparams = {'vid' : vid, 'mode' : 'html'}
    44         urlparams = {'vid' : vid, 'mode' : 'html'}
    42         if eid:
    45         if rql:
    43             urlparams['rql'] = uilib.rql_for_eid(eid)
    46             urlparams['rql'] = rql
       
    47         elif eid:
       
    48             urlparams['rql'] = rql_for_eid(eid)
       
    49         elif rset:
       
    50             urlparams['rql'] = rset.printable_rql()
    44         w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % (
    51         w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % (
    45             vid, html_escape(self.build_url('json', **urlparams))))
    52             vid, html_escape(self.build_url('json', **urlparams))))
    46         if show_spinbox:
    53         if show_spinbox:
    47             w(u'<img src="data/loading.gif" id="%s-hole" alt="%s"/>'
    54             w(u'<img src="data/loading.gif" id="%s-hole" alt="%s"/>'
    48               % (vid, self.req._('loading')))
    55               % (vid, self.req._('loading')))
    62     @property
    69     @property
    63     def cookie_name(self):
    70     def cookie_name(self):
    64         return str('%s_active_tab' % self.config.appid)
    71         return str('%s_active_tab' % self.config.appid)
    65 
    72 
    66     def active_tab(self, tabs, default):
    73     def active_tab(self, tabs, default):
    67         cookie = self.req.get_cookie()
    74         cookies = self.req.get_cookie()
    68         cookiename = self.cookie_name
    75         cookiename = self.cookie_name
    69         activetab = cookie.get(cookiename)
    76         activetab = cookies.get(cookiename)
    70         if activetab is None:
    77         if activetab is None:
    71             cookie[cookiename] = default
    78             cookies[cookiename] = default
    72             self.req.set_cookie(cookie, cookiename)
    79             self.req.set_cookie(cookies, cookiename)
    73             tab = default
    80             tab = default
    74         else:
    81         else:
    75             tab = activetab.value
    82             tab = activetab.value
    76         return tab in tabs and tab or default
    83         return tab in tabs and tab or default
    77 
    84 
    87 
    94 
    88     def render_tabs(self, tabs, default, entity):
    95     def render_tabs(self, tabs, default, entity):
    89         self.req.add_css('ui.tabs.css')
    96         self.req.add_css('ui.tabs.css')
    90         self.req.add_js(('ui.core.js', 'ui.tabs.js',
    97         self.req.add_js(('ui.core.js', 'ui.tabs.js',
    91                          'cubicweb.ajax.js', 'cubicweb.tabs.js', 'cubicweb.lazy.js'))
    98                          'cubicweb.ajax.js', 'cubicweb.tabs.js', 'cubicweb.lazy.js'))
       
    99         # tabbed views do no support concatenation
       
   100         # hence we delegate to the default tab
       
   101         if self.req.form.get('vid') == 'primary':
       
   102             entity.view(default)
       
   103             return
    92         # prune tabs : not all are to be shown
   104         # prune tabs : not all are to be shown
    93         tabs = self.prune_tabs(tabs)
   105         tabs = self.prune_tabs(tabs)
    94         # select a tab
   106         # select a tab
    95         active_tab = self.active_tab(tabs, default)
   107         active_tab = self.active_tab(tabs, default)
    96         # build the html structure
   108         # build the html structure
    97         w = self.w
   109         w = self.w
    98         w(u'<div id="entity-tabs">')
   110         w(u'<div id="entity-tabs-%s">' % entity.eid)
    99         w(u'<ul>')
   111         w(u'<ul>')
   100         for tab in tabs:
   112         for tab in tabs:
   101             w(u'<li>')
   113             w(u'<li>')
   102             w(u'<a href="#as-%s">' % tab)
   114             w(u'<a href="#as-%s">' % tab)
   103             w(u'<span onclick="set_tab(\'%s\', \'%s\')">' % (tab, self.cookie_name))
   115             w(u'<span onclick="set_tab(\'%s\', \'%s\')">' % (tab, self.cookie_name))
   107             w(u'</li>')
   119             w(u'</li>')
   108         w(u'</ul>')
   120         w(u'</ul>')
   109         w(u'</div>')
   121         w(u'</div>')
   110         for tab in tabs:
   122         for tab in tabs:
   111             w(u'<div id="as-%s">' % tab)
   123             w(u'<div id="as-%s">' % tab)
   112             self.lazyview(tab, entity.eid)
   124             self.lazyview(tab, eid=entity.eid)
   113             w(u'</div>')
   125             w(u'</div>')
   114         # call the set_tab() JS function *after* each tab is generated
   126         # call the set_tab() JS function *after* each tab is generated
   115         # because the callback binding needs to be done before
   127         # because the callback binding needs to be done before
   116         self.req.html_headers.add_onload(u"""
   128         self.req.html_headers.add_onload(u"""
   117    jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s });
   129    jQuery('#entity-tabs-%(eeid)s > ul').tabs( { selected: %(tabindex)s });
   118    set_tab('%(vid)s', '%(cookiename)s');
   130    set_tab('%(vid)s', '%(cookiename)s');
   119  """ % {'tabindex'   : tabs.index(active_tab),
   131  """ % {'tabindex'   : tabs.index(active_tab),
   120         'vid'        : active_tab,
   132         'vid'        : active_tab,
       
   133         'eeid'       : entity.eid,
   121         'cookiename' : self.cookie_name})
   134         'cookiename' : self.cookie_name})
   122 
   135 
   123 
   136 
   124 class EntityRelationView(EntityView):
   137 class EntityRelationView(EntityView):
   125     """view displaying entity related stuff.
   138     """view displaying entity related stuff.