web/views/treeview.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 23 Feb 2009 19:22:59 +0100
branchtreeview-tabs
changeset 931 7b701df4dbef
parent 930 bab7f79fa26b
child 939 ad72e06320e2
child 998 9c6ce9d6384f
permissions -rw-r--r--
[treeview] don't readd stuff to the header, poslish css classes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     1
"""Set of tree-building widgets, based on jQuery treeview plugin
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     2
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     3
:organization: Logilab
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     6
"""
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     7
__docformat__ = "restructuredtext en"
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     8
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
from logilab.mtconverter import html_escape
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
from cubicweb.interfaces import ITree
431
18b4dd650ef8 massive selector update
sylvain.thenault@logilab.fr
parents: 237
diff changeset
    11
from cubicweb.common.selectors import implement_interface, yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from cubicweb.common.view import EntityView
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    14
def treecookiename(treeid):
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    15
    return str('treestate-%s' % treeid)
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    16
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
class TreeView(EntityView):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
    id = 'treeview'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
    accepts = ('Any',)
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    20
    itemvid = 'treeitemview'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    21
    css_classes = 'treeview widget'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    22
    title = _('tree view')
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
    23
931
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    24
    def call(self, subvid=None, treeid=None, initial_load=True):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
        if subvid is None and 'subvid' in self.req.form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
            subvid = self.req.form.pop('subvid') # consume it
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
        if subvid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
            subvid = 'oneline'
929
80de412e6ed7 [treeview] treeid is passed along: we can manage as many trees as we want
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 928
diff changeset
    29
        if treeid is None and 'treeid' in self.req.form:
80de412e6ed7 [treeview] treeid is passed along: we can manage as many trees as we want
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 928
diff changeset
    30
            treeid = self.req.form.pop('treeid')
80de412e6ed7 [treeview] treeid is passed along: we can manage as many trees as we want
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 928
diff changeset
    31
        assert treeid is not None
