Reset bounds on drag end (fix #Leaflet/Leaflet.Editable#86)

This commit is contained in:
Yohan Boniface
2016-07-31 16:07:23 +02:00
parent f5dbdf228a
commit 360124d0ca
3 changed files with 58 additions and 5 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "leaflet.path.drag", "name": "leaflet.path.drag",
"version": "0.0.2", "version": "0.0.3",
"description": "Allow to drag Leaflet path", "description": "Allow to drag Leaflet path",
"main": "src/Path.Drag.js", "main": "src/Path.Drag.js",
"scripts": { "scripts": {
@ -25,7 +25,7 @@
"homepage": "https://github.com/Leaflet/Path.Drag.js#readme", "homepage": "https://github.com/Leaflet/Path.Drag.js#readme",
"devDependencies": { "devDependencies": {
"chai": "^3.5.0", "chai": "^3.5.0",
"leaflet": "^1.0.0-rc.1", "leaflet": "^1.0.0-rc.2",
"mocha": "^2.5.3", "mocha": "^2.5.3",
"mocha-phantomjs-core": "^1.3.1", "mocha-phantomjs-core": "^1.3.1",
"phantomjs-prebuilt": "^2.1.7", "phantomjs-prebuilt": "^2.1.7",

View File

@ -82,6 +82,7 @@ L.Handler.PathDrag = L.Handler.extend({
}, },
_onDragEnd: function (e) { _onDragEnd: function (e) {
if (this._path._bounds) this.resetBounds();
this._path.fire('moveend') this._path.fire('moveend')
.fire('dragend', e); .fire('dragend', e);
}, },
@ -98,6 +99,13 @@ L.Handler.PathDrag = L.Handler.extend({
var newLatLng = this._path._map.layerPointToLatLng(oldPoint); var newLatLng = this._path._map.layerPointToLatLng(oldPoint);
latlng.lat = newLatLng.lat; latlng.lat = newLatLng.lat;
latlng.lng = newLatLng.lng; latlng.lng = newLatLng.lng;
},
resetBounds: function () {
this._path._bounds = new L.LatLngBounds();
this._path.eachLatLng(function (latlng) {
this._bounds.extend(latlng);
});
} }
}); });

View File

@ -53,6 +53,21 @@ describe('L.Handler.PathDrag', function () {
}); });
}); });
it('should update bounds of a polyline', function (done) {
var latlngs = [p2ll(100, 100), p2ll(100, 200)];
layer = L.polyline(latlngs).addTo(this.map);
layer.dragging.enable();
var oldLat = layer._bounds._southWest.lat;
drag(100, 130, 20, 20, function () {
// Prosthetic-hand run the onStop before the
// dragend if sent.
window.setTimeout(function () {
assert.notEqual(oldLat, layer._bounds._southWest.lat);
done();
}, 10);
});
});
it('should drag a multipolyline', function (done) { it('should drag a multipolyline', function (done) {
var latlngs = [ var latlngs = [
[p2ll(100, 100), p2ll(100, 200)], [p2ll(100, 100), p2ll(100, 200)],
@ -98,6 +113,21 @@ describe('L.Handler.PathDrag', function () {
}); });
}); });
it('should update bounds of a polygon', function (done) {
var latlngs = [[p2ll(100, 150), p2ll(150, 200), p2ll(200, 100)]];
layer = L.polygon(latlngs).addTo(this.map);
layer.dragging.enable();
var oldLat = layer._bounds._southWest.lat;
drag(150, 150, 20, 20, function () {
// Prosthetic-hand run the onStop before the
// dragend if sent.
window.setTimeout(function () {
assert.notEqual(oldLat, layer._bounds._southWest.lat);
done();
}, 10);
});
});
it('should drag a rectangle', function (done) { it('should drag a rectangle', function (done) {
var latlngs = [p2ll(100, 100), p2ll(200, 200)]; var latlngs = [p2ll(100, 100), p2ll(200, 200)];
layer = L.rectangle(latlngs).addTo(this.map); layer = L.rectangle(latlngs).addTo(this.map);
@ -110,6 +140,21 @@ describe('L.Handler.PathDrag', function () {
}); });
}); });
it('should update bounds of a rectangle', function (done) {
var latlngs = [p2ll(100, 100), p2ll(200, 200)];
layer = L.rectangle(latlngs).addTo(this.map);
layer.dragging.enable();
var oldLat = layer._bounds._southWest.lat;
drag(100, 130, 20, 20, function () {
// Prosthetic-hand run the onStop before the
// dragend if sent.
window.setTimeout(function () {
assert.notEqual(oldLat, layer._bounds._southWest.lat);
done();
}, 10);
});
});
it('should drag a circle', function (done) { it('should drag a circle', function (done) {
layer = L.circle(p2ll(200, 200), {radius: 50}).addTo(this.map); layer = L.circle(p2ll(200, 200), {radius: 50}).addTo(this.map);
var before = layer._latlng.lat; var before = layer._latlng.lat;
@ -125,7 +170,7 @@ describe('L.Handler.PathDrag', function () {
var latlngs = [p2ll(100, 100), p2ll(100, 200)], var latlngs = [p2ll(100, 100), p2ll(100, 200)],
called = 0, called = 0,
call = function () {called++;}; call = function () {called++;};
layer = L.polyline(latlngs).addTo(this.map), layer = L.polyline(latlngs).addTo(this.map);
layer.on('dragstart', call); layer.on('dragstart', call);
layer.dragging.enable(); layer.dragging.enable();
assert.equal(called, 0); assert.equal(called, 0);
@ -139,7 +184,7 @@ describe('L.Handler.PathDrag', function () {
var latlngs = [p2ll(100, 100), p2ll(100, 200)], var latlngs = [p2ll(100, 100), p2ll(100, 200)],
called = 0, called = 0,
call = function () {called++;}; call = function () {called++;};
layer = L.polyline(latlngs).addTo(this.map), layer = L.polyline(latlngs).addTo(this.map);
layer.on('dragend', call); layer.on('dragend', call);
layer.dragging.enable(); layer.dragging.enable();
assert.equal(called, 0); assert.equal(called, 0);
@ -157,7 +202,7 @@ describe('L.Handler.PathDrag', function () {
var latlngs = [p2ll(100, 100), p2ll(100, 200)], var latlngs = [p2ll(100, 100), p2ll(100, 200)],
called = 0, called = 0,
call = function () {called++;}; call = function () {called++;};
layer = L.polyline(latlngs).addTo(this.map), layer = L.polyline(latlngs).addTo(this.map);
layer.on('drag', call); layer.on('drag', call);
layer.dragging.enable(); layer.dragging.enable();
assert.notOk(called); assert.notOk(called);