forked from developmentseed/openlayers_plus
-
Notifications
You must be signed in to change notification settings - Fork 42
/
boxselector.js
223 lines (190 loc) · 7.04 KB
/
boxselector.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
wax = wax || {};
wax.mm = wax.mm || {};
wax.mm.boxselector = function() {
var corner,
nearCorner,
boxDiv,
style,
borderWidth = 0,
horizontal = false, // Whether the resize is horizontal
vertical = false,
edge = 5, // Distance from border sensitive to resizing
addEvent = MM.addEvent,
removeEvent = MM.removeEvent,
box,
boxselector = {},
map,
callbackManager = new MM.CallbackManager(boxselector, ['change']);
function getMousePoint(e) {
// 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;
}
function mouseDown(e) {
if (!e.shiftKey) return;
corner = nearCorner = getMousePoint(e);
horizontal = vertical = true;
style.left = corner.x + 'px';
style.top = corner.y + 'px';
style.width = style.height = 0;
addEvent(document, 'mousemove', mouseMove);
addEvent(document, 'mouseup', mouseUp);
map.parent.style.cursor = 'crosshair';
return MM.cancelEvent(e);
}
// Resize existing box
function mouseDownResize(e) {
var point = getMousePoint(e),
TL = {
x: parseInt(boxDiv.offsetLeft, 10),
y: parseInt(boxDiv.offsetTop, 10)
},
BR = {
x: TL.x + parseInt(boxDiv.offsetWidth, 10),
y: TL.y + parseInt(boxDiv.offsetHeight, 10)
};
// Determine whether resize is horizontal, vertical or both
horizontal = point.x - TL.x <= edge || BR.x - point.x <= edge;
vertical = point.y - TL.y <= edge || BR.y - point.y <= edge;
if (vertical || horizontal) {
corner = {
x: (point.x - TL.x < BR.x - point.x) ? BR.x : TL.x,
y: (point.y - TL.y < BR.y - point.y) ? BR.y : TL.y
};
nearCorner = {
x: (point.x - TL.x < BR.x - point.x) ? TL.x : BR.x,
y: (point.y - TL.y < BR.y - point.y) ? TL.y : BR.y
};
addEvent(document, 'mousemove', mouseMove);
addEvent(document, 'mouseup', mouseUp);
return MM.cancelEvent(e);
}
}
function mouseMove(e) {
var point = getMousePoint(e);
style.display = 'block';
if (horizontal) {
style.left = (point.x < corner.x ? point.x : corner.x) + 'px';
style.width = Math.abs(point.x - corner.x) - 2 * borderWidth + 'px';
}
if (vertical) {
style.top = (point.y < corner.y ? point.y : corner.y) + 'px';
style.height = Math.abs(point.y - corner.y) - 2 * borderWidth + 'px';
}
changeCursor(point, map.parent);
return MM.cancelEvent(e);
}
function mouseUp(e) {
var point = getMousePoint(e),
l1 = map.pointLocation( new MM.Point(
horizontal ? point.x : nearCorner.x,
vertical? point.y : nearCorner.y
));
l2 = map.pointLocation(corner);
// Format coordinates like mm.map.getExtent().
boxselector.extent([
new MM.Location(
Math.max(l1.lat, l2.lat),
Math.min(l1.lon, l2.lon)),
new MM.Location(
Math.min(l1.lat, l2.lat),
Math.max(l1.lon, l2.lon))
]);
removeEvent(document, 'mousemove', mouseMove);
removeEvent(document, 'mouseup', mouseUp);
map.parent.style.cursor = 'auto';
}
function mouseMoveCursor(e) {
changeCursor(getMousePoint(e), boxDiv);
}
// Set resize cursor if mouse is on edge
function changeCursor(point, elem) {
var TL = {
x: parseInt(boxDiv.offsetLeft, 10),
y: parseInt(boxDiv.offsetTop, 10)
},
BR = {
x: TL.x + parseInt(boxDiv.offsetWidth, 10),
y: TL.y + parseInt(boxDiv.offsetHeight, 10)
};
// Build cursor style string
var prefix = '';
if (point.y - TL.y <= edge) prefix = 'n';
else if (BR.y - point.y <= edge) prefix = 's';
if (point.x - TL.x <= edge) prefix += 'w';
else if (BR.x - point.x <= edge) prefix += 'e';
if (prefix !== '') prefix += '-resize';
elem.style.cursor = prefix;
}
function drawbox(map, e) {
if (!boxDiv || !box) return;
var br = map.locationPoint(box[1]),
tl = map.locationPoint(box[0]),
style = boxDiv.style;
style.display = 'block';
style.height = 'auto';
style.width = 'auto';
style.left = Math.max(0, tl.x) + 'px';
style.top = Math.max(0, tl.y) + 'px';
style.right = Math.max(0, map.dimensions.x - br.x) + 'px';
style.bottom = Math.max(0, map.dimensions.y - br.y) + 'px';
}
boxselector.addCallback = function(event, callback) {
callbackManager.addCallback(event, callback);
return boxselector;
};
boxselector.removeCallback = function(event, callback) {
callbackManager.removeCallback(event, callback);
return boxselector;
};
boxselector.extent = function(x, silent) {
if (!x) return box;
box = [
new MM.Location(
Math.max(x[0].lat, x[1].lat),
Math.min(x[0].lon, x[1].lon)),
new MM.Location(
Math.min(x[0].lat, x[1].lat),
Math.max(x[0].lon, x[1].lon))
];
drawbox(map);
if (!silent) callbackManager.dispatchCallback('change', box);
};
boxDiv = document.createElement('div');
boxDiv.className = 'boxselector-box';
style = boxDiv.style;
boxselector.add = function() {
boxDiv.id = map.parent.id + '-boxselector-box';
map.parent.appendChild(boxDiv);
borderWidth = parseInt(window.getComputedStyle(boxDiv).borderWidth, 10);
addEvent(map.parent, 'mousedown', mouseDown);
addEvent(boxDiv, 'mousedown', mouseDownResize);
addEvent(map.parent, 'mousemove', mouseMoveCursor);
map.addCallback('drawn', drawbox);
return boxselector;
};
boxselector.map = function(x) {
if (!arguments.length) return map;
map = x;
return boxselector;
};
boxselector.remove = function() {
map.parent.removeChild(boxDiv);
removeEvent(map.parent, 'mousedown', mouseDown);
removeEvent(boxDiv, 'mousedown', mouseDownResize);
removeEvent(map.parent, 'mousemove', mouseMoveCursor);
map.removeCallback('drawn', drawbox);
return boxselector;
};
return boxselector;
};