web/views/treeview.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 23 Feb 2009 12:12:59 +0100
changeset 923 7c184924d492
parent 918 19862a0e55a5
child 985 6a25c58a1c23
child 999 999198995a53
permissions -rw-r--r--
unwanted merge : Backed out changeset 4328bcbd748e
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
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     2
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
from cubicweb.interfaces import ITree
431
18b4dd650ef8 massive selector update
sylvain.thenault@logilab.fr
parents: 237
diff changeset
     4
from cubicweb.common.selectors import implement_interface, yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
from cubicweb.common.view import EntityView
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
     7
from cubicweb.web.views.baseviews import OneLineView
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
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
class TreeView(EntityView):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
    id = 'treeview'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
    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
    12
    itemvid = 'treeitemview'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    13
    css_classes = 'treeview widget'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    14
    title = _('tree view')
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    15
    
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    16
    def call(self, subvid=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
        if subvid is None and 'subvid' in self.req.form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
            subvid = self.req.form.pop('subvid') # consume it
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
        if subvid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
            subvid = 'oneline'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
        self.req.add_css('jquery.treeview.css')
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    22
        self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js', 'cubicweb.widgets.js'))
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    23
        # XXX noautoload is a quick hack to avoid treeview to be rebuilt
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    24
        #     after a json query and avoid double toggling bugs.
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    25
        #     Need to find a way to do that cleanly.
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    26
        if 'noautoload' in self.req.form:
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    27
            self.w(u'<ul class="%s" cubicweb:wdgtype="TreeView">' % self.css_classes)
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    28
        else:
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    29
            self.w(u'<ul class="%s" cubicweb:loadtype="auto" cubicweb:wdgtype="TreeView">'
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    30
                   % self.css_classes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
        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
    32
            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
    33
                       vid=subvid, parentvid=self.id)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
        self.w(u'</ul>')
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    35
        
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    36
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    37
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
    38
    """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
    39
    """
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    40
    id = 'filetree'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    41
    css_classes = 'treeview widget filetree'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    42
    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
    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
    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
    45
        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
    46
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    47
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    48
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    49
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
    50
    """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
    51
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    52
    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
    53
    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
    54
    """
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    55
    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
    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
    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
    58
        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
    59
        if ITree.is_implemented_by(entity.__class__) and not entity.is_leaf():
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    60
            self.w(u'<div class="folder">%s</div>' % 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
    61
        else:
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    62
            # XXX define specific CSS classes according to mime types
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    63
            self.w(u'<div class="file">%s</div>' % entity.view('oneline'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
class DefaultTreeViewItemView(EntityView):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    """default treeitem view for entities which don't implement ITree
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    id = 'treeitemview'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    accepts = ('Any',)
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    71
    
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    72
    def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        entity = self.entity(row, col)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        itemview = self.view(vid, self.rset, row=row, col=col)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
        if row == len(self.rset) - 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
            self.w(u'<li class="last">%s</li>' % itemview)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        else:
472
958805c342b6 xhtml compliance
sylvain.thenault@logilab.fr
parents: 465
diff changeset
    78
            self.w(u'<li>%s</li>' % itemview)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    81
class TreeViewItemView(EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    """specific treeitem view for entities which implement ITree
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    83
    
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    (each item should be exandable if it's not a tree leaf)
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
    id = 'treeitemview'
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 187
diff changeset
    87
    # 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
    88
    #     the default treeitem view
431
18b4dd650ef8 massive selector update
sylvain.thenault@logilab.fr
parents: 237
diff changeset
    89
    __selectors__ = (implement_interface, yes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    accepts_interfaces = (ITree,)
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    91
    
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    92
    def cell_call(self, row, col, vid='oneline', parentvid='treeview'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        entity = self.entity(row, col)
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    94
        cssclasses = []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        is_leaf = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        if row == len(self.rset) - 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
            is_leaf = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        if not hasattr(entity, 'is_leaf') or entity.is_leaf():
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    99
            if is_leaf : cssclasses.append('last')
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   100
            self.w(u'<li class="%s">' % u' '.join(cssclasses))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
            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
   103
            url = html_escape(self.build_url('json', rql=rql, vid=parentvid,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
                                             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
   105
                                             subvid=vid,
cae87ca76f02 quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 150
diff changeset
   106
                                             noautoload=True))
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   107
            cssclasses.append('expandable')
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   108
            divclasses = ['hitarea expandable-hitarea']
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   109
            if is_leaf :
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   110
                cssclasses.append('lastExpandable')
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   111
                divclasses.append('lastExpandable-hitarea')
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   112
            self.w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(cssclasses)))
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   113
            self.w(u'<div class="%s"> </div>' % u' '.join(divclasses))
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   114
                
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
            # add empty <ul> because jquery's treeview plugin checks for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
            # sublists presence
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   117
            self.w(u'<ul class="placeholder"><li>place holder</li></ul>')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        self.wview(vid, self.rset, row=row, col=col)
923
7c184924d492 unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   119
        self.w(u'</li>')
918
19862a0e55a5 [treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 916
diff changeset
   120