From 92674f465afcde8a15685b8a7c7d5a24e4ff432b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 10 Oct 2014 19:19:25 +0200 Subject: [PATCH] map field (work in progress) --- wcs/fields.py | 17 +++++++++++++++++ wcs/qommon/form.py | 27 +++++++++++++++++++++++++++ wcs/qommon/http_response.py | 4 ++++ wcs/qommon/static/css/qommon.css | 4 ++++ wcs/qommon/static/js/qommon.map.js | 27 +++++++++++++++++++++++++++ wcs/root.py | 4 ++++ 6 files changed, 83 insertions(+) create mode 100644 wcs/qommon/static/js/qommon.map.js diff --git a/wcs/fields.py b/wcs/fields.py index c346203..a098eee 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -15,6 +15,7 @@ # along with this program; if not, see . import time +import random import re import xml.etree.ElementTree as ET @@ -1348,6 +1349,22 @@ class TableRowsField(WidgetField): register_field_class(TableRowsField) +class MapField(WidgetField): + key = 'map' + description = N_('Map') + + widget_class = MapWidget + + def get_view_value(self, value): + widget = self.widget_class('x%s' % random.random(), value, readonly=True) + return widget.render_content() + + def get_rst_view_value(self, value, indent=''): + return indent + value + +register_field_class(MapField) + + class RankedItemsField(WidgetField): key = 'ranked-items' description = N_('Ranked Items') diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index d55fb4b..d3554ea 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -1747,3 +1747,30 @@ class PasswordEntryWidget(CompositeWidget): self.value = hashlib.sha1(pwd1).hexdigest() else: self.value = None + + +class MapWidget(CompositeWidget): + def __init__(self, name, value=None, **kwargs): + CompositeWidget.__init__(self, name, value, **kwargs) + self.add(HiddenWidget, 'latlng') + self.readonly = kwargs.pop('readonly', False) + + def render_content(self): + get_response().add_javascript(['qommon.map.js']) + r = TemplateIO(html=True) + for widget in self.get_widgets(): + r += widget.render() + attrs = { + 'class': 'qommon-map', + 'id': 'map-%s' % self.name, + } + if self.value: + attrs['data-init-lat'], attrs['data-init-lng'] = self.value.split(';') + if self.readonly: + attrs['data-readonly'] = 'true' + r += htmltext('
' % ' '.join(['%s="%s"' % x for x in attrs.items()])) + return r.getvalue() + + def _parse(self, request): + CompositeWidget._parse(self, request) + self.value = self.get('latlng') diff --git a/wcs/qommon/http_response.py b/wcs/qommon/http_response.py index 6d8e2ab..a13bcb9 100644 --- a/wcs/qommon/http_response.py +++ b/wcs/qommon/http_response.py @@ -61,6 +61,10 @@ class HTTPResponse(quixote.http_response.HTTPResponse): self.javascript_scripts = [] for script_name in script_names: if not script_name in self.javascript_scripts: + if script_name == 'qommon.map.js': + self.add_javascript(['jquery.js']) + self.add_javascript(['../../leaflet/leaflet.js']) + self.add_css_include('../../leaflet/leaflet.css') self.javascript_scripts.append(str(script_name)) if script_name == 'afterjob.js': self.add_javascript_code('var QOMMON_ROOT_URL = "%s";\n' % \ diff --git a/wcs/qommon/static/css/qommon.css b/wcs/qommon/static/css/qommon.css index a753050..c6373e0 100644 --- a/wcs/qommon/static/css/qommon.css +++ b/wcs/qommon/static/css/qommon.css @@ -389,3 +389,7 @@ ul.select2-results { outline: 0; border: 1px solid #aaa; } + +div.qommon-map { + height: 280px; +} diff --git a/wcs/qommon/static/js/qommon.map.js b/wcs/qommon/static/js/qommon.map.js new file mode 100644 index 0000000..2b6d4cc --- /dev/null +++ b/wcs/qommon/static/js/qommon.map.js @@ -0,0 +1,27 @@ +$(function() { + $('.qommon-map').each(function() { + var map = L.map($(this).attr('id')).setView([50.84, 4.36], 13); + var hidden = $(this).prev(); + map.marker = null; + if ($(this).data('init-lat')) { + map.marker = L.marker([$(this).data('init-lat'), $(this).data('init-lng')]); + map.marker.addTo(map); + } + L.tileLayer( + 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', + { + attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA', + maxZoom: 18 + }).addTo(map); + if (! $(this).data('readonly')) { + map.on('click', function(e) { + if (map.marker === null) { + map.marker = L.marker([0, 0]); + map.marker.addTo(map); + } + map.marker.setLatLng(e.latlng); + hidden.val(e.latlng.lat + ';' + e.latlng.lng); + }); + } + }); +}); diff --git a/wcs/root.py b/wcs/root.py index dcb8d94..ce1c1a5 100644 --- a/wcs/root.py +++ b/wcs/root.py @@ -359,6 +359,10 @@ class RootDirectory(Directory): dirname = os.path.join(get_publisher().data_dir, 'qommon') return StaticDirectory(dirname, follow_symlinks = True) + # maps to locations provided by libjs-openlayers + if component == 'leaflet': + return StaticDirectory('/usr/share/javascript/leaflet') + # is this a category ? try: category = Category.get_by_urlname(component) -- 2.1.1