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