web/views/tabs.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 28 Jan 2009 15:50:14 +0100
changeset 498 fc7ce5f95dd9
parent 494 c47229797d0d
child 504 459e50669d96
permissions -rw-r--r--
[tabs] * monkey patch the default json controller to work around #80969 * rename the wrapper
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()
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    58
        activetab = cookie.get('active_tab')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    59
        if activetab is None:
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    60
            cookie['active_tab'] = default
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    61
            self.req.set_cookie(cookie, 'active_tab')
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
    62
            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
    63
        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
    64
            tab = activetab.value
494
c47229797d0d python 2.4 compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 490
diff changeset
    65
        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
    66
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    67
    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
    68
        selected_tabs = []
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    69
        for tab in tabs:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    70
            try:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    71
                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
    72
                selected_tabs.append(tab)
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    73
            except NoSelectableObject:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    74
                continue
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    75
        return selected_tabs
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    76
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
    77
    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
    78
        self.req.add_css('ui.tabs.css')
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
    79
        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
    80
        # 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
    81
        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
    82
        # 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
    83
        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
    84
        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
    85
 jQuery(document).ready(function() {
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    86
   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
    87
   set_tab('%(vid)s');
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    88
 });
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    89
 """ % {'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
    90
        'vid'      : active_tab})
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    91
        # build the html structure
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    92
        w = self.w
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    93
        w(u'<div id="entity-tabs">')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    94
        w(u'<ul>')
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    95
        for tab in tabs:
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    96
            w(u'<li>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    97
            w(u'<a href="#as-%s">' % tab)
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    98
            w(u'<span onclick="set_tab(\'%s\')">' % tab)
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    99
            w(self.req._(tab))
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   100
            w(u'</span>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   101
            w(u'</a>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   102
            w(u'</li>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   103
        w(u'</ul>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   104
        w(u'</div>')
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
   105
        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
   106
            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
   107
            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
   108
            w(u'</div>')
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   109
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   110
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   111
from cubicweb.web.views.basecontrollers import JSonController
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   112
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   113
def js_remember_active_tab(self, tabname):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   114
    cookie = self.req.get_cookie()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   115
    cookie['active_tab'] = tabname
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   116
    self.req.set_cookie(cookie, 'active_tab')
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   117
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   118
def js_lazily(self, vid_eid):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   119
    vid, eid = vid_eid.split('-')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   120
    rset = eid and self.req.eid_rset(eid) or None
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   121
    view = self.vreg.select_view(vid, self.req, rset)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   122
    return self._set_content_type(view, view.dispatch())
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   123
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   124
JSonController.js_remember_active_tab = js_remember_active_tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   125
JSonController.js_lazily = js_lazily
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   126
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   127
class EntityRelatedTab(EntityView):
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   128
    """A view you should inherit from leftmost,
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   129
    to wrap another actual view displaying entity related stuff.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   130
    Such a view _must_ provide the rtype, target and vid attributes :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   131
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   132
    Example :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   133
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   134
    class ProjectScreenshotsView(EntityRelationView):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   135
        "display project's screenshots"
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   136
        id = title = _('projectscreenshots')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   137
        accepts = ('Project',)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   138
        rtype = 'screenshot'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   139
        target = 'object'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   140
        vid = 'gallery'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   141
        __selectors__ = EntityRelationView.__selectors__ + (one_line_rset,)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   142
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   143
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   144
    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
   145
    Then, just define as below, and declare this being the tab content :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   146
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   147
    class ProjectScreenshotTab(DataDependantTab, ProjectScreenshotsView):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   148
        id = 'screenshots_tab'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   149
    """
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   150
    __selectors__ = EntityView.__selectors__ + (has_related_entities,)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   151
    vid = 'list'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   152
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   153
    def cell_call(self, row, col):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   154
        rset = self.rset.get_entity(row, col).related(self.rtype, role(self))
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   155
        self.w(u'<div class="mainInfo">')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   156
        self.wview(self.vid, rset, 'noresult')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   157
        self.w(u'</div>')