Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Changelog
1.5 (unreleased)
----------------

- Nothing changed yet.
- Geosearch bounds: Patched ``L.GeoSearch.Provider.Esri`` to also return the
location bounds. Added ``bounds`` field to ``IGeolocation``. Bounds are
updated on pan, zoom, click and address lookup.
[david-batranu]


1.4 (2015-11-26)
Expand Down
27 changes: 24 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

patch: {
'l.geosearch.provider.esri.js': {
options: {
patch: 'patches/l.geosearch.provider.esri.js.patch'
},
files: {
'patches/l.geosearch.provider.esri.js': 'bower_components/L.GeoSearch/src/js/l.geosearch.provider.esri.js'
}
}
},

concat: {
options: {
separator: grunt.util.linefeed + grunt.util.linefeed
Expand All @@ -17,7 +28,7 @@ module.exports = function(grunt) {
'bower_components/Leaflet.fullscreen/dist/Leaflet.fullscreen.js',
'bower_components/leaflet-providers/leaflet-providers.js',
'bower_components/L.GeoSearch/src/js/l.control.geosearch.js',
'bower_components/L.GeoSearch/src/js/l.geosearch.provider.esri.js',
'patches/l.geosearch.provider.esri.js',
'bower_components/leaflet.markercluster/dist/leaflet.markercluster-src.js',
'bower_components/Leaflet.awesome-markers/dist/leaflet.awesome-markers.js'
],
Expand Down Expand Up @@ -89,17 +100,27 @@ module.exports = function(grunt) {
path: dest_path + 'libs.css',
pattern: 'fullscreen@2x.png',
replacement: '++resource++plone.formwidget.geolocation/images/fullscreen@2x.png'
},
}
},

clean: {
patches: {
src: [
'patches/l.geosearch.provider.esri.js'
]
}
}

});

grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-patcher');
grunt.loadNpmTasks('grunt-sed');

// Default task(s).
grunt.registerTask('default', ['concat', 'uglify', 'copy', 'sed']);
grunt.registerTask('default', ['patch', 'concat', 'uglify', 'copy', 'sed', 'clean']);

};
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"leaflet.markercluster": "v0.4",
"Leaflet.fullscreen": "git@github.com:Leaflet/Leaflet.fullscreen#v0.0.4",
"leaflet-providers": "1.1.6",
"L.GeoSearch": "git@github.com:smeijer/L.GeoSearch.git#master",
"L.GeoSearch": "git@github.com:smeijer/L.GeoSearch.git#9417b3a3447a9b41ef69870569c7ecf205193fc9",
"Leaflet.awesome-markers": "v2.0.2"
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"grunt-contrib-concat": "",
"grunt-contrib-uglify": "",
"grunt-contrib-copy": "",
"grunt-sed": ""
"grunt-contrib-clean": "",
"grunt-sed": "",
"grunt-patcher": ""
},
"repository": {
"type": "git",
Expand Down
28 changes: 28 additions & 0 deletions patches/l.geosearch.provider.esri.js.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--- <unnamed>
+++ <unnamed>
@@ -29,12 +29,20 @@
return [];

var results = [];
- for (var i = 0; i < data.locations.length; i++)
+ for (var i = 0; i < data.locations.length; i++) {
+ var loc = data.locations[i];
+ var bounds = new L.LatLngBounds([
+ new L.LatLng(loc.extent.ymax, loc.extent.xmax),
+ new L.LatLng(loc.extent.ymin, loc.extent.xmin),
+ ]);
+
results.push(new L.GeoSearch.Result(
- data.locations[i].feature.geometry.x,
- data.locations[i].feature.geometry.y,
- data.locations[i].name
+ loc.feature.geometry.x,
+ loc.feature.geometry.y,
+ loc.name,
+ bounds
));
+ }

return results;
}

39 changes: 39 additions & 0 deletions plone/formwidget/geolocation/bounds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from zope import schema
from zope.i18nmessageid import MessageFactory
from zope.schema.interfaces import IObject
from zope.interface import implements
from zope.interface import Interface


_ = MessageFactory('plone.formwidget.geolocation')


class IBounds(Interface):
south = schema.Float(title=_(u'South'))
west = schema.Float(title=_(u'West'))
north = schema.Float(title=_(u'North'))
east = schema.Float(title=_(u'East'))


