author | sylvain.thenault@logilab.fr |
Fri, 09 Jan 2009 17:00:21 +0100 | |
changeset 384 | f117f6cefad0 |
parent 237 | 3df2e0ae2eba |
child 431 | 18b4dd650ef8 |
permissions | -rw-r--r-- |
0 | 1 |
from logilab.mtconverter import html_escape |
2 |
||
3 |
from cubicweb.interfaces import ITree |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
187
diff
changeset
|
4 |
from cubicweb.common.selectors import interface_selector, 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 |
fstree = False |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
13 |
itemvid = 'treeitemview' |
0 | 14 |
|
15 |
def call(self, subvid=None): |
|
16 |
if subvid is None and 'subvid' in self.req.form: |
|
17 |
subvid = self.req.form.pop('subvid') # consume it |
|
18 |
if subvid is None: |
|
19 |
subvid = 'oneline' |
|
20 |
self.req.add_css('jquery.treeview.css') |
|
21 |
self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js', 'cubicweb.widgets.js')) |
|
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
22 |
css_classes = 'treeview widget' |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
23 |
if self.fstree: |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
24 |
css_classes += ' filetree' |
187
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
25 |
# 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
|
26 |
# 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
|
27 |
# 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
|
28 |
if 'noautoload' in self.req.form: |
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:wdgtype="TreeView">' % css_classes) |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
30 |
else: |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
31 |
self.w(u'<ul class="%s" cubicweb:loadtype="auto" cubicweb:wdgtype="TreeView">' |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
32 |
% css_classes) |
0 | 33 |
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
|
34 |
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
|
35 |
vid=subvid, parentvid=self.id) |
0 | 36 |
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
|
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 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
39 |
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
|
40 |
"""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
|
41 |
""" |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
42 |
id = 'filetree' |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
43 |
fstree = True |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
44 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
45 |
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
|
46 |
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
|
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 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
49 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
50 |
class FileItemInnerView(OneLineView): |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
51 |
"""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
|
52 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
""" |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
56 |
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
|
57 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
if ITree.is_implemented_by(entity.__class__) and not entity.is_leaf(): |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
61 |
self.w(u'<span class="folder">%s</span>' % entity.view('oneline')) |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
62 |
else: |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
63 |
# XXX define specific CSS classes according to mime types |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
64 |
self.w(u'<span class="file">%s</span>' % entity.view('oneline')) |
0 | 65 |
|
66 |
||
67 |
class DefaultTreeViewItemView(EntityView): |
|
68 |
"""default treeitem view for entities which don't implement ITree |
|
69 |
""" |
|
70 |
id = 'treeitemview' |
|
71 |
accepts = ('Any',) |
|
72 |
||
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
73 |
def cell_call(self, row, col, vid='oneline', parentvid='treeview'): |
0 | 74 |
entity = self.entity(row, col) |
75 |
itemview = self.view(vid, self.rset, row=row, col=col) |
|
76 |
if row == len(self.rset) - 1: |
|
77 |
self.w(u'<li class="last">%s</li>' % itemview) |
|
78 |
else: |
|
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
79 |
self.w(u'<li><span>%s</span></li>' % itemview) |
0 | 80 |
|
81 |
||
82 |
class TreeViewItemView(EntityView): |
|
83 |
"""specific treeitem view for entities which implement ITree |
|
84 |
|
|
85 |
(each item should be exandable if it's not a tree leaf) |
|
86 |
""" |
|
87 |
id = 'treeitemview' |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
187
diff
changeset
|
88 |
# XXX append yes to make sure we get an higher score than |
0 | 89 |
# the default treeitem view |
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
187
diff
changeset
|
90 |
__selectors__ = (interface_selector, yes) |
0 | 91 |
accepts_interfaces = (ITree,) |
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'): |
0 | 94 |
entity = self.entity(row, col) |
95 |
cssclasses = [] |
|
96 |
is_leaf = False |
|
97 |
if row == len(self.rset) - 1: |
|
98 |
is_leaf = True |
|
99 |
if not hasattr(entity, 'is_leaf') or entity.is_leaf(): |
|
100 |
if is_leaf : cssclasses.append('last') |
|
101 |
self.w(u'<li class="%s">' % u' '.join(cssclasses)) |
|
102 |
else: |
|
103 |
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
|
104 |
url = html_escape(self.build_url('json', rql=rql, vid=parentvid, |
0 | 105 |
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
|
106 |
subvid=vid, |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
107 |
noautoload=True)) |
0 | 108 |
cssclasses.append('expandable') |
109 |
divclasses = ['hitarea expandable-hitarea'] |
|
110 |
if is_leaf : |
|
111 |
cssclasses.append('lastExpandable') |
|
112 |
divclasses.append('lastExpandable-hitarea') |
|
113 |
self.w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(cssclasses))) |
|
114 |
self.w(u'<div class="%s"> </div>' % u' '.join(divclasses)) |
|
115 |
||
116 |
# add empty <ul> because jquery's treeview plugin checks for |
|
117 |
# sublists presence |
|
118 |
self.w(u'<ul class="placeholder"><li>place holder</li></ul>') |
|
119 |
self.wview(vid, self.rset, row=row, col=col) |
|
120 |
self.w(u'</li>') |
|
121 |