web/views/tabs.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 28 Jan 2009 16:58:05 +0100
changeset 504 459e50669d96
parent 498 fc7ce5f95dd9
child 510 6b92504d77c1
permissions -rw-r--r--
[tabs] cookie name par cube instance will reduce cookie collisions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     1
"""base classes to handle tabbed views
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     2
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     3
:organization: Logilab
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     4
:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     6
"""
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     7
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     8
__docformat__ = "restructuredtext en"
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     9
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    10
from logilab.mtconverter import html_escape
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    11
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    12
from cubicweb import NoSelectableObject, role
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    13
from cubicweb.common.view import EntityView
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
    14
from cubicweb.common.selectors import has_related_entities
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    15
485
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    16
from cubicweb.common.utils import HTMLHead
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    17
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    18
# the prepend hack only work for 1-level lazy views
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    19
# a whole lot different thing must be done otherwise
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    20
def prepend_post_inline_script(self, content):
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    21
    self.post_inlined_scripts.insert(0, content)
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    22
HTMLHead.prepend_post_inline_script = prepend_post_inline_script
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    23
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    24
class LazyViewMixin(object):
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    25
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    26
    def lazyview(self, vid, eid=None, show_spinbox=True, w=None):
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    27
        """a lazy version of wview
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    28
        first version only support lazy viewing for an entity at a time
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    29
        """
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    30
        w = w or self.w
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    31
        self.req.add_js('cubicweb.lazy.js')
494
c47229797d0d python 2.4 compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 490
diff changeset
    32
        eid = eid or ''
485
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    33
        w(u'<div id="lazy-%s" cubicweb__loadurl="%s-%s">' % (vid, vid, eid))
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    34
        if show_spinbox:
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    35
            w(u'<img src="data/loading.gif" id="%s-hole"/>' % vid)
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    36
        w(u'</div>')
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    37
        self.req.html_headers.prepend_post_inline_script(u"""
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    38
jQuery(document).ready(function () {
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    39
  $('#lazy-%(vid)s').bind('%(event)s', function(event) {
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    40
     load_now('#lazy-%(vid)s', '#%(vid)s-hole');
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    41
  });});""" % {'event' : 'load_%s' % vid,
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    42
               'vid' : vid})
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    43
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    44
    def forceview(self, vid):
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    45
        """trigger an event that will force immediate loading of the view
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    46
        on dom readyness
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    47
        """
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    48
        self.req.add_js('.lazy.js')
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    49
        self.req.html_headers.add_post_inline_script(u"""
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    50
jQuery(document).ready(function() {
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    51
  trigger_load('%(vid)s');})
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    52
""" % {'vid' : vid})
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    53
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    54
class TabsMixin(LazyViewMixin):
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    55
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    56
    def active_tab(self, tabs, default):
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    57
        cookie = self.req.get_cookie()
504
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
    58
        cookiename = '%s_active_tab' % self.config.appid
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
    59
        activetab = cookie.get(cookiename)
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    60
        if activetab is None:
504
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
    61
            cookie[cookiename] = default
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
    62
            self.req.set_cookie(cookie, cookiename)
487
355356b781d7 [tabs] avoid nasty surprises when tab name change & the cookie remembers the past
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 485
diff changeset
    63
            tab = default
355356b781d7 [tabs] avoid nasty surprises when tab name change & the cookie remembers the past
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 485
diff changeset
    64
        else:
355356b781d7 [tabs] avoid nasty surprises when tab name change & the cookie remembers the past
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 485
diff changeset
    65
            tab = activetab.value