class Bounds(object):
implements(IBounds)

def __init__(self, south=0, west=0, north=0, east=0):
self.south = float(south)
self.west = float(west)
self.north = float(north)
self.east = float(east)


class IBoundsField(IObject):
pass


class BoundsField(schema.Object):
implements(IBoundsField)

_type = Bounds
schema = IBounds

def __init__(self, **kw):
super(BoundsField, self).__init__(schema=self.schema, **kw)
19 changes: 15 additions & 4 deletions plone/formwidget/geolocation/converter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from plone.formwidget.geolocation.geolocation import Geolocation
from plone.formwidget.geolocation.bounds import Bounds
from plone.formwidget.geolocation.interfaces import IGeolocation
from plone.formwidget.geolocation.interfaces import IGeolocationField
from plone.formwidget.geolocation.interfaces import IGeolocationWidget
Expand All @@ -7,19 +8,29 @@


class GeolocationConverter(BaseDataConverter):
"""Converts from a 2-tuple to a Geolocation
"""Converts from a 6-tuple to a Geolocation
"""
adapts(IGeolocationField, IGeolocationWidget)

def toWidgetValue(self, value):
if value:
return (value.latitude, value.longitude)
bounds = getattr(value, 'bounds', Bounds(0, 0, 0, 0))
return (
value.latitude,
value.longitude,
bounds.south,
bounds.west,
bounds.north,
bounds.east,
)

def toFieldValue(self, value):
if value is None or value == ('0', '0'):
if value is None or value == ('0', '0', '0', '0', '0', '0'):
return self.field.missing_value

if IGeolocation.providedBy(value):
return value

return Geolocation(value[0], value[1])
bounds = Bounds(*value[2:])

return Geolocation(value[0], value[1], bounds)
3 changes: 2 additions & 1 deletion plone/formwidget/geolocation/geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Geolocation(object):
implements(IGeolocation)

def __init__(self, latitude=0, longitude=0):
def __init__(self, latitude=0, longitude=0, bounds=None):
self.latitude = float(latitude)
self.longitude = float(longitude)
self.bounds = bounds
24 changes: 24 additions & 0 deletions plone/formwidget/geolocation/geolocation_input.pt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,29 @@
class string:${view/klass} longitude;
style view/style;
value python:view.value[1]" />
<input type="hidden" class="bounds bounds-south"
tal:attributes="id string:${view/id}_bounds_south;
name string:${view/name}:tuple;
class string:${view/klass} bounds bounds-south;
style view/style;
value python:view.value[2]" />
<input type="hidden" class="bounds bounds-west"
tal:attributes="id string:${view/id}_bounds_west;
name string:${view/name}:tuple;
class string:${view/klass} bounds bounds-west;
style view/style;
value python:view.value[3]" />
<input type="hidden" class="bounds bounds-north"
tal:attributes="id string:${view/id}_bounds_north;
name string:${view/name}:tuple;
class string:${view/klass} bounds bounds-north;
style view/style;
value python:view.value[4]" />
<input type="hidden" class="bounds bounds-east"
tal:attributes="id string:${view/id}_bounds_east;
name string:${view/name}:tuple;
class string:${view/klass} bounds bounds-east;
style view/style;
value python:view.value[5]" />
</div>
</html>
2 changes: 2 additions & 0 deletions plone/formwidget/geolocation/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from zope.i18nmessageid import MessageFactory
from zope.interface import Interface
from zope.schema.interfaces import IObject
from plone.formwidget.geolocation import bounds


_ = MessageFactory('plone.formwidget.geolocation')
Expand All @@ -11,6 +12,7 @@
class IGeolocation(Interface):
latitude = schema.Float(title=_(u'Latitude'))
longitude = schema.Float(title=_(u'Longitude'))
bounds = bounds.BoundsField(title=_(u'Bounds'))


class IGeolocationField(IObject):
Expand Down
37 changes: 31 additions & 6 deletions plone/formwidget/geolocation/resources/libs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ var oldL = window.L,

L.version = '0.7.7';

// define Leaflet for Node module pattern loaders, including Browserify
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = L;

// define Leaflet as an AMD module
} else if (typeof define === 'function' && define.amd) {
define(L);
}

// define Leaflet as a global L variable, saving the original L to restore later if needed

L.noConflict = function () {
Expand Down Expand Up @@ -9301,8 +9310,16 @@ L.control.fullscreen = function (options) {


(function (root, factory) {
// Assume Leaflet is loaded into global object L already
factory(L);
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['leaflet'], factory);
} else if (typeof modules === 'object' && module.exports) {
// define a Common JS module that relies on 'leaflet'
module.exports = factory(require('leaflet'));
} else {
// Assume Leaflet is loaded into global object L already
factory(L);
}
}(this, function (L) {
'use strict';

Expand Down Expand Up @@ -10259,12 +10276,20 @@ L.GeoSearch.Provider.Esri = L.Class.extend({
return [];

var results = [];
for (var i = 0; i < data.locations.length; i++)
for (var i = 0; i < data.locations.length; i++) {
var loc = data.locations[i];
var bounds = new L.LatLngBounds([
new L.LatLng(loc.extent.ymax, loc.extent.xmax),
new L.LatLng(loc.extent.ymin, loc.extent.xmin),
]);

results.push(new L.GeoSearch.Result(
data.locations[i].feature.geometry.x,
data.locations[i].feature.geometry.y,
data.locations[i].name
loc.feature.geometry.x,
loc.feature.geometry.y,
loc.name,
bounds
));
}

return results;
}
Expand Down
12 changes: 6 additions & 6 deletions plone/formwidget/geolocation/resources/libs.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plone/formwidget/geolocation/resources/libs.min.js.map

Large diffs are not rendered by default.

53 changes: 46 additions & 7 deletions plone/formwidget/geolocation/resources/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,44 @@
map_wrap = $(el).closest('div.geolocation_wrapper');
editable = map_wrap.hasClass('edit');

var update_inputs = function(lat, lng) {
map_wrap.find('input.latitude').attr('value', lat);
map_wrap.find('input.longitude').attr('value', lng);
var get_bounds_object = function(bounds) {
return {
south: bounds.getSouth(),
west: bounds.getWest(),
north: bounds.getNorth(),
east: bounds.getEast()
};
};

var set_map_bounds = function(map, bounds_obj) {
var bounds = new L.LatLngBounds([
new L.LatLng(bounds_obj.south, bounds_obj.west),
new L.LatLng(bounds_obj.north, bounds_obj.east),
]);
if (bounds && bounds.isValid()) {
map.fitBounds(bounds);
}
};

var update_inputs = function(lat, lng, bounds) {
if (lat && lng) {
map_wrap.find('input.latitude').attr('value', lat);
map_wrap.find('input.longitude').attr('value', lng);
}

if (bounds) {
map_wrap.find('input.bounds-south').attr('value', bounds.south);
map_wrap.find('input.bounds-west').attr('value', bounds.west);
map_wrap.find('input.bounds-north').attr('value', bounds.north);
map_wrap.find('input.bounds-east').attr('value', bounds.east);
}
};

var bind_draggable_marker = function (marker) {
marker.on('dragend', function(e) {
var coords = e.target.getLatLng();
update_inputs(coords.lat, coords.lng);
var bounds = get_bounds_object(e.target._map.getBounds());
update_inputs(coords.lat, coords.lng, bounds);
});
};

Expand Down Expand Up @@ -79,17 +108,27 @@
map.addLayer(markers);

// autozoom
bounds = markers.getBounds();
map.fitBounds(bounds);
if (geopoints[0].bounds) {
set_map_bounds(map, geopoints[0].bounds);
} else {
map.fitBounds(markers.getBounds());
}


if (editable) {
map.on('geosearch_showlocation', function(e) {
map.removeLayer(markers);
var coords = e.Location;
update_inputs(coords.Y, coords.X);
var bounds = get_bounds_object(coords.bounds);
update_inputs(coords.Y, coords.X, bounds);
bind_draggable_marker(e.Marker);
});

map.on('dragend zoomend', function(e){
var bounds = get_bounds_object(e.target.getBounds());
update_inputs(0, 0, bounds)
});

// GEOSEARCH
geosearch = new L.Control.GeoSearch({
showMarker: true,
Expand Down
Loading