/
mouse.js
172 lines (138 loc) · 5.27 KB
/
mouse.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Event Handlers
// --------------
// A utility function for finding the offset of the
// mouse from the top-left of the page
MM.getMousePoint = function(e, map) {
// start with just the mouse (x, y)
var point = new MM.Point(e.clientX, e.clientY);
// correct for scrolled document
point.x += document.body.scrollLeft + document.documentElement.scrollLeft;
point.y += document.body.scrollTop + document.documentElement.scrollTop;
// correct for nested offsets in DOM
for (var node = map.parent; node; node = node.offsetParent) {
point.x -= node.offsetLeft;
point.y -= node.offsetTop;
}
return point;
};
// A handler that allows mouse-wheel zooming - zooming in
// when page would scroll up, and out when the page would scroll down.
MM.MouseWheelHandler = function(map) {
if (map !== undefined) this.init(map);
};
MM.MouseWheelHandler.prototype = {
init: function(map) {
this.map = map;
this._mouseWheel = MM.bind(this.mouseWheel, this);
MM.addEvent(map.parent, 'mousewheel', this._mouseWheel);
},
remove: function() {
MM.removeEvent(this.map.parent, 'mousewheel', this._mouseWheel);
},
mouseWheel: function(e) {
var delta = 0;
this.prevTime = this.prevTime || new Date().getTime();
if (e.wheelDelta) {
delta = e.wheelDelta;
} else if (e.detail) {
delta = -e.detail;
}
// limit mousewheeling to once every 200ms
var timeSince = new Date().getTime() - this.prevTime;
if (Math.abs(delta) > 0 && (timeSince > 200)) {
var point = MM.getMousePoint(e, this.map);
this.map.zoomByAbout(delta > 0 ? 1 : -1, point);
this.prevTime = new Date().getTime();
}
// Cancel the event so that the page doesn't scroll
return MM.cancelEvent(e);
}
};
// Handle double clicks, that zoom the map in one zoom level.
MM.DoubleClickHandler = function(map) {
if (map !== undefined) {
this.init(map);
}
};
MM.DoubleClickHandler.prototype = {
init: function(map) {
this.map = map;
this._doubleClick = MM.bind(this.doubleClick, this);
MM.addEvent(map.parent, 'dblclick', this._doubleClick);
},
remove: function() {
MM.removeEvent(this.map.parent, 'dblclick', this._doubleClick);
},
doubleClick: function(e) {
// Ensure that this handler is attached once.
// Get the point on the map that was double-clicked
var point = MM.getMousePoint(e, this.map);
// use shift-double-click to zoom out
this.map.zoomByAbout(e.shiftKey ? -1 : 1, point);
return MM.cancelEvent(e);
}
};
// Handle the use of mouse dragging to pan the map.
MM.DragHandler = function(map) {
if (map !== undefined) {
this.init(map);
}
};
MM.DragHandler.prototype = {
init: function(map) {
this.map = map;
this._mouseDown = MM.bind(this.mouseDown, this);
MM.addEvent(map.parent, 'mousedown', this._mouseDown);
},
remove: function() {
MM.removeEvent(this.map.parent, 'mousedown', this._mouseDown);
},
mouseDown: function(e) {
MM.addEvent(document, 'mouseup', this._mouseUp = MM.bind(this.mouseUp, this));
MM.addEvent(document, 'mousemove', this._mouseMove = MM.bind(this.mouseMove, this));
this.prevMouse = new MM.Point(e.clientX, e.clientY);
this.map.parent.style.cursor = 'move';
return MM.cancelEvent(e);
},
mouseMove: function(e) {
if (this.prevMouse) {
this.map.panBy(
e.clientX - this.prevMouse.x,
e.clientY - this.prevMouse.y);
this.prevMouse.x = e.clientX;
this.prevMouse.y = e.clientY;
this.prevMouse.t = +new Date();
}
return MM.cancelEvent(e);
},
mouseUp: function(e) {
MM.removeEvent(document, 'mouseup', this._mouseUp);
MM.removeEvent(document, 'mousemove', this._mouseMove);
this.prevMouse = null;
this.map.parent.style.cursor = '';
return MM.cancelEvent(e);
}
};
// A shortcut for adding drag, double click,
// and mouse wheel events to the map. This is the default
// handler attached to a map if the handlers argument isn't given.
MM.MouseHandler = function(map) {
if (map !== undefined) {
this.init(map);
}
};
MM.MouseHandler.prototype = {
init: function(map) {
this.map = map;
this.handlers = [
new MM.DragHandler(map),
new MM.DoubleClickHandler(map),
new MM.MouseWheelHandler(map)
];
},
remove: function() {
for (var i = 0; i < this.handlers.length; i++) {
this.handlers[i].remove();
}
}
};