location field refactor

This commit is contained in:
Martin Bauer
2013-09-28 11:41:05 +02:00
parent 05b7a2b969
commit 6c3a7ca408
7 changed files with 261 additions and 120 deletions

View File

@@ -1,6 +1,8 @@
from django.forms import widgets
from django.utils.safestring import mark_safe
import os
class LocationWidget(widgets.TextInput):
def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height = 480, **kwargs):
self.based_field = based_field
@@ -24,26 +26,33 @@ class LocationWidget(widgets.TextInput):
based_field = "#id_" + self.based_field.name
attrs = attrs or {}
attrs['readonly'] = "readonly"
attrs['data-location-widget'] = name
attrs['data-based-field'] = based_field
attrs['data-zoom'] = self.zoom
attrs['data-map'] = '#map_' + name
attrs['data-dialog-id'] = "#map_dialog_" + name
text_input = super(LocationWidget, self).render(name, value, attrs)
map_div = u'''
<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 })
path = os.path.abspath(os.path.dirname(__file__))
with open( path + "/media/form.html" , 'r') as content_file:
html = content_file.read()
return mark_safe(text_input + html % {'name': name, 'width': self.width, 'height': self.height })
class Media:
css = {
'all' : ('/location_field/media/form.css', )
}
# 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
'//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js',
'//maps.google.com/maps/api/js?sensor=false&language=de',
'/static/js/bindWithDelay.js',
'/location_field/media/form.js',
)