931
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    32
        if initial_load:
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    33
            self.req.add_css('jquery.treeview.css')
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    34
            self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js'))
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    35
            self.req.html_headers.add_onload(u"""
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    36
                 jQuery("#tree-%s").treeview({toggle: toggleTree,
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    37
                                              prerendered: true});""" % treeid)
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
    38
        self.w(u'<ul id="tree-%s" class="%s">' % (treeid, self.css_classes))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        for rowidx in xrange(len(self.rset)):
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    40
            self.wview(self.itemvid, self.rset, row=rowidx, col=0,
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    41
                       vid=subvid, parentvid=self.id, treeid=treeid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
        self.w(u'</ul>')
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    43
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    44
class FileTreeView(TreeView):
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    45
    """specific version of the treeview to display file trees
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    46
    """
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    47
    id = 'filetree'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    48
    css_classes = 'treeview widget filetree'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    49
    title = _('file tree view')
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    50
931
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    51
    def call(self, subvid=None, treeid=None, initial_load=True):
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
    52
        super(FileTreeView, self).call(treeid=treeid, subvid='filetree-oneline', initial_load=initial_load)
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    53
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    54
class FileItemInnerView(EntityView):
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    55
    """inner view used by the TreeItemView instead of oneline view
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    56
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    57
    This view adds an enclosing <span> with some specific CSS classes
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    58
    around the oneline view. This is needed by the jquery treeview plugin.
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    59
    """
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    60
    id = 'filetree-oneline'
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    61
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    62
    def cell_call(self, row, col):
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    63
        entity = self.entity(row, col)
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    64
        if ITree.is_implemented_by(entity.__class__) and not entity.is_leaf():
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
    65
            self.w(u'<div class="folder">%s</div>\n' % entity.view('oneline'))
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    66
        else:
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    67
            # XXX define specific CSS classes according to mime types
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
    68
            self.w(u'<div class="file">%s</div>\n' % entity.view('oneline'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
class DefaultTreeViewItemView(EntityView):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    """default treeitem view for entities which don't implement ITree
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    id = 'treeitemview'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    accepts = ('Any',)
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
    76
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    77
    def cell_call(self, row, col, vid='oneline', parentvid='treeview', treeid=None):
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    78
        assert treeid is not None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        entity = self.entity(row, col)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        itemview = self.view(vid, self.rset, row=row, col=col)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        if row == len(self.rset) - 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
            self.w(u'<li class="last">%s</li>' % itemview)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        else:
472
958805c342b6 xhtml compliance
sylvain.thenault@logilab.fr
parents: 465
diff changeset
    84
            self.w(u'<li>%s</li>' % itemview)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    87
class TreeViewItemView(EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
    """specific treeitem view for entities which implement ITree
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
    89
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    (each item should be exandable if it's not a tree leaf)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    id = 'treeitemview'
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 187
diff changeset
    93
    # XXX append yes to make sure we get an higher score than
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    #     the default treeitem view
431
18b4dd650ef8 massive selector update
sylvain.thenault@logilab.fr
parents: 237
diff changeset
    95
    __selectors__ = (implement_interface, yes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    accepts_interfaces = (ITree,)
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    97
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    98
    def open_state(self, eeid, treeid):
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    99
        cookies = self.req.get_cookie()
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   100
        treestate = cookies.get(treecookiename(treeid))
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   101
        if treestate:
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   102
            return str(eeid) in treestate.value.split(';')
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   103
        return False
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   104
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   105
    def cell_call(self, row, col, treeid, vid='oneline', parentvid='treeview'):
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   106
        w = self.w
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        entity = self.entity(row, col)
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   108
        liclasses = []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
        is_leaf = False
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   110
        is_last = row == len(self.rset) - 1
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   111
        is_open = self.open_state(entity.eid, treeid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        if not hasattr(entity, 'is_leaf') or entity.is_leaf():
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   113
            if is_last:
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   114
                liclasses.append('last')
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   115
            w(u'<li class="%s">' % u' '.join(liclasses))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
            rql = entity.children_rql() % {'x': entity.eid}
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   118
            url = html_escape(self.build_url('json', rql=rql, vid=parentvid,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
                                             pageid=self.req.pageid,
929
80de412e6ed7 [treeview] treeid is passed along: we can manage as many trees as we want
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 928
diff changeset
   120
                                             treeid=treeid,
931
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
   121
                                             subvid=vid))
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
   122
            divclasses = ['hitarea']
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   123
            if is_open:
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   124
                liclasses.append('collapsable')
931
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
   125
                divclasses.append('collapsable-hitarea')
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   126
            else:
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   127
                liclasses.append('expandable')
931
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
   128
                divclasses.append('closed-hitarea expandable-hitarea')
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   129
            if is_last:
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   130
                if is_open:
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   131
                    liclasses.append('lastCollapsable')
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   132
                    divclasses.append('lastCollapsable-hitarea')
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   133
                else:
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   134
                    liclasses.append('lastExpandable')
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   135
                    divclasses.append('lastExpandable-hitarea')
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   136
            if is_open:
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   137
                w(u'<li class="%s">' % u' '.join(liclasses))
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   138
            else:
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   139
                w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(liclasses)))
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   140
            if is_leaf:
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   141
                divtail = ''
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   142
            else:
929
80de412e6ed7 [treeview] treeid is passed along: we can manage as many trees as we want
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 928
diff changeset
   143
                divtail = ''' onclick="async_remote_exec('node_clicked', '%s', '%s')"''' % \
80de412e6ed7 [treeview] treeid is passed along: we can manage as many trees as we want
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 928
diff changeset
   144
                    (treeid, entity.eid)
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   145
            w(u'<div class="%s"%s></div>' % (u' '.join(divclasses), divtail))
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   146
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
            # add empty <ul> because jquery's treeview plugin checks for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
            # sublists presence
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   149
            if not is_open:
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   150
                w(u'<ul class="placeholder"><li>place holder</li></ul>')
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   151
        # the local node info
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        self.wview(vid, self.rset, row=row, col=col)
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   153
        if is_open: # recurse if needed
931
7b701df4dbef [treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 930
diff changeset
   154
            self.wview(parentvid, self.req.execute(rql), treeid=treeid, initial_load=False)
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   155
        w(u'</li>')
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   156
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   157
from logilab.common.decorators import monkeypatch
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   158
from cubicweb.web.views.basecontrollers import JSonController
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   159
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   160
@monkeypatch(JSonController)
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   161
def js_node_clicked(self, treeid, nodeeid):
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   162
    """add/remove eid in treestate cookie"""
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   163
    cookies = self.req.get_cookie()
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   164
    statename = treecookiename(treeid)
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   165
    treestate = cookies.get(statename)
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   166
    if treestate is None:
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   167
        cookies[statename] = nodeeid
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   168
        self.req.set_cookie(cookies, statename)
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   169
    else:
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   170
        marked = set(filter(None, treestate.value.split(';')))
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   171
        if nodeeid in marked:
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   172
            marked.remove(nodeeid)
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   173
        else:
930
bab7f79fa26b [treeview] rename, manage lastCollapsable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 929
diff changeset
   174
            marked.add(nodeeid)
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   175
        cookies[statename] = ';'.join(marked)
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   176
        self.req.set_cookie(cookies, statename)