web/views/tabs.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 28 Jan 2009 17:30:26 +0100
changeset 510 6b92504d77c1
parent 504 459e50669d96
child 522 385ce5e0b30b
permissions -rw-r--r--
[tabs] improve pylint note :)
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):
510
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    25
    """provides two convenience methods for the tab machinery
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    26
    can also be used to lazy-load arbitrary views
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    27
    caveat : lazyview is not recursive, i.e : you can't (successfully)
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    28
    lazyload a view that in turns does the same
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    29
    """
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
    30
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
    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
    32
        """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
    33
        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
    34
        """
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 = 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
    36
        self.req.add_js('cubicweb.lazy.js')
494
c47229797d0d python 2.4 compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 490
diff changeset
    37
        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
    38
        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
    39
        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
    40
            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
    41
        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
    42
        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
    43
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
    44
  $('#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
    45
     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
    46
  });});""" % {'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
    47
               '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
    48
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
    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
    50
        """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
    51
        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
    52
        """
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
        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
    54
        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
    55
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
    56
  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
    57
""" % {'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
    58
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
    59
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
    60
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
    61
    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
    62
        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
    63
        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
    64
        activetab = cookie.get(cookiename)
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    65
        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
    66
            cookie[cookiename] = default
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
    67
            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
    68
            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
    69
        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
    70
            tab = activetab.value
494
c47229797d0d python 2.4 compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 490
diff changeset
    71
        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
    72
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    73
    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
    74
        selected_tabs = []
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    75
        for tab in tabs:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    76
            try:
510
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    77
                self.vreg.select_view(tab, self.req, self.rset)
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    78
                selected_tabs.append(tab)
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    79
            except NoSelectableObject:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    80
                continue
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    81
        return selected_tabs
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    82
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
    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
    84
        self.req.add_css('ui.tabs.css')
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
    85
        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
    86
        # 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
    87
        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
    88
        # 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
    89
        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
    90
        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
    91
 jQuery(document).ready(function() {
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    92
   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
    93
   set_tab('%(vid)s');
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    94
 });
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    95
 """ % {'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
    96
        'vid'      : active_tab})
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    97
        # build the html structure
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    98
        w = self.w
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    99
        w(u'<div id="entity-tabs">')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   100
        w(u'<ul>')
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
   101
        for tab in tabs:
235
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'<a href="#as-%s">' % tab)
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   104
            w(u'<span onclick="set_tab(\'%s\')">' % tab)
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   105
            w(self.req._(tab))
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   106
            w(u'</span>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   107
            w(u'</a>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   108
            w(u'</li>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   109
        w(u'</ul>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   110
        w(u'</div>')
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
   111
        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
   112
            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
   113
            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
   114
            w(u'</div>')
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   115
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   116
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   117
from cubicweb.web.views.basecontrollers import JSonController
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   118
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   119
def js_remember_active_tab(self, tabname):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   120
    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
   121
    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
   122
    cookie[cookiename] = tabname
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
   123
    self.req.set_cookie(cookie, cookiename)
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   124
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   125
def js_lazily(self, vid_eid):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   126
    vid, eid = vid_eid.split('-')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   127
    rset = eid and self.req.eid_rset(eid) or None
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   128
    view = self.vreg.select_view(vid, self.req, rset)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   129
    return self._set_content_type(view, view.dispatch())
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   130
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   131
JSonController.js_remember_active_tab = js_remember_active_tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   132
JSonController.js_lazily = js_lazily
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   133
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   134
class EntityRelatedTab(EntityView):
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   135
    """A view you should inherit from leftmost,
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   136
    to wrap another actual view displaying entity related stuff.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   137
    Such a view _must_ provide the rtype, target and vid attributes :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   138
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   139
    Example :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   140
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   141
    class ProjectScreenshotsView(EntityRelationView):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   142
        "display project's screenshots"
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   143
        id = title = _('projectscreenshots')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   144
        accepts = ('Project',)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   145
        rtype = 'screenshot'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   146
        target = 'object'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   147
        vid = 'gallery'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   148
        __selectors__ = EntityRelationView.__selectors__ + (one_line_rset,)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   149
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   150
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   151
    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
   152
    Then, just define as below, and declare this being the tab content :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   153
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   154
    class ProjectScreenshotTab(DataDependantTab, ProjectScreenshotsView):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   155
        id = 'screenshots_tab'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   156
    """
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   157
    __selectors__ = EntityView.__selectors__ + (has_related_entities,)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   158
    vid = 'list'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   159
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   160
    def cell_call(self, row, col):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   161
        rset = self.rset.get_entity(row, col).related(self.rtype, role(self))
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   162
        self.w(u'<div class="mainInfo">')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   163
        self.wview(self.vid, rset, 'noresult')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   164
        self.w(u'</div>')