web/views/tabs.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 18 Jun 2009 21:01:55 +0200
changeset 2123 3e1d2ab5f8c0
parent 2058 7ef12c03447c
child 2293 7ded2a1416e4
child 2673 9e639925ca2f
permissions -rw-r--r--
replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
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
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1802
diff changeset
     4
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
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
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1802
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
235
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
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
     9
__docformat__ = "restructuredtext en"
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    10
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
838
f2c56312b03a rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 782
diff changeset
    14
from cubicweb.selectors import partial_has_related_entities
981
d86d1ee3b60e fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents: 940
diff changeset
    15
from cubicweb.view import EntityView
1135
00eb43688a57 pylint detected errors
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    16
from cubicweb.common import tags, uilib
1997
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    17
from cubicweb.utils import make_uid
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
    18
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
class LazyViewMixin(object):
510
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    20
    """provides two convenience methods for the tab machinery
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    21
    can also be used to lazy-load arbitrary views
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    22
    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
    23
    lazyload a view that in turns does the same
6b92504d77c1 [tabs] improve pylint note :)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 504
diff changeset
    24
    """
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
    25
559
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    26
    def _prepare_bindings(self, vid, reloadable):
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    27
        self.req.html_headers.add_onload(u"""
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    28
  jQuery('#lazy-%(vid)s').bind('%(event)s', function(event) {
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    29
     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
    30
  });""" % {'event': 'load_%s' % vid, 'vid': vid,
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    31
            'reloadable' : str(reloadable).lower()})
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    32
1000
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    33
    def lazyview(self, vid, rql=None, eid=None, rset=None, static=False,
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    34
                 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
    35
        """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
    36
        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
    37
        """
1000
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    38
        assert rql or eid or rset or static, \
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    39
            'lazyview wants at least : rql, or an eid, or an rset -- or call it with static=True'
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
    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')
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 1477
diff changeset
    42
        urlparams = {'vid' : vid, 'fname' : 'view'}
1000
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    43
        if rql:
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    44
            urlparams['rql'] = rql
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    45
        elif eid:
1135
00eb43688a57 pylint detected errors
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    46
            urlparams['rql'] = uilib.rql_for_eid(eid)
1000
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    47
        elif rset:
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    48
            urlparams['rql'] = rset.printable_rql()
543
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
    49
        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
    50
            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
    51
        if show_spinbox:
522
385ce5e0b30b cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents: 510
diff changeset
    52
            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
    53
              % (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
    54
        w(u'</div>')
559
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    55
        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
    56
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
    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
    58
        """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
    59
        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
    60
        """
559
7cfd3d11edc8 [tabs] prepare for reloadability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 547
diff changeset
    61
        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
    62
        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
    63
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
    64
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
    65
class TabsMixin(LazyViewMixin):
1079
452cb76fe07a backport typo fix
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1000
diff changeset
    66
    """a tab mixin
452cb76fe07a backport typo fix
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1000
diff changeset
    67
    """
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
    68
606
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    69
    @property
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    70
    def cookie_name(self):
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    71
        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
    72
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
    73
    def active_tab(self, tabs, default):
1000
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    74
        cookies = self.req.get_cookie()
606
40c7c2cf0797 [tabs] reuse the new set_cookie controller method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 559
diff changeset
    75
        cookiename = self.cookie_name
1000
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    76
        activetab = cookies.get(cookiename)
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
    77
        if activetab is None:
1000
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    78
            cookies[cookiename] = default
90705536b7c8 [tabs] from rev 917 - got lost in a merge
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 999
diff changeset
    79
            self.req.set_cookie(cookies, 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
    80
            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
    81
        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
    82
            tab = activetab.value
494
c47229797d0d python 2.4 compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 490
diff changeset
    83
        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
    84
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    85
    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
    86
        selected_tabs = []
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    87
        for tab in tabs:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    88
            try:
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1997
diff changeset
    89
                self.vreg.select('views', tab, self.req, rset=self.rset)
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    90
                selected_tabs.append(tab)
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    91
            except NoSelectableObject:
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    92
                continue
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    93
        return selected_tabs
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
    94
1997
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    95
    def render_tabs(self, tabs, default, entity=None):
1379
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
    96
        # tabbed views do no support concatenation
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
    97
        # hence we delegate to the default tab if there is more than on entity
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
    98
        # in the result set
1997
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    99
        if entity and len(self.rset) > 1:
1379
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   100
            entity.view(default, w=self.w)
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   101
            return
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   102
        # XXX (syt) fix below add been introduced at some point to fix something
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   103
        # (http://intranet.logilab.fr/jpl/ticket/32174 ?) but this is not a clean
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   104
        # way. We must not consider form['rql'] here since it introduces some
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   105
        # other failures on non rql queries (plain text, shortcuts,... handled by
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   106
        # magicsearch) which has a single result whose primary view is using tabs
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   107
        # (https://www.logilab.net/cwo/ticket/342789)
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   108
        #rql = self.req.form.get('rql')
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   109
        #if rql:
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   110
        #    self.req.execute(rql).get_entity(0,0).view(default, w=self.w)
6c6dbc0df829 MUST NOT consider req.form['rql'] in a view, use len(rset) > 1 to detect if we can apply tabs or not
sylvain.thenault@logilab.fr
parents: 1112
diff changeset
   111
        #    return
2123
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   112
        self.req.add_css('tabs-no-images.css')
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   113
        self.req.add_js(('jquery.tools.min.js', 'cubicweb.htmlhelpers.js',
522
385ce5e0b30b cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents: 510
diff changeset
   114
                         '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
   115
        # 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
   116
        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
   117
        # 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
   118
        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
   119
        # build the html structure
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   120
        w = self.w
1997
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   121
        if entity:
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   122
            w(u'<div id="entity-tabs-%s">' % entity.eid)
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   123
        else:
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   124
            uid = make_uid('tab')
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   125
            w(u'<div id="entity-tabs-%s">' % uid)
2123
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   126
        w(u'<ul class="css-tabs" id="tabs-%s">' % entity.eid)
214
aaf2957bf69e tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff changeset
   127
        for tab in tabs:
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   128
            w(u'<li>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   129
            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
   130
            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
   131
            w(self.req._(tab))
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   132
            w(u'</span>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   133
            w(u'</a>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   134
            w(u'</li>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   135
        w(u'</ul>')
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   136
        w(u'</div>')
2123
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   137
        w(u'<div id="panes-%s">' % entity.eid)
490
b84d64db2350 [tabs] prune tab set _before_ computing & setting 'active' tab
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 489
diff changeset
   138
        for tab in tabs:
2123
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   139
            w(u'<div>')
1997
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   140
            if entity:
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   141
                self.lazyview(tab, eid=entity.eid)
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   142
            else:
554eb4dd533d [views] get tabs to work with startup views
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
   143
                self.lazyview(tab, static=True)
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
   144
            w(u'</div>')
2123
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   145
        w(u'</div>')
543
c0f2b6378f70 simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 542
diff changeset
   146
        # 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
   147
        # 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
   148
        self.req.html_headers.add_onload(u"""
2123
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   149
    jQuery(function() {
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   150
      jQuery("#tabs-%(eeid)s").tabs("#panes-%(eeid)s > div", {initialIndex: %(tabindex)s});
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   151
      set_tab('%(vid)s', '%(cookiename)s');
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   152
    });""" % {'eeid' : entity.eid,
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   153
              'vid'  : active_tab,
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   154
              'cookiename' : self.cookie_name,
3e1d2ab5f8c0 replace tabs implementation (behaviour should be strictly identical, look is a bit different), using jquery.tools (see #343644)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   155
              'tabindex' : tabs.index(active_tab)})
235
b43362d92a1d rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 220
diff changeset
   156
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   157
982
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   158
class EntityRelationView(EntityView):
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   159
    """view displaying entity related stuff.
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   160
    Such a view _must_ provide the rtype, target and vid attributes :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   161
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   162
    Example :
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   163
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   164
    class ProjectScreenshotsView(EntityRelationView):
542
46d4ff5aa917 use lgc.decorators.monkeypatch in tabs.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 522
diff changeset
   165
        '''display project's screenshots'''
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   166
        id = title = _('projectscreenshots')
982
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   167
        __select__ = EntityRelationView.__select__ & implements('Project')
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   168
        rtype = 'screenshot'
982
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   169
        role = 'subject'
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   170
        vid = 'gallery'
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   171
982
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   172
    in this example, entities related to project entity by the'screenshot'
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   173
    relation (where the project is subject of the relation) will be displayed
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   174
    using the 'gallery' view.
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   175
    """
838
f2c56312b03a rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 782
diff changeset
   176
    __select__ = EntityView.__select__ & partial_has_related_entities()
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   177
    vid = 'list'
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1498
diff changeset
   178
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   179
    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
   180
        rset = self.entity(row, col).related(self.rtype, role(self))
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   181
        self.w(u'<div class="mainInfo">')
982
ba5257deec7d rename EntityRelatedTab into EntityRelationView, some fixes
sylvain.thenault@logilab.fr
parents: 981
diff changeset
   182
        if self.title:
1014
4792a1bb72a9 fix name error
sylvain.thenault@logilab.fr
parents: 1006
diff changeset
   183
            self.w(tags.h1(self.req._(self.title)))
489
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   184
        self.wview(self.vid, rset, 'noresult')
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 487
diff changeset
   185
        self.w(u'</div>')