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