Skip to content

Commit

Permalink
Merge pull request #186 from codeofsumit/update-bounds
Browse files Browse the repository at this point in the history
correctly update bounds of shapes
  • Loading branch information
codeofsumit authored Jul 25, 2017
2 parents 3d82cce + c088f60 commit a4b094f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"start": "npm run dev",
"dev": "./node_modules/.bin/webpack --config=webpack.dev.js",
"test": "browserify ./spec/test.spec.js | tape-run",
"prepublish": "./node_modules/.bin/webpack --config=webpack.build.js"
"build": "./node_modules/.bin/webpack --config=webpack.build.js",
"prepublish": "npm run build"
},
"repository": {
"type": "git",
Expand Down
20 changes: 18 additions & 2 deletions src/js/Edit/L.PM.Edit.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ Edit.Line = Edit.extend({

coords.splice(index, 0, latlng);

// set new latlngs to trigger bounds update
this._layer.setLatLngs(coords);

// associate polygon coordinate with marker coordinate
newM._origLatLng = coords[index];

Expand Down Expand Up @@ -216,6 +219,9 @@ Edit.Line = Edit.extend({
// remove polygon coordinate from this marker
coords.splice(index, 1);

// set new latlngs to trigger bounds update
this._layer.setLatLngs(coords);

// if the poly has no coordinates left, remove the layer
// else, redraw it
if(coords.length < 1) {
Expand Down Expand Up @@ -257,6 +263,15 @@ Edit.Line = Edit.extend({
this._fireEdit();
},

updatePolygonCoordsFromMarkerDrag(marker) {
// update polygon coords
const coords = this._layer.getLatLngs();
const index = marker._index;

coords.splice(index, 1, marker.getLatLng());
this._layer.setLatLngs(coords).redraw();
},

_onMarkerDrag(e) {
// dragged marker
const marker = e.target;
Expand All @@ -270,9 +285,10 @@ Edit.Line = Edit.extend({
const nextMarkerIndex = marker._index + 1 >= this._markers.length ? 0 : marker._index + 1;
const prevMarkerIndex = marker._index - 1 < 0 ? this._markers.length - 1 : marker._index - 1;

// update marker coordinates which will update polygon coordinates
// update marker coordinates
L.extend(marker._origLatLng, marker._latlng);
this._layer.redraw();

this.updatePolygonCoordsFromMarkerDrag(marker);

// update middle markers on the left and right
// be aware that "next" and "prev" might be interchanged, depending on the geojson array
Expand Down
15 changes: 15 additions & 0 deletions src/js/Edit/L.PM.Edit.Poly.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ Edit.Poly = Edit.Line.extend({
}
},

updatePolygonCoordsFromMarkerDrag(marker) {
// update polygon coords
const coords = this._layer.getLatLngs()[0];
const index = marker._index;

coords.splice(index, 1, marker.getLatLng());
this._layer.setLatLngs(coords).redraw();
},

// adds a new marker from a middlemarker
_addMarker(newM, leftM, rightM) {
// first, make this middlemarker a regular marker
Expand All @@ -46,6 +55,9 @@ Edit.Poly = Edit.Line.extend({

coords.splice(index, 0, latlng);

// set new latlngs to trigger bounds update
this._layer.setLatLngs(coords);

// associate polygon coordinate with marker coordinate
newM._origLatLng = coords[index];

Expand Down Expand Up @@ -86,6 +98,9 @@ Edit.Poly = Edit.Line.extend({
// remove polygon coordinate from this marker
coords.splice(index, 1);

// set new latlngs to trigger bounds update
this._layer.setLatLngs(coords);

// if the poly has no coordinates left, remove the layer
// else, redraw it
if(coords.length < 1) {
Expand Down
5 changes: 2 additions & 3 deletions src/js/L.PM.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ L.PM = L.PM || {
},
addInitHooks() {
function initLayerGroup() {
if(!this.options.pmIgnore) {
this.pm = new L.PM.Edit.LayerGroup(this);
}
this.pm = new L.PM.Edit.LayerGroup(this);
}

L.LayerGroup.addInitHook(initLayerGroup);


function initMarker() {
if(!this.options.pmIgnore) {
this.pm = new L.PM.Edit.Marker(this);
Expand Down

0 comments on commit a4b094f

Please sign in to comment.