2013-09-22 11:11:48 +02:00
|
|
|
from django.forms import widgets
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
|
|
|
class LocationWidget(widgets.TextInput):
|
2013-09-27 11:39:28 +02:00
|
|
|
def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height = 480, **kwargs):
|
|
|
|
self.based_field = based_field
|
2013-09-22 11:11:48 +02:00
|
|
|
self.zoom = zoom
|
2013-09-27 11:39:28 +02:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2013-09-22 11:11:48 +02:00
|
|
|
super(LocationWidget, self).__init__(attrs)
|
|
|
|
|
|
|
|
def render(self, name, value, attrs=None):
|
|
|
|
if value is not None:
|
|
|
|
lat, lng, zoom = value.split(',')
|
|
|
|
|
|
|
|
value = '%s,%s,%d' % (
|
|
|
|
float(lat),
|
|
|
|
float(lng),
|
|
|
|
float(zoom)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
value = ''
|
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
based_field = "#id_" + self.based_field.name
|
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
attrs = attrs or {}
|
|
|
|
attrs['data-location-widget'] = name
|
2013-09-27 11:39:28 +02:00
|
|
|
attrs['data-based-field'] = based_field
|
2013-09-22 11:11:48 +02:00
|
|
|
attrs['data-zoom'] = self.zoom
|
|
|
|
attrs['data-map'] = '#map_' + name
|
|
|
|
|
|
|
|
text_input = super(LocationWidget, self).render(name, value, attrs)
|
|
|
|
|
|
|
|
map_div = u'''
|
2013-09-27 11:39:28 +02:00
|
|
|
<div style="margin:4px 0 0 0"> <label></label>
|
|
|
|
<div id="map_%(name)s" style="width: %(width)dpx; height: %(height)dpx"></div>
|
|
|
|
</div>
|
|
|
|
'''
|
|
|
|
|
|
|
|
return mark_safe(text_input + map_div % {'name': name, 'width': self.width, 'height': self.height })
|
2013-09-22 11:11:48 +02:00
|
|
|
|
|
|
|
class Media:
|
|
|
|
# Use schemaless URL so it works with both, http and https websites
|
|
|
|
js = (
|
|
|
|
'//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', # jquery
|
|
|
|
'//maps.google.com/maps/api/js?sensor=false&language=de',
|
|
|
|
'/static/js/bindWithDelay.js',
|
|
|
|
'/location_field/media/form.js',
|
|
|
|
)
|