494
c47229797d0d python 2.4 compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 490
diff changeset
    66
        return tab in tabs and tab or default
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    67
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    68
    def prune_tabs(self, tabs):
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    69
        selected_tabs = []
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    70
        for tab in tabs:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    71
            try:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    72
                tabview = self.vreg.select_view(tab, self.req, self.rset)
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    73
                selected_tabs.append(tab)
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    74
            except NoSelectableObject:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    75
                continue
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    76
        return selected_tabs
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    77
485
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    78
    def render_tabs(self, tabs, default, entity):
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    79
        self.req.add_css('ui.tabs.css')
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
    80
        self.req.add_js(('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js', 'cubicweb.lazy.js'))
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    81
        # prune tabs : not all are to be shown
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    82
        tabs = self.prune_tabs(tabs)
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    83
        # select a tab
485
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
    84
        active_tab = self.active_tab(tabs, default)
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    85
        self.req.html_headers.add_post_inline_script(u"""
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    86
 jQuery(document).ready(function() {
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    87
   jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s });
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    88
   set_tab('%(vid)s');
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    89
 });
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    90
 """ % {'tabindex' : tabs.index(active_tab),
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    91
        'vid'      : active_tab})
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    92
        # build the html structure
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    93
        w = self.w
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    94
        w(u'<div id="entity-tabs">')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    95
        w(u'<ul>')
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    96
        for tab in tabs:
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    97
            w(u'<li>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    98
            w(u'<a href="#as-%s">' % tab)
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    99
            w(u'<span onclick="set_tab(\'%s\')">' % tab)
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   100
            w(self.req._(tab))
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   101
            w(u'</span>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   102
            w(u'</a>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   103
            w(u'</li>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   104
        w(u'</ul>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   105
        w(u'</div>')
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
   106
        for tab in tabs:
485
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
   107
            w(u'<div id="as-%s">' % tab)
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
   108
            self.lazyview(tab, entity.eid)
0f830732be19 [tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 235
diff changeset
   109
            w(u'</div>')
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   110
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   111
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   112
from cubicweb.web.views.basecontrollers import JSonController
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   113
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   114
def js_remember_active_tab(self, tabname):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   115
    cookie = self.req.get_cookie()
504
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
   116
    cookiename = '%s_active_tab' % self.config.appid
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
   117
    cookie[cookiename] = tabname
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
   118
    self.req.set_cookie(cookie, cookiename)
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   119
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   120
def js_lazily(self, vid_eid):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   121
    vid, eid = vid_eid.split('-')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   122
    rset = eid and self.req.eid_rset(eid) or None
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   123
    view = self.vreg.select_view(vid, self.req, rset)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   124
    return self._set_content_type(view, view.dispatch())
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   125
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   126
JSonController.js_remember_active_tab = js_remember_active_tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   127
JSonController.js_lazily = js_lazily
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   128
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   129
class EntityRelatedTab(EntityView):
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   130
    """A view you should inherit from leftmost,
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   131
    to wrap another actual view displaying entity related stuff.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   132
    Such a view _must_ provide the rtype, target and vid attributes :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   133
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   134
    Example :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   135
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   136
    class ProjectScreenshotsView(EntityRelationView):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   137
        "display project's screenshots"
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   138
        id = title = _('projectscreenshots')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   139
        accepts = ('Project',)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   140
        rtype = 'screenshot'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   141
        target = 'object'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   142
        vid = 'gallery'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   143
        __selectors__ = EntityRelationView.__selectors__ + (one_line_rset,)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   144
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   145
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   146
    This is the view we want to have in a tab, only if there is something to show.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   147
    Then, just define as below, and declare this being the tab content :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   148
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   149
    class ProjectScreenshotTab(DataDependantTab, ProjectScreenshotsView):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   150
        id = 'screenshots_tab'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   151
    """
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   152
    __selectors__ = EntityView.__selectors__ + (has_related_entities,)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   153
    vid = 'list'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   154
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   155
    def cell_call(self, row, col):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   156
        rset = self.rset.get_entity(row, col).related(self.rtype, role(self))
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   157
        self.w(u'<div class="mainInfo">')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   158
        self.wview(self.vid, rset, 'noresult')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   159
        self.w(u'</div>')