author | sylvain.thenault@logilab.fr |
Tue, 28 Apr 2009 19:09:37 +0200 | |
branch | tls-sprint |
changeset 1519 | 5cfc5cc1dd20 |
parent 1133 | 8a409ea0c9ec |
child 1802 | d628defebc17 |
permissions | -rw-r--r-- |
928
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
1 |
"""Set of tree-building widgets, based on jQuery treeview plugin |
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
2 |
|
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
3 |
:organization: Logilab |
1007
34e92a8d9167
typo, don't give treeid as argument to treeitemview (XXX auc?)
sylvain.thenault@logilab.fr
parents:
1006
diff
changeset
|
4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
928
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
6 |
""" |
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
7 |
__docformat__ = "restructuredtext en" |
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
8 |
|
0 | 9 |
from logilab.mtconverter import html_escape |
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
10 |
|
0 | 11 |
from cubicweb.interfaces import ITree |
981
d86d1ee3b60e
fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents:
939
diff
changeset
|
12 |
from cubicweb.selectors import implements |
d86d1ee3b60e
fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents:
939
diff
changeset
|
13 |
from cubicweb.view import EntityView |
1007
34e92a8d9167
typo, don't give treeid as argument to treeitemview (XXX auc?)
sylvain.thenault@logilab.fr
parents:
1006
diff
changeset
|
14 |
from cubicweb.utils import make_uid |
0 | 15 |
|
928
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
16 |
def treecookiename(treeid): |
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
17 |
return str('treestate-%s' % treeid) |
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
18 |
|
0 | 19 |
|
20 |
class TreeView(EntityView): |
|
21 |
id = 'treeview' |
|
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 |
itemvid = 'treeitemview' |
443 | 23 |
css_classes = 'treeview widget' |
24 |
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
|
25 |
|
931
7b701df4dbef
[treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
930
diff
changeset
|
26 |
def call(self, subvid=None, treeid=None, initial_load=True): |
0 | 27 |
if subvid is None: |
998
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
28 |
if 'subvid' in self.req.form: |
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
29 |
subvid = self.req.form.pop('subvid') # consume it |
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
30 |
else: |
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
31 |
subvid = 'oneline' |
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
32 |
if treeid is None: |
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
33 |
if 'treeid' in self.req.form: |
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
34 |
treeid = self.req.form.pop('treeid') |
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
35 |
else: |
1001
7d0fccdb8125
[treeview] uuid is not available in the stdlib before python 2.5
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
999
diff
changeset
|
36 |
treeid = make_uid('throw away uid') |
998
9c6ce9d6384f
[treeview] if the client is not interested in maintaining the tree state across page reloads, we must provide a treeid
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
931
diff
changeset
|
37 |
self.warning('Tree state won\'t be properly restored after next reload') |
931
7b701df4dbef
[treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
930
diff
changeset
|
38 |
if initial_load: |
7b701df4dbef
[treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
930
diff
changeset
|
39 |
self.req.add_css('jquery.treeview.css') |
7b701df4dbef
[treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
930
diff
changeset
|
40 |
self.req.add_js(('cubicweb.ajax.js', 'jquery.treeview.js')) |
7b701df4dbef
[treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
930
diff
changeset
|
41 |
self.req.html_headers.add_onload(u""" |
1006 | 42 |
jQuery("#tree-%s").treeview({toggle: toggleTree, prerendered: true});""" % treeid) |
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
|
43 |
self.w(u'<ul id="tree-%s" class="%s">' % (treeid, self.css_classes)) |
0 | 44 |
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
|
45 |
self.wview(self.itemvid, self.rset, row=rowidx, col=0, |
1007
34e92a8d9167
typo, don't give treeid as argument to treeitemview (XXX auc?)
sylvain.thenault@logilab.fr
parents:
1006
diff
changeset
|
46 |
vid=subvid, parentvid=self.id) |
0 | 47 |
self.w(u'</ul>') |
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
48 |
|
150
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 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
|
51 |
"""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
|
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 |
id = 'filetree' |
443 | 54 |
css_classes = 'treeview widget filetree' |
55 |
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
|
56 |
|
931
7b701df4dbef
[treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
930
diff
changeset
|
57 |
def call(self, subvid=None, treeid=None, initial_load=True): |
7b701df4dbef
[treeview] don't readd stuff to the header, poslish css classes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
930
diff
changeset
|
58 |
super(FileTreeView, self).call(treeid=treeid, subvid='filetree-oneline', initial_load=initial_load) |
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
59 |
|
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
60 |
|
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
61 |
|
443 | 62 |
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
|
63 |
"""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
|
64 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
""" |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
68 |
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
|
69 |
|
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
else: |
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
75 |
# 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
|
76 |
self.w(u'<div class="file">%s</div>' % entity.view('oneline')) |
0 | 77 |
|
78 |
||
79 |
class DefaultTreeViewItemView(EntityView): |
|
981
d86d1ee3b60e
fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents:
939
diff
changeset
|
80 |
"""default treeitem view for entities which don't implement ITree""" |
0 | 81 |
id = 'treeitemview' |
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
82 |
|
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
83 |
def cell_call(self, row, col, vid='oneline', parentvid='treeview'): |
0 | 84 |
entity = self.entity(row, col) |
85 |
itemview = self.view(vid, self.rset, row=row, col=col) |
|
86 |
if row == len(self.rset) - 1: |
|
87 |
self.w(u'<li class="last">%s</li>' % itemview) |
|
88 |
else: |
|
472 | 89 |
self.w(u'<li>%s</li>' % itemview) |
0 | 90 |
|
91 |
||
928
57614f333dc6
[treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
92 |
class TreeViewItemView(EntityView): |
0 | 93 |
"""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
|
94 |
|
981
d86d1ee3b60e
fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents:
939
diff
changeset
|
95 |
(each item should be expandable if it's not a tree leaf) |
0 | 96 |
""" |
97 |
id = 'treeitemview' |
|
1007
34e92a8d9167
typo, don't give treeid as argument to treeitemview (XXX auc?)
sylvain.thenault@logilab.fr
parents:
1006
diff
changeset
|
98 |
__select__ = implements(ITree) |
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
99 |
|
150
1190261a1f13
provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
100 |
def cell_call(self, row, col, vid='oneline', parentvid='treeview'): |
0 | 101 |
entity = self.entity(row, col) |
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
102 |
cssclasses = [] |
0 | 103 |
is_leaf = False |
104 |
if row == len(self.rset) - 1: |
|
105 |
is_leaf = True |
|
106 |
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
|
107 |
if is_leaf : cssclasses.append('last') |
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
108 |
self.w(u'<li class="%s">' % u' '.join(cssclasses)) |
0 | 109 |
else: |
110 |
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
|
111 |
url = html_escape(self.build_url('json', rql=rql, vid=parentvid, |
0 | 112 |
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
|
113 |
subvid=vid, |
cae87ca76f02
quick hack to fix treeview double toggling bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
150
diff
changeset
|
114 |
noautoload=True)) |
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
115 |
cssclasses.append('expandable') |
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
116 |
divclasses = ['hitarea expandable-hitarea'] |
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
117 |
if is_leaf : |
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
118 |
cssclasses.append('lastExpandable') |
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
119 |
divclasses.append('lastExpandable-hitarea') |
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
120 |
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
|
121 |
self.w(u'<div class="%s"> </div>' % u' '.join(divclasses)) |
0 | 122 |
# add empty <ul> because jquery's treeview plugin checks for |
123 |
# sublists presence |
|
923
7c184924d492
unwanted merge : Backed out changeset 4328bcbd748e
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
918
diff
changeset
|
124 |
self.w(u'<ul class="placeholder"><li>place holder</li></ul>') |
0 | 125 |
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
|
126 |
self.w(u'</li>') |
918
19862a0e55a5
[treeview] (inprogress) minimally working state-remembering treeview
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
916
diff
changeset
|
127 |