-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move all tap hacks code into a separate handler, close #1781
- Loading branch information
Showing
5 changed files
with
122 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* L.Map.Tap is used to enable mobile hacks like quick taps and long hold. | ||
*/ | ||
|
||
L.Map.mergeOptions({ | ||
tap: true, | ||
tapTolerance: 15 | ||
}); | ||
|
||
L.Map.Tap = L.Handler.extend({ | ||
addHooks: function () { | ||
if (!L.Browser.touch) { return; } | ||
L.DomEvent.on(this._map._container, 'touchstart', this._onDown, this); | ||
}, | ||
|
||
removeHooks: function () { | ||
if (!L.Browser.touch) { return; } | ||
L.DomEvent.off(this._map._container, 'touchstart', this._onDown, this); | ||
}, | ||
|
||
_onDown: function (e) { | ||
if (!e.touches) { return; } | ||
|
||
L.DomEvent.preventDefault(e); | ||
|
||
this._fireClick = true; | ||
|
||
// don't simulate click or track longpress if more than 1 touch | ||
if (e.touches.length > 1) { | ||
this._fireClick = false; | ||
clearTimeout(this._holdTimeout); | ||
return; | ||
} | ||
|
||
var first = e.touches[0], | ||
el = first.target; | ||
|
||
this._startPos = this._newPos = new L.Point(first.clientX, first.clientY); | ||
|
||
// if touching a link, highlight it | ||
if (el.tagName.toLowerCase() === 'a') { | ||
L.DomUtil.addClass(el, 'leaflet-active'); | ||
} | ||
|
||
// simulate long hold but setting a timeout | ||
if (!L.Browser.msTouch) { | ||
this._holdTimeout = setTimeout(L.bind(function () { | ||
if (this._isTapValid()) { | ||
this._fireClick = false; | ||
this._onUp(); | ||
this._simulateEvent('contextmenu', first); | ||
} | ||
}, this), 1000); | ||
} | ||
|
||
L.DomEvent | ||
.on(document, 'touchmove', this._onMove, this) | ||
.on(document, 'touchend', this._onUp, this); | ||
}, | ||
|
||
_onUp: function (e) { | ||
clearTimeout(this._holdTimeout); | ||
|
||
L.DomEvent | ||
.off(document, 'touchmove', this._onMove, this) | ||
.off(document, 'touchend', this._onUp, this); | ||
|
||
if (this._fireClick && e && e.changedTouches) { | ||
|
||
var first = e.changedTouches[0], | ||
el = first.target; | ||
|
||
if (el.tagName.toLowerCase() === 'a') { | ||
L.DomUtil.removeClass(el, 'leaflet-active'); | ||
} | ||
|
||
// simulate click if the touch didn't move too much | ||
if (this._isTapValid()) { | ||
this._simulateEvent('click', first); | ||
} | ||
} | ||
}, | ||
|
||
_isTapValid: function () { | ||
return this._newPos.distanceTo(this._startPos) <= this._map.options.tapTolerance; | ||
}, | ||
|
||
_onMove: function (e) { | ||
var first = e.touches[0]; | ||
this._newPos = new L.Point(first.clientX, first.clientY); | ||
}, | ||
|
||
_simulateEvent: function (type, e) { | ||
var simulatedEvent = document.createEvent('MouseEvents'); | ||
|
||
simulatedEvent._simulated = true; | ||
|
||
simulatedEvent.initMouseEvent( | ||
type, true, true, window, 1, | ||
e.screenX, e.screenY, | ||
e.clientX, e.clientY, | ||
false, false, false, false, 0, null); | ||
|
||
e.target.dispatchEvent(simulatedEvent); | ||
} | ||
}); | ||
|
||
L.Map.addInitHook('addHandler', 'tap', L.Map.Tap); |