brouter-web/js/util/ClickTolerantBoxZoom.js
2024-10-19 13:44:15 +02:00

55 lines
1.8 KiB
JavaScript

/**
* Avoids conflict between shift-click and shift-drag.
* Extends BoxZoom to support a small click tolerance like in Draggable and
* a larger drag tolerance as a "neutral zone" before starting with box zoom dragging,
* to avoid accidental zooms.
*/
BR.ClickTolerantBoxZoom = L.Map.BoxZoom.extend({
clickTolerance: L.Draggable.prototype.options.clickTolerance,
// use more than clickTolerance before starting box zoom to surely avoid accidental zooms
dragTolerance: 15,
// flag to enable or disable click/drag tolerance, classic BoxZoom behaviour when false
tolerant: true,
// "neutral zone", state between clickTolerance and dragTolerance,
// already signals dragging to map and thus prevents click
_preMoved: false,
moved() {
return this._preMoved || this._moved;
},
_resetState() {
L.Map.BoxZoom.prototype._resetState.call(this);
this._preMoved = false;
},
_onMouseMove(e) {
if (!this._moved) {
const point = this._map.mouseEventToContainerPoint(e);
// derived from L.Draggable._onMove
var offsetPoint = point.clone()._subtract(this._startPoint);
var offset = Math.abs(offsetPoint.x || 0) + Math.abs(offsetPoint.y || 0);
if (this.tolerant && offset < this.dragTolerance) {
if (!this._preMoved && offset >= this.clickTolerance) {
this._preMoved = true;
}
return;
}
}
L.Map.BoxZoom.prototype._onMouseMove.call(this, e);
},
_onMouseUp(e) {
L.Map.BoxZoom.prototype._onMouseUp.call(this, e);
if (!this._moved && this._preMoved) {
this._clearDeferredResetState();
this._resetStateTimeout = setTimeout(L.Util.bind(this._resetState, this), 0);
}
},
});