author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Fri, 20 Feb 2009 20:48:33 +0100 | |
branch | treeview-tabs |
changeset 916 | 968f00dd9a24 |
parent 472 | 958805c342b6 |
child 918 | 19862a0e55a5 |
permissions | -rw-r--r-- |
0 | 1 |
from logilab.mtconverter import html_escape |
2 |
from cubicweb.interfaces import ITree |
|
431 | 3 |
from cubicweb.common.selectors import implement_interface, yes |
0 | 4 |
from cubicweb.common.view import EntityView |
5 |
||
6 |
class TreeView(EntityView): |
|
7 |
id = 'treeview' |
|
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 | 10 |
css_classes = 'treeview widget' |
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 | 14 |
if subvid is None and 'subvid' in self.req.form: |
15 |
subvid = self.req.form.pop('subvid') # consume it |
|
16 |
if subvid is None: |
|
17 |
subvid = 'oneline' |
|
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 | 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 | 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 | 35 |
css_classes = 'treeview widget filetree' |
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 | 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 | 56 |
|
57 |
||
58 |
class DefaultTreeViewItemView(EntityView): |
|
59 |
"""default treeitem view for entities which don't implement ITree |
|
60 |
""" |
|
61 |
id = 'treeitemview' |
|
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 | 65 |
entity = self.entity(row, col) |
66 |
itemview = self.view(vid, self.rset, row=row, col=col) |
|
67 |
if row == len(self.rset) - 1: |
|
68 |
self.w(u'<li class="last">%s</li>' % itemview) |
|
69 |
else: |
|
472 | 70 |
self.w(u'<li>%s</li>' % itemview) |
0 | 71 |
|
72 |
||
73 |
class TreeViewItemView(EntityView): |
|
74 |
"""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
|
75 |
|
0 | 76 |
(each item should be exandable if it's not a tree leaf) |
77 |
""" |
|
78 |
id = 'treeitemview' |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
187
diff
changeset
|
79 |
# XXX append yes to make sure we get an higher score than |
0 | 80 |
# the default treeitem view |
431 | 81 |
__selectors__ = (implement_interface, yes) |
0 | 82 |
accepts_interfaces = (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
|
83 |
opening = 'man_a/a1'.split('/') |
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
|
84 |
|
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
85 |
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
|
86 |
w = self.w |
0 | 87 |
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
|
88 |
liclasses = [] |
0 | 89 |
is_leaf = 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
|
90 |
is_open = entity.name in self.opening |
0 | 91 |
if row == len(self.rset) - 1: |
92 |
is_leaf = True |
|
93 |
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
|
94 |
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
|
95 |
w(u'<li class="%s">' % u' '.join(liclasses)) |
0 | 96 |
else: |
97 |
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
|
98 |
url = html_escape(self.build_url('json', rql=rql, vid=parentvid, |
0 | 99 |
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
|
100 |
subvid=vid, |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
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
|
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 |
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
|
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 |
w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, 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
|
119 |
w(u'<div class="%s"> </div>' % u' '.join(divclasses)) |
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 |
|
0 | 121 |
# add empty <ul> because jquery's treeview plugin checks for |
122 |
# 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
|
123 |
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
|
124 |
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
|
125 |
# the local node info |
0 | 126 |
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
|
127 |
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
|
128 |
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
|
129 |
w(u'</li>') |