21 templatable = False |
21 templatable = False |
22 content_type = 'application/json' |
22 content_type = 'application/json' |
23 |
23 |
24 def call(self): |
24 def call(self): |
25 # remove entities that don't define latitude and longitude |
25 # remove entities that don't define latitude and longitude |
26 self.rset = self.rset.filtered_rset(lambda e: e.latitude and e.longitude) |
26 self.cw_rset = self.cw_rset.filtered_rset(lambda e: e.latitude and e.longitude) |
27 zoomlevel = self.req.form.pop('zoomlevel', 8) |
27 zoomlevel = self._cw.form.pop('zoomlevel', 8) |
28 extraparams = self.req.form.copy() |
28 extraparams = self._cw.form.copy() |
29 extraparams.pop('vid', None) |
29 extraparams.pop('vid', None) |
30 extraparams.pop('rql', None) |
30 extraparams.pop('rql', None) |
31 markers = [self.build_marker_data(rowidx, extraparams) |
31 markers = [self.build_marker_data(rowidx, extraparams) |
32 for rowidx in xrange(len(self.rset))] |
32 for rowidx in xrange(len(self.cw_rset))] |
33 center = { |
33 center = { |
34 'latitude': sum(marker['latitude'] for marker in markers) / len(markers), |
34 'latitude': sum(marker['latitude'] for marker in markers) / len(markers), |
35 'longitude': sum(marker['longitude'] for marker in markers) / len(markers), |
35 'longitude': sum(marker['longitude'] for marker in markers) / len(markers), |
36 } |
36 } |
37 geodata = { |
37 geodata = { |
40 'markers': markers, |
40 'markers': markers, |
41 } |
41 } |
42 self.w(simplejson.dumps(geodata)) |
42 self.w(simplejson.dumps(geodata)) |
43 |
43 |
44 def build_marker_data(self, row, extraparams): |
44 def build_marker_data(self, row, extraparams): |
45 entity = self.rset.get_entity(row, 0) |
45 entity = self.cw_rset.get_entity(row, 0) |
46 icon = None |
46 icon = None |
47 if hasattr(entity, 'marker_icon'): |
47 if hasattr(entity, 'marker_icon'): |
48 icon = entity.marker_icon() |
48 icon = entity.marker_icon() |
49 else: |
49 else: |
50 icon = (self.req.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None) |
50 icon = (self._cw.external_resource('GMARKER_ICON'), (20, 34), (4, 34), None) |
51 return {'latitude': entity.latitude, 'longitude': entity.longitude, |
51 return {'latitude': entity.latitude, 'longitude': entity.longitude, |
52 'title': entity.dc_long_title(), |
52 'title': entity.dc_long_title(), |
53 #icon defines : (icon._url, icon.size, icon.iconAncho', icon.shadow) |
53 #icon defines : (icon._url, icon.size, icon.iconAncho', icon.shadow) |
54 'icon': icon, |
54 'icon': icon, |
55 'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), |
55 'bubbleUrl': entity.absolute_url(vid='gmap-bubble', __notemplate=1, **extraparams), |
59 class GoogleMapBubbleView(EntityView): |
59 class GoogleMapBubbleView(EntityView): |
60 __regid__ = 'gmap-bubble' |
60 __regid__ = 'gmap-bubble' |
61 __select__ = implements(IGeocodable) |
61 __select__ = implements(IGeocodable) |
62 |
62 |
63 def cell_call(self, row, col): |
63 def cell_call(self, row, col): |
64 entity = self.rset.get_entity(row, col) |
64 entity = self.cw_rset.get_entity(row, col) |
65 self.w(u'<div>%s</div>' % entity.view('oneline')) |
65 self.w(u'<div>%s</div>' % entity.view('oneline')) |
66 # FIXME: we should call something like address-view if available |
66 # FIXME: we should call something like address-view if available |
67 |
67 |
68 |
68 |
69 class GoogleMapsView(EntityView): |
69 class GoogleMapsView(EntityView): |
71 __select__ = implements(IGeocodable) |
71 __select__ = implements(IGeocodable) |
72 |
72 |
73 need_navigation = False |
73 need_navigation = False |
74 |
74 |
75 def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None): |
75 def call(self, gmap_key, width=400, height=400, uselabel=True, urlparams=None): |
76 self.req.demote_to_html() |
76 self._cw.demote_to_html() |
77 # remove entities that don't define latitude and longitude |
77 # remove entities that don't define latitude and longitude |
78 self.rset = self.rset.filtered_rset(lambda e: e.latitude and e.longitude) |
78 self.cw_rset = self.cw_rset.filtered_rset(lambda e: e.latitude and e.longitude) |
79 self.req.add_js('http://maps.google.com/maps?sensor=false&file=api&v=2&key=%s' % gmap_key, |
79 self._cw.add_js('http://maps.google.com/maps?sensor=false&file=api&v=2&key=%s' % gmap_key, |
80 localfile=False) |
80 localfile=False) |
81 self.req.add_js( ('cubicweb.widgets.js', 'cubicweb.gmap.js', 'gmap.utility.labeledmarker.js') ) |
81 self._cw.add_js( ('cubicweb.widgets.js', 'cubicweb.gmap.js', 'gmap.utility.labeledmarker.js') ) |
82 rql = self.rset.printable_rql() |
82 rql = self.cw_rset.printable_rql() |
83 if urlparams is None: |
83 if urlparams is None: |
84 loadurl = self.build_url(rql=rql, vid='geocoding-json') |
84 loadurl = self.build_url(rql=rql, vid='geocoding-json') |
85 else: |
85 else: |
86 loadurl = self.build_url(rql=rql, vid='geocoding-json', **urlparams) |
86 loadurl = self.build_url(rql=rql, vid='geocoding-json', **urlparams) |
87 self.w(u'<div style="width: %spx; height: %spx;" class="widget gmap" ' |
87 self.w(u'<div style="width: %spx; height: %spx;" class="widget gmap" ' |
92 class GoogeMapsLegend(EntityView): |
92 class GoogeMapsLegend(EntityView): |
93 __regid__ = 'gmap-legend' |
93 __regid__ = 'gmap-legend' |
94 |
94 |
95 def call(self): |
95 def call(self): |
96 self.w(u'<ol>') |
96 self.w(u'<ol>') |
97 for rowidx in xrange(len(self.rset)): |
97 for rowidx in xrange(len(self.cw_rset)): |
98 self.w(u'<li>') |
98 self.w(u'<li>') |
99 self.wview('listitem', self.rset, row=rowidx, col=0) |
99 self.wview('listitem', self.cw_rset, row=rowidx, col=0) |
100 self.w(u'</li>') |
100 self.w(u'</li>') |
101 self.w(u'</ol>') |
101 self.w(u'</ol>') |