commit 6f33a041a2cf2b2eb6759b9fce54c57e2f590e05
parent 35eb685dc0826b76f98967b631a1400969615f07
Author: Sergey Orlov <wladimirych@gmail.com>
Date: Mon, 4 May 2020 22:48:23 +0200
drag events: add option to allow tracking drag events outside of element
Related to #16
Diffstat:
2 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/src/lib/leaflet.control.elevation-profile/index.js b/src/lib/leaflet.control.elevation-profile/index.js
@@ -197,7 +197,7 @@ const ElevationProfile = L.Class.extend({
L.DomEvent.on(svg, 'mousemove', this.onSvgEnter, this);
L.DomEvent.on(svg, 'mouseleave', this.onSvgLeave, this);
L.DomEvent.on(svg, 'mousewheel', this.onSvgMouseWheel, this);
- this.svgDragEvents = new DragEvents(this.svg, {dragButtons: [0, 2]});
+ this.svgDragEvents = new DragEvents(this.svg, {dragButtons: [0, 2], stopOnLeave: true});
this.svgDragEvents.on('dragstart', this.onSvgDragStart, this);
this.svgDragEvents.on('dragend', this.onSvgDragEnd, this);
this.svgDragEvents.on('drag', this.onSvgDrag, this);
diff --git a/src/lib/leaflet.events.drag/index.js b/src/lib/leaflet.events.drag/index.js
@@ -24,20 +24,20 @@ function movementFromEvents(e1, e2) {
const DragEvents = L.Evented.extend({
options: {
dragTolerance: 2,
- dragButtons: [0]
+ dragButtons: [0],
+ stopOnLeave: false,
},
- initialize: function(eventsSource, options) {
+ initialize: function(element, options) {
L.setOptions(this, options);
+ this.element = element;
+
this.dragButton = null;
this.startEvent = null;
this.prevEvent = null;
this.isDragging = false;
- L.DomEvent.on(eventsSource, 'mousemove', this.onMouseMove, this);
- L.DomEvent.on(eventsSource, 'mouseup', this.onMouseUp, this);
- L.DomEvent.on(eventsSource, 'mousedown', this.onMouseDown, this);
- L.DomEvent.on(eventsSource, 'mouseleave', this.onMouseLeave, this);
+ L.DomEvent.on(element, 'mousedown', this.onMouseDown, this);
},
onMouseDown: function(e) {
@@ -47,6 +47,12 @@ const DragEvents = L.Evented.extend({
this.startEvent = this.prevEvent = e;
L.DomUtil.disableImageDrag();
L.DomUtil.disableTextSelection();
+ this._moveHandler = this.onMouseMove.bind(this);
+ document.addEventListener('mousemove', this._moveHandler, true);
+ L.DomEvent.on(document, 'mouseup', this.onMouseUp, this);
+ if (this.options.stopOnLeave) {
+ L.DomEvent.on(this.element, 'mouseleave', this.onMouseLeave, this);
+ }
}
},
@@ -68,12 +74,14 @@ const DragEvents = L.Evented.extend({
);
}
this.dragButton = null;
+ document.removeEventListener('mousemove', this._moveHandler, true);
+ L.DomEvent.off(document, 'mouseup', this.onMouseUp, this);
+ L.DomEvent.off(this.element, 'mouseleave', this.onMouseLeave, this);
}
},
onMouseMove: function(e) {
var that = this;
-
function exceedsTolerance() {
var tolerance = that.options.dragTolerance;
return Math.abs(e.clientX - that.startEvent.clientX) > tolerance ||