Skip to content

Commit

Permalink
Removed console log, added test, added dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner committed Mar 20, 2016
1 parent 4a1e200 commit 3609858
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
63 changes: 59 additions & 4 deletions dist/leaflet.draw-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -1138,11 +1138,18 @@ L.Edit.Poly = L.Handler.extend({
icon: new L.DivIcon({
iconSize: new L.Point(8, 8),
className: 'leaflet-div-icon leaflet-editing-icon'
})
}),
drawError: {
color: '#b00b00',
timeout: 1000
},
},

initialize: function (poly, options) {
this._poly = poly;
if (options && options.drawError) {
options.drawError = L.Util.extend({}, this.options.drawError, options.drawError);
}
L.setOptions(this, options);
},

Expand Down Expand Up @@ -1251,6 +1258,7 @@ L.Edit.Poly = L.Handler.extend({

_onMarkerDrag: function (e) {
var marker = e.target;
var poly = this._poly;

L.extend(marker._origLatLng, marker._latlng);

Expand All @@ -1261,6 +1269,35 @@ L.Edit.Poly = L.Handler.extend({
marker._middleRight.setLatLng(this._getMiddleLatLng(marker, marker._next));
}

if (poly.options.poly) {
var tooltip = poly._map._editTooltip; // Access the tooltip

// If we don't allow intersects and the polygon intersects
if (!poly.options.poly.allowIntersection && poly.intersects()) {

var originalColor = poly.options.color;
poly.setStyle({ color: this.options.drawError.color });

if (tooltip) {
tooltip.updateContent({
text: L.drawLocal.draw.handlers.polyline.error
});
}

// Reset everything back to normal after a second
setTimeout(function(){
poly.setStyle({ color: originalColor });
if (tooltip) {
tooltip.updateContent({
text: L.drawLocal.edit.handlers.edit.tooltip.text,
subtext: L.drawLocal.edit.handlers.edit.tooltip.subtext
});
}
}, 1000);
this._onMarkerClick(e); // Reset the marker to it's original position
}
}

this._poly.redraw();
},

Expand Down Expand Up @@ -1345,11 +1382,13 @@ L.Edit.Poly = L.Handler.extend({
};

onDragEnd = function () {

marker.off('dragstart', onDragStart, this);
marker.off('dragend', onDragEnd, this);

this._createMiddleMarker(marker1, marker);
this._createMiddleMarker(marker, marker2);

};

onClick = function () {
Expand Down Expand Up @@ -1392,7 +1431,8 @@ L.Polyline.addInitHook(function () {
}

if (L.Edit.Poly) {
this.editing = new L.Edit.Poly(this);

this.editing = new L.Edit.Poly(this, this.options.poly);

if (this.options.editable) {
this.editing.enable();
Expand Down Expand Up @@ -2496,6 +2536,7 @@ L.EditToolbar = L.Toolbar.extend({
}
},
remove: {},
poly: null,
featureGroup: null /* REQUIRED! TODO: perhaps if not set then all layers on the map are selectable? */
},

Expand All @@ -2512,6 +2553,10 @@ L.EditToolbar = L.Toolbar.extend({
options.remove = L.extend({}, this.options.remove, options.remove);
}

if (options.poly) {
options.poly = L.extend({}, this.options.poly, options.poly);
}

this._toolbarClass = 'leaflet-draw-edit';
L.Toolbar.prototype.initialize.call(this, options);

Expand All @@ -2525,7 +2570,8 @@ L.EditToolbar = L.Toolbar.extend({
enabled: this.options.edit,
handler: new L.EditToolbar.Edit(map, {
featureGroup: featureGroup,
selectedPathOptions: this.options.edit.selectedPathOptions
selectedPathOptions: this.options.edit.selectedPathOptions,
poly : this.options.poly
}),
title: L.drawLocal.edit.toolbar.buttons.edit
},
Expand Down Expand Up @@ -2692,6 +2738,9 @@ L.EditToolbar.Edit = L.Handler.extend({
subtext: L.drawLocal.edit.handlers.edit.tooltip.subtext
});

// Quickly access the tooltip to update for intersection checking
map._editTooltip = this._tooltip;

this._map.on('mousemove', this._onMouseMove, this);
}
},
Expand Down Expand Up @@ -2770,11 +2819,16 @@ L.EditToolbar.Edit = L.Handler.extend({

_enableLayerEdit: function (e) {
var layer = e.layer || e.target || e,
pathOptions;
pathOptions, poly;

// Back up this layer (if haven't before)
this._backupLayer(layer);

if (this.options.poly) {
poly = L.Util.extend({}, this.options.poly);
layer.options.poly = poly;
}

// Set different style for editing mode
if (this.options.selectedPathOptions) {
pathOptions = L.Util.extend({}, this.options.selectedPathOptions);
Expand All @@ -2787,6 +2841,7 @@ L.EditToolbar.Edit = L.Handler.extend({

layer.options.original = L.extend({}, layer.options);
layer.options.editing = pathOptions;

}

layer.editing.enable();
Expand Down
4 changes: 2 additions & 2 deletions dist/leaflet.draw.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"happen": "~0.1.3",
"karma": "^0.12.19",
"karma-mocha": "~0.1.0",
"karma-coverage": "~0.1.3"
"karma-coverage": "~0.1.3",
"phantomjs": "~2.1.3",
"karma-phantomjs-launcher": "~1.0.0"
},
"main": "dist/leaflet.draw.js",
"directories": {
Expand Down
18 changes: 16 additions & 2 deletions spec/suites/EditSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ describe("L.Edit", function () {
drawnItems = new L.FeatureGroup().addTo(map);
edit = new L.EditToolbar.Edit(map, {
featureGroup: drawnItems,
poly: {
allowIntersection : false
},
selectedPathOptions: L.EditToolbar.prototype.options.edit.selectedPathOptions
});
poly = new L.Polyline(L.latLng(41, -87), L.latLng(42, -88));
Expand All @@ -59,13 +62,24 @@ describe("L.Edit", function () {
});

it("Should revert to original styles when editing is toggled.", function () {
var originalOptions = L.extend({maintainColor: false }, poly.options);
var originalOptions = L.extend({maintainColor: false, poly : {allowIntersection: false} }, poly.options);

drawnItems.addLayer(poly);
edit.enable();
edit.disable();

expect(poly.options).to.eql(originalOptions);
});

it("Should set allowIntersection to be false when setting is set", function () {

drawnItems.addLayer(poly);
edit.enable();

expect(poly.editing.enabled()).to.equal(true);
expect(poly.options.poly.allowIntersection).to.equal(false);

});

});
});
});
10 changes: 5 additions & 5 deletions src/edit/handler/Edit.Poly.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ L.Edit.Poly = L.Handler.extend({
if (poly.options.poly) {
var tooltip = poly._map._editTooltip; // Access the tooltip

// If we don't allow intersects and the polygon intersects
// If we don't allow intersections and the polygon intersects
if (!poly.options.poly.allowIntersection && poly.intersects()) {

var originalColor = poly.options.color;
Expand All @@ -152,23 +152,23 @@ L.Edit.Poly = L.Handler.extend({
if (tooltip) {
tooltip.updateContent({
text: L.drawLocal.draw.handlers.polyline.error
});
});
}

// Reset everything back to normal after a second
setTimeout(function(){
setTimeout(function () {
poly.setStyle({ color: originalColor });
if (tooltip) {
tooltip.updateContent({
text: L.drawLocal.edit.handlers.edit.tooltip.text,
subtext: L.drawLocal.edit.handlers.edit.tooltip.subtext
});
});
}
}, 1000);
this._onMarkerClick(e); // Reset the marker to it's original position
}
}

this._poly.redraw();
},

Expand Down
2 changes: 0 additions & 2 deletions src/edit/handler/EditToolbar.Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ L.EditToolbar.Edit = L.Handler.extend({
// Back up this layer (if haven't before)
this._backupLayer(layer);

console.log(this._tooltip, this.tooltip, layer);

if (this.options.poly) {
poly = L.Util.extend({}, this.options.poly);
layer.options.poly = poly;
Expand Down

0 comments on commit 3609858

Please sign in to comment.