author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 01 Oct 2009 16:06:29 +0200 | |
branch | stable |
changeset 3544 | 188d86631c45 |
parent 2312 | af4d8f75c5db |
child 3377 | dd9d292b6a6d |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
"""basic support for SIMILE's timline widgets |
2 |
||
3 |
cf. http://code.google.com/p/simile-widgets/ |
|
4 |
||
5 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1884
diff
changeset
|
6 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 7 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1884
diff
changeset
|
8 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 9 |
""" |
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
12 |
import simplejson |
|
13 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
14 |
from logilab.mtconverter import xml_escape |
0 | 15 |
|
16 |
from cubicweb.interfaces import ICalendarable |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
17 |
from cubicweb.selectors import implements |
984 | 18 |
from cubicweb.view import EntityView, StartupView |
0 | 19 |
|
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
20 |
_ = unicode |
0 | 21 |
|
22 |
class TimelineJsonView(EntityView): |
|
23 |
"""generates a json file to feed Timeline.loadJSON() |
|
24 |
NOTE: work in progress (image_url, bubbleUrl and so on |
|
25 |
should be properties of entity classes or subviews) |
|
26 |
""" |
|
27 |
id = 'timeline-json' |
|
28 |
binary = True |
|
29 |
templatable = False |
|
30 |
content_type = 'application/json' |
|
31 |
||
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
32 |
__select__ = implements(ICalendarable) |
0 | 33 |
date_fmt = '%Y/%m/%d' |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
34 |
|
0 | 35 |
def call(self): |
36 |
events = [] |
|
37 |
for entity in self.rset.entities(): |
|
38 |
event = self.build_event(entity) |
|
39 |
if event is not None: |
|
40 |
events.append(event) |
|
41 |
timeline_data = {'dateTimeFormat': self.date_fmt, |
|
42 |
'events': events} |
|
43 |
self.w(simplejson.dumps(timeline_data)) |
|
44 |
||
45 |
# FIXME: those properties should be defined by the entity class |
|
46 |
def onclick_url(self, entity): |
|
47 |
return entity.absolute_url() |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
48 |
|
0 | 49 |
def onclick(self, entity): |
50 |
url = self.onclick_url(entity) |
|
51 |
if url: |
|
52 |
return u"javascript: document.location.href='%s'" % url |
|
53 |
return None |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
54 |
|
0 | 55 |
def build_event(self, entity): |
56 |
"""converts `entity` into a JSON object |
|
57 |
{'start': '1891', |
|
58 |
'end': '1915', |
|
59 |
'title': 'Portrait of Horace Brodsky', |
|
60 |
'description': 'by Henri Gaudier-Brzeska, French Sculptor, 1891-1915', |
|
61 |
'image': 'http://imagecache2.allposters.com/images/BRGPOD/102770_b.jpg', |
|
62 |
'link': 'http://www.allposters.com/-sp/Portrait-of-Horace-Brodsky-Posters_i1584413_.htm' |
|
63 |
} |
|
64 |
""" |
|
65 |
start = entity.start |
|
66 |
stop = entity.stop |
|
67 |
start = start or stop |
|
68 |
if start is None and stop is None: |
|
69 |
return None |
|
70 |
event_data = {'start': start.strftime(self.date_fmt), |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
71 |
'title': xml_escape(entity.dc_title()), |
1881
75540944ae18
fix dc_description format arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1730
diff
changeset
|
72 |
'description': entity.dc_description(format='text/html'), |
0 | 73 |
'link': entity.absolute_url(), |
74 |
} |
|
75 |
onclick = self.onclick(entity) |
|
76 |
if onclick: |
|
77 |
event_data['onclick'] = onclick |
|
78 |
if stop: |
|
79 |
event_data['end'] = stop.strftime(self.date_fmt) |
|
80 |
return event_data |
|
81 |
||
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
82 |
|
0 | 83 |
class TimelineViewMixIn(object): |
84 |
widget_class = 'TimelineWidget' |
|
85 |
jsfiles = ('cubicweb.timeline-bundle.js', 'cubicweb.widgets.js', |
|
86 |
'cubicweb.timeline-ext.js', 'cubicweb.ajax.js') |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
87 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
88 |
def render_url(self, loadurl, tlunit=None): |
0 | 89 |
tlunit = tlunit or self.req.form.get('tlunit') |
90 |
self.req.add_js(self.jsfiles) |
|
91 |
self.req.add_css('timeline-bundle.css') |
|
92 |
if tlunit: |
|
93 |
additional = u' cubicweb:tlunit="%s"' % tlunit |
|
94 |
else: |
|
95 |
additional = u'' |
|
96 |
self.w(u'<div class="widget" cubicweb:wdgtype="%s" ' |
|
97 |
u'cubicweb:loadtype="auto" cubicweb:loadurl="%s" %s >' % |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
(self.widget_class, xml_escape(loadurl), |
0 | 99 |
additional)) |
100 |
self.w(u'</div>') |
|
101 |
||
102 |
||
103 |
class TimelineView(TimelineViewMixIn, EntityView): |
|
104 |
"""builds a cubicweb timeline widget node""" |
|
105 |
id = 'timeline' |
|
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
106 |
title = _('timeline') |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
107 |
__select__ = implements(ICalendarable) |
0 | 108 |
need_navigation = False |
109 |
def call(self, tlunit=None): |
|
110 |
self.req.html_headers.define_var('Timeline_urlPrefix', self.req.datadir_url) |
|
111 |
rql = self.rset.printable_rql() |
|
112 |
loadurl = self.build_url(rql=rql, vid='timeline-json') |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
113 |
self.render_url(loadurl, tlunit) |
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
114 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
115 |
|
0 | 116 |
class StaticTimelineView(TimelineViewMixIn, StartupView): |
117 |
"""similar to `TimelineView` but loads data from a static |
|
118 |
JSON file instead of one after a RQL query. |
|
119 |
""" |
|
120 |
id = 'static-timeline' |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
121 |
|
0 | 122 |
def call(self, loadurl, tlunit=None, wdgclass=None): |
1093 | 123 |
self.widget_class = wdgclass or self.widget_class |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
124 |
self.render_url(loadurl, tlunit) |