web/views/tabs.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 18 Feb 2009 13:36:28 +0100
branchtls-sprint
changeset 782 01801a10c567
parent 771 ae1b0081ff30
child 838 f2c56312b03a
permissions -rw-r--r--
introduce abstract selectors to get rid of the my_selector() contagion
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
692
800592b8d39b replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
     4
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
235
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
542
46d4ff5aa917 use lgc.decorators.monkeypatch in tabs.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 522
diff changeset
    10
from logilab.common.decorators import monkeypatch
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    11
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
    12
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    13
from cubicweb import NoSelectableObject, role
782
01801a10c567 introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 771
diff changeset
    14
from cubicweb.selectors import abstract_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
from cubicweb.common.view import EntityView
542
46d4ff5aa917 use lgc.decorators.monkeypatch in tabs.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 522
diff changeset
    16
from cubicweb.common.utils import HTMLHead
46d4ff5aa917 use lgc.decorators.monkeypatch in tabs.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 522
diff changeset
    17
from cubicweb.common.uilib import rql_for_eid
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
    18
542
46d4ff5aa917 use lgc.decorators.monkeypatch in tabs.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 522
diff changeset
    19
from cubicweb.web.views.basecontrollers import JSonController
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
    20
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
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
class LazyViewMixin(object):
510
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    23
    """provides two convenience methods for the tab machinery
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    24
    can also be used to lazy-load arbitrary views
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    25
    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
    26
    lazyload a view that in turns does the same
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    27
    """
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
    28
559
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    29
    def _prepare_bindings(self, vid, reloadable):
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    30
        self.req.html_headers.add_onload(u"""
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    31
  jQuery('#lazy-%(vid)s').bind('%(event)s', function(event) {
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    32
     load_now('#lazy-%(vid)s', '#%(vid)s-hole', %(reloadable)s);
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    33
  });""" % {'event': 'load_%s' % vid, 'vid': vid,
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    34
            'reloadable' : str(reloadable).lower()})
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    35
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    36
    def lazyview(self, vid, eid=None, reloadable=False, show_spinbox=True, w=None):
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
    37
        """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
    38
        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
    39
        """
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 = 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
    41
        self.req.add_js('cubicweb.lazy.js')
545
73aab1d2a27a [tabs] fix the no-eid case, remove unused code
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 544
diff changeset
    42
        urlparams = {'vid' : vid, 'mode' : 'html'}
73aab1d2a27a [tabs] fix the no-eid case, remove unused code
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 544
diff changeset
    43
        if eid:
73aab1d2a27a [tabs] fix the no-eid case, remove unused code
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 544
diff changeset
    44
            urlparams['rql'] = rql_for_eid(eid)
543
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
    45
        w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % (
545
73aab1d2a27a [tabs] fix the no-eid case, remove unused code
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 544
diff changeset
    46
            vid, html_escape(self.build_url('json', **urlparams))))
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
    47
        if show_spinbox:
522
385ce5e0b30b cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents: 510
diff changeset
    48
            w(u'<img src="data/loading.gif" id="%s-hole" alt="%s"/>'
385ce5e0b30b cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents: 510
diff changeset
    49
              % (vid, self.req._('loading')))
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
    50
        w(u'</div>')
559
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    51
        self._prepare_bindings(vid, reloadable)
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
    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
    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
    54
        """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
    55
        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
    56
        """
559
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    57
        self.req.add_js('cubicweb.lazy.js')
544
890e8d016d12 fix js syntax in tabs.forceview()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 543
diff changeset
    58
        self.req.html_headers.add_onload("trigger_load('%s');" % vid)
543
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
    59
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
    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
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
    62
606
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    63
    @property
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    64
    def cookie_name(self):
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    65
        return str('%s_active_tab' % self.config.appid)
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    66
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
    67
    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
    68
        cookie = self.req.get_cookie()
606
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    69
        cookiename = self.cookie_name
504
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
    70
        activetab = cookie.get(cookiename)
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    71
        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
    72
            cookie[cookiename] = default
459e50669d96 [tabs] cookie name par cube instance will reduce cookie collisions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 498
diff changeset
    73
            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
    74
            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
    75
        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
    76
            tab = activetab.value
494
c47229797d0d python 2.4 compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 490
diff changeset
    77
        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
    78
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    79
    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
    80
        selected_tabs = []
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    81
        for tab in tabs:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    82
            try:
510
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    83
                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
    84
                selected_tabs.append(tab)
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    85
            except NoSelectableObject:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    86
                continue
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    87
        return selected_tabs
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    88
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
    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
    90
        self.req.add_css('ui.tabs.css')
522
385ce5e0b30b cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents: 510
diff changeset
    91
        self.req.add_js(('ui.core.js', 'ui.tabs.js',
385ce5e0b30b cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents: 510
diff changeset
    92
                         'cubicweb.ajax.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
    93
        # 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
    94
        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
    95
        # 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
    96
        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
    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)
606
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
   104
            w(u'<span onclick="set_tab(\'%s\', \'%s\')">' % (tab, self.cookie_name))
235
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>')
543
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
   115
        # call the set_tab() JS function *after* each tab is generated
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
   116
        # because the callback binding needs to be done before
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
   117
        self.req.html_headers.add_onload(u"""
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
   118
   jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s });
606
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
   119
   set_tab('%(vid)s', '%(cookiename)s');
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
   120
 """ % {'tabindex'   : tabs.index(active_tab),
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
   121
        'vid'        : active_tab,
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
   122
        'cookiename' : self.cookie_name})
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   123
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   124
498
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 494
diff changeset
   125
class EntityRelatedTab(EntityView):
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   126
    """A view you should inherit from leftmost,
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   127
    to wrap another actual view displaying entity related stuff.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   128
    Such a view _must_ provide the rtype, target and vid attributes :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   129
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   130
    Example :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   131
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   132
    class ProjectScreenshotsView(EntityRelationView):
542
46d4ff5aa917 use lgc.decorators.monkeypatch in tabs.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 522
diff changeset
   133
        '''display project's screenshots'''
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   134
        id = title = _('projectscreenshots')
728
a95b284150d1 first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
   135
        __select__ = implements('Project')
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   136
        rtype = 'screenshot'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   137
        target = 'object'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   138
        vid = 'gallery'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   139
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   140
    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
   141
    Then, just define as below, and declare this being the tab content :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   142
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   143
    class ProjectScreenshotTab(DataDependantTab, ProjectScreenshotsView):
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   144
        id = 'screenshots_tab'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   145
    """
782
01801a10c567 introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 771
diff changeset
   146
    __select__ = EntityView.__select__ & abstract_has_related_entities()
01801a10c567 introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 771
diff changeset
   147
                                                          
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   148
    vid = 'list'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   149
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   150
    def cell_call(self, row, col):
542
46d4ff5aa917 use lgc.decorators.monkeypatch in tabs.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 522
diff changeset
   151
        rset = self.entity(row, col).related(self.rtype, role(self))
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   152
        self.w(u'<div class="mainInfo">')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   153
        self.wview(self.vid, rset, 'noresult')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   154
        self.w(u'</div>')