author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 11 Feb 2009 18:46:14 +0100 | |
changeset 604 | e85042d18b48 |
parent 472 | 958805c342b6 |
child 688 | cddfbdee0eb3 |
child 916 | 968f00dd9a24 |
permissions | -rw-r--r-- |
0 | 1 |
from logilab.mtconverter import html_escape |
2 |
||
3 |
from cubicweb.interfaces import ITree |
|
431 | 4 |
from cubicweb.common.selectors import implement_interface, yes |
0 | 5 |
from cubicweb.common.view import EntityView |
6 |
||
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
7 |
from cubicweb.web.views.baseviews import OneLineView |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
8 |
|
0 | 9 |
class TreeView(EntityView): |
10 |
id = 'treeview' |
|
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 | 13 |
css_classes = 'treeview widget' |
14 |
title = _('tree view') |
|
0 | 15 |
|
16 |
def call(self, subvid=None): |
|
17 |
if subvid is None and 'subvid' in self.req.form: |
|
18 |
subvid = self.req.form.pop('subvid') # consume it |
|
19 |
if subvid is None: |
|
20 |
subvid = 'oneline' |
|
21 |
self.req.add_css('jquery.treeview.css') |
|
22 |
self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js', 'cubicweb.widgets.js')) |
|
187
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
23 |
# XXX noautoload is a quick hack to avoid treeview to be rebuilt |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
24 |
# after a json query and avoid double toggling bugs. |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
25 |
# Need to find a way to do that cleanly. |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
26 |
if 'noautoload' in self.req.form: |
443 | 27 |
self.w(u'<ul class="%s" cubicweb:wdgtype="TreeView">' % self.css_classes) |
187
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
28 |
else: |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
29 |
self.w(u'<ul class="%s" cubicweb:loadtype="auto" cubicweb:wdgtype="TreeView">' |
443 | 30 |
% self.css_classes) |
0 | 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 | 34 |
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
|
35 |
|
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 | 41 |
css_classes = 'treeview widget filetree' |
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 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
47 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
48 |
|
443 | 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(): |
464
75d242975792
[treeview] replace span elements by div for xhtml conformance
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
431
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 |
464
75d242975792
[treeview] replace span elements by div for xhtml conformance
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
431
diff
changeset
|
63 |
self.w(u'<div class="file">%s</div>' % entity.view('oneline')) |
0 | 64 |
|
65 |
||
66 |
class DefaultTreeViewItemView(EntityView): |
|
67 |
"""default treeitem view for entities which don't implement ITree |
|
68 |
""" |
|
69 |
id = 'treeitemview' |
|
70 |
accepts = ('Any',) |
|
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 | 73 |
entity = self.entity(row, col) |
74 |
itemview = self.view(vid, self.rset, row=row, col=col) |
|
75 |
if row == len(self.rset) - 1: |
|
76 |
self.w(u'<li class="last">%s</li>' % itemview) |
|
77 |
else: |
|
472 | 78 |
self.w(u'<li>%s</li>' % itemview) |
0 | 79 |
|
80 |
||
81 |
class TreeViewItemView(EntityView): |
|
82 |
"""specific treeitem view for entities which implement ITree |
|
83 |
|
|
84 |
(each item should be exandable if it's not a tree leaf) |
|
85 |
""" |
|
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 | 88 |
# the default treeitem view |
431 | 89 |
__selectors__ = (implement_interface, yes) |
0 | 90 |
accepts_interfaces = (ITree,) |
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 | 93 |
entity = self.entity(row, col) |
94 |
cssclasses = [] |
|
95 |
is_leaf = False |
|
96 |
if row == len(self.rset) - 1: |
|
97 |
is_leaf = True |
|
98 |
if not hasattr(entity, 'is_leaf') or entity.is_leaf(): |
|
99 |
if is_leaf : cssclasses.append('last') |
|
100 |
self.w(u'<li class="%s">' % u' '.join(cssclasses)) |
|
101 |
else: |
|
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 | 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)) |
0 | 107 |
cssclasses.append('expandable') |
108 |
divclasses = ['hitarea expandable-hitarea'] |
|
109 |
if is_leaf : |
|
110 |
cssclasses.append('lastExpandable') |
|
111 |
divclasses.append('lastExpandable-hitarea') |
|
112 |
self.w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(cssclasses))) |
|
113 |
self.w(u'<div class="%s"> </div>' % u' '.join(divclasses)) |
|
114 |
||
115 |
# add empty <ul> because jquery's treeview plugin checks for |
|
116 |
# sublists presence |
|
117 |
self.w(u'<ul class="placeholder"><li>place holder</li></ul>') |
|
118 |
self.wview(vid, self.rset, row=row, col=col) |
|
119 |
self.w(u'</li>') |
|
120 |