/
dblclick_zoom.js
105 lines (90 loc) · 2.44 KB
/
dblclick_zoom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// @flow
import { bindAll } from '../../util/util';
import type Map from '../map';
import type {MapMouseEvent, MapTouchEvent} from '../events';
/**
* The `DoubleClickZoomHandler` allows the user to zoom the map at a point by
* double clicking or double tapping.
*/
class DoubleClickZoomHandler {
_map: Map;
_enabled: boolean;
_active: boolean;
_tapped: ?TimeoutID;
/**
* @private
*/
constructor(map: Map) {
this._map = map;
bindAll([
'_onDblClick',
'_onZoomEnd'
], this);
}
/**
* Returns a Boolean indicating whether the "double click to zoom" interaction is enabled.
*
* @returns {boolean} `true` if the "double click to zoom" interaction is enabled.
*/
isEnabled() {
return !!this._enabled;
}
/**
* Returns a Boolean indicating whether the "double click to zoom" interaction is active, i.e. currently being used.
*
* @returns {boolean} `true` if the "double click to zoom" interaction is active.
*/
isActive() {
return !!this._active;
}
/**
* Enables the "double click to zoom" interaction.
*
* @example
* map.doubleClickZoom.enable();
*/
enable() {
if (this.isEnabled()) return;
this._enabled = true;
}
/**
* Disables the "double click to zoom" interaction.
*
* @example
* map.doubleClickZoom.disable();
*/
disable() {
if (!this.isEnabled()) return;
this._enabled = false;
}
onTouchStart(e: MapTouchEvent) {
if (!this.isEnabled()) return;
if (e.points.length > 1) return;
if (!this._tapped) {
this._tapped = setTimeout(() => { this._tapped = null; }, 300);
} else {
clearTimeout(this._tapped);
this._tapped = null;
this._zoom(e);
}
}
onDblClick(e: MapMouseEvent) {
if (!this.isEnabled()) return;
e.originalEvent.preventDefault();
this._zoom(e);
}
_zoom(e: MapMouseEvent | MapTouchEvent) {
this._active = true;
this._map.on('zoomend', this._onZoomEnd);
this._map.zoomTo(
this._map.getZoom() + (e.originalEvent.shiftKey ? -1 : 1),
{around: e.lngLat},
e
);
}
_onZoomEnd() {
this._active = false;
this._map.off('zoomend', this._onZoomEnd);
}
}
export default DoubleClickZoomHandler;