web/views/treeview.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 20 Feb 2009 21:32:23 +0100
branchtreeview-tabs
changeset 918 19862a0e55a5
parent 916 968f00dd9a24
child 923 7c184924d492
child 928 57614f333dc6
permissions -rw-r--r--
[treeview] (inprogress) minimally working state-remembering treeview
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
from logilab.mtconverter import html_escape
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
from cubicweb.interfaces import ITree
431
18b4dd650ef8 massive selector update
sylvain.thenault@logilab.fr
parents: 237
diff changeset
     3
from cubicweb.common.selectors import implement_interface, yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
from cubicweb.common.view import EntityView
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
class TreeView(EntityView):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
    id = 'treeview'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
    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
     9
    itemvid = 'treeitemview'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    10
    css_classes = 'treeview widget'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    11
    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
    12
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
    13
    def call(self, subvid=None, treeid=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
        if subvid is None and 'subvid' in self.req.form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
            subvid = self.req.form.pop('subvid') # consume it
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
        if subvid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
            subvid = 'oneline'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
        self.req.add_css('jquery.treeview.css')
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
    19
        self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js'))
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
    20
        # XXX find a way, an id is MANDATORY
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
    21
        treeid = 'TREE' #treeid or self.rset.rows[0][0]
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
    22
        self.req.html_headers.add_onload(u"""
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
             $("#tree-%s").treeview({toggle: toggleTree,
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
    24
		                     prerendered: true});""" % treeid)
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
    25
        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
    26
        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
    27
            self.wview(self.itemvid, self.rset, row=rowidx, col=0,
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    28
                       vid=subvid, parentvid=self.id)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
        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
    30
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    31
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
    32
    """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
    33
    """
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    34
    id = 'filetree'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    35
    css_classes = 'treeview widget filetree'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    36
    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
    37
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    38
    def call(self, subvid=None):
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    39
        super(FileTreeView, self).call(subvid='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
    40
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    41
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
    42
    """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
    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
    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
    45
    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
    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-oneline'
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    48
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    49
    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
    50
        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
    51
        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
    52
            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
    53
        else:
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    54
            # 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
    55
            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
    56
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
class DefaultTreeViewItemView(EntityView):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
    """default treeitem view for entities which don't implement ITree
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    id = 'treeitemview'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
    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
    63
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    64
    def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        entity = self.entity(row, col)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        itemview = self.view(vid, self.rset, row=row, col=col)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
        if row == len(self.rset) - 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
            self.w(u'<li class="last">%s</li>' % itemview)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        else:
472
958805c342b6 xhtml compliance
sylvain.thenault@logilab.fr
parents: 465
diff changeset
    70
            self.w(u'<li>%s</li>' % itemview)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    73
class TreeStateMixin(object):
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    74
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    75
    def open_state(self):
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    76
        raise NotImplementedError
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    77
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    78
class TreeViewItemView(EntityView, TreeStateMixin):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    """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
    80
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
    (each item should be exandable if it's not a tree leaf)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    id = 'treeitemview'
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 187
diff changeset
    84
    # 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
    85
    #     the default treeitem view
431
18b4dd650ef8 massive selector update
sylvain.thenault@logilab.fr
parents: 237
diff changeset
    86
    __selectors__ = (implement_interface, yes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    accepts_interfaces = (ITree,)
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    88
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    89
    def open_state(self):
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    90
        """implements TreeState mixin"""
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    91
        return ()
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
    92
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    93
    def cell_call(self, row, col, 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
    94
        w = self.w
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        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
    96
        liclasses = []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
        is_leaf = False
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
    98
        is_open = str(entity.eid) in self.open_state()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        if row == len(self.rset) - 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
            is_leaf = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        if not hasattr(entity, 'is_leaf') or 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
   102
            if is_leaf : liclasses.append('last')
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
   103
            w(u'<li class="%s">' % u' '.join(liclasses))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
            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
   106
            url = html_escape(self.build_url('json', rql=rql, vid=parentvid,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
                                             pageid=self.req.pageid,
187
cae87ca76f02 quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 150
diff changeset
   108
                                             subvid=vid,
cae87ca76f02 quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 150
diff changeset
   109
                                             noautoload=True))
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
   110
            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
   111
                liclasses.append('collapsable')
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
   112
            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
   113
                liclasses.append('expandable')
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
   114
            divclasses = ['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
   115
            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
   116
                divclasses.append('collapsable-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
   117
            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
   118
                divclasses.append('expandable-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
   119
            if is_leaf:
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
   120
                liclasses.append('lastExpandable')
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
   121
                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
   122
                    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
   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
                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
   125
            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
   126
                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
   127
            if is_leaf:
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   128
                divtail = ''
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   129
            else:
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   130
                divtail = ''' onclick="async_remote_exec('node_clicked', %s)"''' % entity.eid
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   131
            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
   132
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
            # add empty <ul> because jquery's treeview plugin checks for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
            # 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
   135
            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
   136
                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
   137
        # the local node info
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        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
   139
        if is_open: # recurse if needed
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
   140
            self.wview(parentvid, self.req.execute(rql))
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
   141
        w(u'</li>')
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   142
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   143
from logilab.common.decorators import monkeypatch
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   144
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
   145
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   146
@monkeypatch(JSonController)
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   147
def js_node_clicked(self, eid):
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   148
    """add/remove eid in treestate cookie
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   149
    XXX this deals with one tree per page
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   150
        also check the treeid issue above
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   151
    """
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   152
    cookies = self.req.get_cookie()
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   153
    treestate = cookies.get('treestate')
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   154
    if treestate is None:
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   155
        cookies['treestate'] = str(eid)
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   156
        self.req.set_cookie(cookies, 'treestate')
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   157
    else:
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   158
        marked = set(treestate.value.split(';'))
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   159
        if eid in marked:
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   160
            marked.remove(eid)
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   161
        else:
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   162
            marked.add(eid)
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   163
        cookies['treestate'] = ';'.join(str(x) for x in marked)
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   164
        self.req.set_cookie(cookies, 'treestate')