Skip to content

Commit

Permalink
# This is a combination of 2 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

checking distance to last point
fixes; touch event still appears to be firing twice on IE
change gitignore so we can deploy git repo
add touch blocker
block touch when touch has already begun
touch events and log
browser.touch typo
dont show tooltip text for touch
move browser touch check to disable showlength
try timeout approach
debugging intersections
try different distance method
alternate method of distance calculation
another approach to distance
function for approaching close distance
lower threshold for finishing shape and log
prevent L.browser unbinding issue

# This is the commit message #2:

remove conditional application of event handlers and onMouseMove call from onTouch
  • Loading branch information
Thomas Grice authored and Jon West committed Dec 18, 2016
1 parent 7381569 commit 15d3517
Showing 1 changed file with 74 additions and 13 deletions.
87 changes: 74 additions & 13 deletions src/draw/handler/Draw.Polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ L.Draw.Polyline = L.Draw.Feature.extend({
}

this._mouseMarker
.on('mousedown', this._onMouseDown, this)
.on('mouseout', this._onMouseOut, this)
.on('mouseup', this._onMouseUp, this) // Necessary for 0.8 compatibility
.on('mousemove', this._onMouseMove, this) // Necessary to prevent 0.8 stutter
.on('mousedown', this._onMouseDown, this)
.on('mouseup', this._onMouseUp, this) // Necessary for 0.8 compatibility
.addTo(this._map);

this._map
Expand All @@ -107,6 +107,7 @@ L.Draw.Polyline = L.Draw.Feature.extend({
.on('zoomlevelschange', this._onZoomEnd, this)
.on('touchstart', this._onTouch, this)
.on('zoomend', this._onZoomEnd, this);

}
},

Expand Down Expand Up @@ -174,8 +175,8 @@ L.Draw.Polyline = L.Draw.Feature.extend({
// Add a vertex to the end of the polyline
addVertex: function (latlng) {
var markersLength = this._markers.length;

if (markersLength > 0 && !this.options.allowIntersection && this._poly.newLatLngIntersects(latlng)) {
// markersLength must be greater than or equal to 2 before intersections can occur
if (markersLength >= 2 && !this.options.allowIntersection && this._poly.newLatLngIntersects(latlng)) {
this._showErrorTooltip();
return;
}
Expand Down Expand Up @@ -268,12 +269,17 @@ L.Draw.Polyline = L.Draw.Feature.extend({
},

_onMouseDown: function (e) {
var originalEvent = e.originalEvent;
var clientX = originalEvent.clientX;
var clientY = originalEvent.clientY;
this._startPoint.call(this, clientX, clientY);

if (!this._clickHandled && !this._touchHandled && !this._disableMarkers) {
this._onMouseMove(e);
this._clickHandled = true;
this._disableNewMarkers();
var originalEvent = e.originalEvent;
var clientX = originalEvent.clientX;
var clientY = originalEvent.clientY;
this._startPoint.call(this, clientX, clientY);
}
},

_startPoint: function (clientX, clientY) {
this._mouseDownOrigin = L.point(clientX, clientY);
},
Expand All @@ -283,28 +289,40 @@ L.Draw.Polyline = L.Draw.Feature.extend({
var clientX = originalEvent.clientX;
var clientY = originalEvent.clientY;
this._endPoint.call(this, clientX, clientY, e);
this._clickHandled = null;
},

_endPoint: function (clientX, clientY, e) {
if (this._mouseDownOrigin) {
var distance = L.point(clientX, clientY)
var dragCheckDistance = L.point(clientX, clientY)
.distanceTo(this._mouseDownOrigin);
if (Math.abs(distance) < 9 * (window.devicePixelRatio || 1)) {
var lastPtDistance = this._calculateFinishDistance(e.latlng);
if (lastPtDistance < 10 && L.Browser.touch) {
this._finishShape();
} else if (Math.abs(dragCheckDistance) < 9 * (window.devicePixelRatio || 1)) {
this.addVertex(e.latlng);
}
this._enableNewMarkers(); // after a short pause, enable new markers
}
this._mouseDownOrigin = null;
},

// ontouch prevented by clickHandled flag because some browsers fire both click/touch events,
// causing unwanted behavior
_onTouch: function (e) {
var originalEvent = e.originalEvent;
var clientX;
var clientY;
if (originalEvent.touches && originalEvent.touches[0]) {
if (originalEvent.touches && originalEvent.touches[0] && !this._clickHandled && !this._touchHandled && !this._disableMarkers) {
clientX = originalEvent.touches[0].clientX;
clientY = originalEvent.touches[0].clientY;
this._disableNewMarkers();
this._touchHandled = true;
this._startPoint.call(this, clientX, clientY);
this._endPoint.call(this, clientX, clientY, e);
this._touchHandled = null;
}
this._clickHandled = null;
},

_onMouseOut: function () {
Expand All @@ -313,6 +331,34 @@ L.Draw.Polyline = L.Draw.Feature.extend({
}
},

// calculate if we are currently within close enough distance
// of the closing point (first point for shapes, last point for lines)
// this is semi-ugly code but the only reliable way i found to get the job done
// note: calculating point.distanceTo between mouseDownOrigin and last marker did NOT work
_calculateFinishDistance: function (potentialLatLng) {
var lastPtDistance
if (this._markers.length > 0) {
var finishMarker;
if (this.type === L.Draw.Polyline.TYPE) {
finishMarker = this._markers[this._markers.length - 1];
} else if (this.type === L.Draw.Polygon.TYPE) {
finishMarker = this._markers[0];
} else {
return Infinity;
}
var lastMarkerPoint = this._map.latLngToContainerPoint(finishMarker.getLatLng()),
potentialMarker = new L.Marker(potentialLatLng, {
icon: this.options.icon,
zIndexOffset: this.options.zIndexOffset * 2
});
var potentialMarkerPint = this._map.latLngToContainerPoint(potentialMarker.getLatLng());
lastPtDistance = lastMarkerPoint.distanceTo(potentialMarkerPint);
} else {
lastPtDistance = Infinity;
}
return lastPtDistance;
},

_updateFinishHandler: function () {
var markerCount = this._markers.length;
// The last marker should have a click handler to close the polyline
Expand Down Expand Up @@ -419,7 +465,9 @@ L.Draw.Polyline = L.Draw.Feature.extend({
_getTooltipText: function () {
var showLength = this.options.showLength,
labelText, distanceStr;

if (L.Browser.touch) {
showLength = false; // if there's a better place to put this, feel free to move it
}
if (this._markers.length === 0) {
labelText = {
text: L.drawLocal.draw.handlers.polyline.tooltip.start
Expand Down Expand Up @@ -506,6 +554,19 @@ L.Draw.Polyline = L.Draw.Feature.extend({
}
},

// disable new markers temporarily;
// this is to prevent duplicated touch/click events in some browsers
_disableNewMarkers: function () {
this._disableMarkers = true;
},

// see _disableNewMarkers
_enableNewMarkers: function () {
setTimeout(function() {
this._disableMarkers = false;
}.bind(this), 50);
},

_cleanUpShape: function () {
if (this._markers.length > 1) {
this._markers[this._markers.length - 1].off('click', this._finishShape, this);
Expand Down

0 comments on commit 15d3517

Please sign in to comment.