Projet

Général

Profil

0001-map-field-work-in-progress.patch

Frédéric Péters, 10 octobre 2014 20:01

Télécharger (5,65 ko)

Voir les différences:

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
wcs/fields.py
15 15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import time
18
import random
18 19
import re
19 20
import xml.etree.ElementTree as ET
20 21

  
......
1348 1349
register_field_class(TableRowsField)
1349 1350

  
1350 1351

  
1352
class MapField(WidgetField):
1353
    key = 'map'
1354
    description = N_('Map')
1355

  
1356
    widget_class = MapWidget
1357

  
1358
    def get_view_value(self, value):
1359
        widget = self.widget_class('x%s' % random.random(), value, readonly=True)
1360
        return widget.render_content()
1361

  
1362
    def get_rst_view_value(self, value, indent=''):
1363
        return indent + value
1364

  
1365
register_field_class(MapField)
1366

  
1367

  
1351 1368
class RankedItemsField(WidgetField):
1352 1369
    key = 'ranked-items'
1353 1370
    description = N_('Ranked Items')
wcs/qommon/form.py
1747 1747
            self.value = hashlib.sha1(pwd1).hexdigest()
1748 1748
        else:
1749 1749
            self.value = None
1750

  
1751

  
1752
class MapWidget(CompositeWidget):
1753
    def __init__(self, name, value=None, **kwargs):
1754
        CompositeWidget.__init__(self, name, value, **kwargs)
1755
        self.add(HiddenWidget, 'latlng')
1756
        self.readonly = kwargs.pop('readonly', False)
1757

  
1758
    def render_content(self):
1759
        get_response().add_javascript(['qommon.map.js'])
1760
        r = TemplateIO(html=True)
1761
        for widget in self.get_widgets():
1762
            r += widget.render()
1763
        attrs = {
1764
            'class': 'qommon-map',
1765
            'id': 'map-%s' % self.name,
1766
        }
1767
        if self.value:
1768
            attrs['data-init-lat'], attrs['data-init-lng'] = self.value.split(';')
1769
        if self.readonly:
1770
            attrs['data-readonly'] = 'true'
1771
        r += htmltext('<div %s></div>' % ' '.join(['%s="%s"' % x for x in attrs.items()]))
1772
        return r.getvalue()
1773

  
1774
    def _parse(self, request):
1775
        CompositeWidget._parse(self, request)
1776
        self.value = self.get('latlng')
wcs/qommon/http_response.py
61 61
            self.javascript_scripts = []
62 62
        for script_name in script_names:
63 63
            if not script_name in self.javascript_scripts:
64
                if script_name == 'qommon.map.js':
65
                    self.add_javascript(['jquery.js'])
66
                    self.add_javascript(['../../leaflet/leaflet.js'])
67
                    self.add_css_include('../../leaflet/leaflet.css')
64 68
                self.javascript_scripts.append(str(script_name))
65 69
                if script_name == 'afterjob.js':
66 70
                    self.add_javascript_code('var QOMMON_ROOT_URL = "%s";\n' % \
wcs/qommon/static/css/qommon.css
389 389
        outline: 0;
390 390
        border: 1px solid #aaa;
391 391
}
392

  
393
div.qommon-map {
394
	height: 280px;
395
}
wcs/qommon/static/js/qommon.map.js
1
$(function() {
2
  $('.qommon-map').each(function() {
3
     var map = L.map($(this).attr('id')).setView([50.84, 4.36], 13);
4
     var hidden = $(this).prev();
5
     map.marker = null;
6
     if ($(this).data('init-lat')) {
7
       map.marker = L.marker([$(this).data('init-lat'), $(this).data('init-lng')]);
8
       map.marker.addTo(map);
9
     }
10
     L.tileLayer(
11
        'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
12
        {
13
                attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
14
                maxZoom: 18
15
        }).addTo(map);
16
     if (! $(this).data('readonly')) {
17
       map.on('click', function(e) {
18
         if (map.marker === null) {
19
           map.marker = L.marker([0, 0]);
20
           map.marker.addTo(map);
21
         }
22
         map.marker.setLatLng(e.latlng);
23
         hidden.val(e.latlng.lat + ';' + e.latlng.lng);
24
       });
25
     }
26
  });
27
});
wcs/root.py
359 359
            dirname = os.path.join(get_publisher().data_dir, 'qommon')
360 360
            return StaticDirectory(dirname, follow_symlinks = True)
361 361

  
362
        # maps to locations provided by libjs-openlayers
363
        if component == 'leaflet':
364
            return StaticDirectory('/usr/share/javascript/leaflet')
365

  
362 366
        # is this a category ?
363 367
        try:
364 368
            category = Category.get_by_urlname(component)
365
-