aboutsummaryrefslogtreecommitdiffstats
path: root/extlib/leaflet/src/handler/ShiftDragZoom.js
blob: ba216109aeff162069207cde8edaade5130642ea (plain)
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
/*
 * L.Handler.ShiftDragZoom is used internally by L.Map to add shift-drag zoom (zoom to a selected bounding box).
 */

L.Handler.ShiftDragZoom = L.Handler.extend({
	initialize: function(map) {
		this._map = map;
		this._container = map._container;
		this._pane = map._panes.overlayPane;
	},
	
	enable: function() {
		if (this._enabled) { return; }
		
		L.DomEvent.addListener(this._container, 'mousedown', this._onMouseDown, this);
		
		this._enabled = true;
	},
	
	disable: function() {
		if (!this._enabled) { return; }
		
		L.DomEvent.removeListener(this._container, 'mousedown', this._onMouseDown);
		
		this._enabled = false;
	},
	
	_onMouseDown: function(e) {
		if (!e.shiftKey || ((e.which != 1) && (e.button != 1))) { return false; }
		
		L.DomUtil.disableTextSelection();
		
		this._startLayerPoint = this._map.mouseEventToLayerPoint(e);
		
		this._box = L.DomUtil.create('div', 'leaflet-zoom-box', this._pane);
		L.DomUtil.setPosition(this._box, this._startLayerPoint);
		
		//TODO move cursor to styles
		this._container.style.cursor = 'crosshair';
		
		L.DomEvent.addListener(document, 'mousemove', this._onMouseMove, this);
		L.DomEvent.addListener(document, 'mouseup', this._onMouseUp, this);
		
		L.DomEvent.preventDefault(e);
	},
	
	_onMouseMove: function(e) {
		var layerPoint = this._map.mouseEventToLayerPoint(e),
			dx = layerPoint.x - this._startLayerPoint.x,
			dy = layerPoint.y - this._startLayerPoint.y;
		
		var newX = Math.min(layerPoint.x, this._startLayerPoint.x),
			newY = Math.min(layerPoint.y, this._startLayerPoint.y),
			newPos = new L.Point(newX, newY);
		
		L.DomUtil.setPosition(this._box, newPos);
		
		this._box.style.width = (Math.abs(dx) - 4) + 'px';
		this._box.style.height = (Math.abs(dy) - 4) + 'px';
	},
	
	_onMouseUp: function(e) {
		this._pane.removeChild(this._box);
		this._container.style.cursor = '';
		
		L.DomUtil.enableTextSelection();

		L.DomEvent.removeListener(document, 'mousemove', this._onMouseMove);
		L.DomEvent.removeListener(document, 'mouseup', this._onMouseUp);
		
		var layerPoint = this._map.mouseEventToLayerPoint(e);
		
		var bounds = new L.LatLngBounds(
				this._map.layerPointToLatLng(this._startLayerPoint),
				this._map.layerPointToLatLng(layerPoint));
		
		this._map.fitBounds(bounds);
	}
});