1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | |
---|
7 | /** |
---|
8 | * @requires OpenLayers/Control.js |
---|
9 | * @requires OpenLayers/Control/MouseDefaults.js |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Class: OpenLayers.Control.MouseToolbar |
---|
14 | * This class is DEPRECATED in 2.4 and will be removed by 3.0. |
---|
15 | * If you need this functionality, use <OpenLayers.Control.NavToolbar> |
---|
16 | * instead!!! |
---|
17 | */ |
---|
18 | OpenLayers.Control.MouseToolbar = OpenLayers.Class( |
---|
19 | OpenLayers.Control.MouseDefaults, { |
---|
20 | |
---|
21 | /** |
---|
22 | * Property: mode |
---|
23 | */ |
---|
24 | mode: null, |
---|
25 | /** |
---|
26 | * Property: buttons |
---|
27 | */ |
---|
28 | buttons: null, |
---|
29 | |
---|
30 | /** |
---|
31 | * APIProperty: direction |
---|
32 | * {String} 'vertical' or 'horizontal' |
---|
33 | */ |
---|
34 | direction: "vertical", |
---|
35 | |
---|
36 | /** |
---|
37 | * Property: buttonClicked |
---|
38 | * {String} |
---|
39 | */ |
---|
40 | buttonClicked: null, |
---|
41 | |
---|
42 | /** |
---|
43 | * Constructor: OpenLayers.Control.MouseToolbar |
---|
44 | * |
---|
45 | * Parameters: |
---|
46 | * position - {<OpenLayers.Pixel>} |
---|
47 | * direction - {String} |
---|
48 | */ |
---|
49 | initialize: function(position, direction) { |
---|
50 | OpenLayers.Control.prototype.initialize.apply(this, arguments); |
---|
51 | this.position = new OpenLayers.Pixel(OpenLayers.Control.MouseToolbar.X, |
---|
52 | OpenLayers.Control.MouseToolbar.Y); |
---|
53 | if (position) { |
---|
54 | this.position = position; |
---|
55 | } |
---|
56 | if (direction) { |
---|
57 | this.direction = direction; |
---|
58 | } |
---|
59 | this.measureDivs = []; |
---|
60 | }, |
---|
61 | |
---|
62 | /** |
---|
63 | * APIMethod: destroy |
---|
64 | */ |
---|
65 | destroy: function() { |
---|
66 | for( var btnId in this.buttons) { |
---|
67 | var btn = this.buttons[btnId]; |
---|
68 | btn.map = null; |
---|
69 | btn.events.destroy(); |
---|
70 | } |
---|
71 | OpenLayers.Control.MouseDefaults.prototype.destroy.apply(this, |
---|
72 | arguments); |
---|
73 | }, |
---|
74 | |
---|
75 | /** |
---|
76 | * Method: draw |
---|
77 | */ |
---|
78 | draw: function() { |
---|
79 | OpenLayers.Control.prototype.draw.apply(this, arguments); |
---|
80 | OpenLayers.Control.MouseDefaults.prototype.draw.apply(this, arguments); |
---|
81 | this.buttons = {}; |
---|
82 | var sz = new OpenLayers.Size(28,28); |
---|
83 | var centered = new OpenLayers.Pixel(OpenLayers.Control.MouseToolbar.X,0); |
---|
84 | this._addButton("zoombox", "drag-rectangle-off.png", "drag-rectangle-on.png", centered, sz, "Shift->Drag to zoom to area"); |
---|
85 | centered = centered.add((this.direction == "vertical" ? 0 : sz.w), (this.direction == "vertical" ? sz.h : 0)); |
---|
86 | this._addButton("pan", "panning-hand-off.png", "panning-hand-on.png", centered, sz, "Drag the map to pan."); |
---|
87 | centered = centered.add((this.direction == "vertical" ? 0 : sz.w), (this.direction == "vertical" ? sz.h : 0)); |
---|
88 | this.switchModeTo("pan"); |
---|
89 | |
---|
90 | return this.div; |
---|
91 | }, |
---|
92 | |
---|
93 | /** |
---|
94 | * Method: _addButton |
---|
95 | */ |
---|
96 | _addButton:function(id, img, activeImg, xy, sz, title) { |
---|
97 | var imgLocation = OpenLayers.Util.getImagesLocation() + img; |
---|
98 | var activeImgLocation = OpenLayers.Util.getImagesLocation() + activeImg; |
---|
99 | // var btn = new ol.AlphaImage("_"+id, imgLocation, xy, sz); |
---|
100 | var btn = OpenLayers.Util.createAlphaImageDiv( |
---|
101 | "OpenLayers_Control_MouseToolbar_" + id, |
---|
102 | xy, sz, imgLocation, "absolute"); |
---|
103 | |
---|
104 | //we want to add the outer div |
---|
105 | this.div.appendChild(btn); |
---|
106 | btn.imgLocation = imgLocation; |
---|
107 | btn.activeImgLocation = activeImgLocation; |
---|
108 | |
---|
109 | btn.events = new OpenLayers.Events(this, btn, null, true); |
---|
110 | btn.events.on({ |
---|
111 | "mousedown": this.buttonDown, |
---|
112 | "mouseup": this.buttonUp, |
---|
113 | "dblclick": OpenLayers.Event.stop, |
---|
114 | scope: this |
---|
115 | }); |
---|
116 | btn.action = id; |
---|
117 | btn.title = title; |
---|
118 | btn.alt = title; |
---|
119 | btn.map = this.map; |
---|
120 | |
---|
121 | //we want to remember/reference the outer div |
---|
122 | this.buttons[id] = btn; |
---|
123 | return btn; |
---|
124 | }, |
---|
125 | |
---|
126 | /** |
---|
127 | * Method: buttonDown |
---|
128 | * |
---|
129 | * Parameters: |
---|
130 | * evt - {Event} |
---|
131 | */ |
---|
132 | buttonDown: function(evt) { |
---|
133 | if (!OpenLayers.Event.isLeftClick(evt)) { |
---|
134 | return; |
---|
135 | } |
---|
136 | this.buttonClicked = evt.element.action; |
---|
137 | OpenLayers.Event.stop(evt); |
---|
138 | }, |
---|
139 | |
---|
140 | /** |
---|
141 | * Method: buttonUp |
---|
142 | * |
---|
143 | * Parameters: |
---|
144 | * evt - {Event} |
---|
145 | */ |
---|
146 | buttonUp: function(evt) { |
---|
147 | if (!OpenLayers.Event.isLeftClick(evt)) { |
---|
148 | return; |
---|
149 | } |
---|
150 | if (this.buttonClicked != null) { |
---|
151 | if (this.buttonClicked == evt.element.action) { |
---|
152 | this.switchModeTo(evt.element.action); |
---|
153 | } |
---|
154 | OpenLayers.Event.stop(evt); |
---|
155 | this.buttonClicked = null; |
---|
156 | } |
---|
157 | }, |
---|
158 | |
---|
159 | /** |
---|
160 | * Method: defaultDblClick |
---|
161 | * |
---|
162 | * Parameters: |
---|
163 | * evt - {Event} |
---|
164 | */ |
---|
165 | defaultDblClick: function (evt) { |
---|
166 | this.switchModeTo("pan"); |
---|
167 | this.performedDrag = false; |
---|
168 | var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); |
---|
169 | this.map.setCenter(newCenter, this.map.zoom + 1); |
---|
170 | OpenLayers.Event.stop(evt); |
---|
171 | return false; |
---|
172 | }, |
---|
173 | |
---|
174 | /** |
---|
175 | * Method: defaultMouseDown |
---|
176 | * |
---|
177 | * Parameters: |
---|
178 | * evt - {Event} |
---|
179 | */ |
---|
180 | defaultMouseDown: function (evt) { |
---|
181 | if (!OpenLayers.Event.isLeftClick(evt)) { |
---|
182 | return; |
---|
183 | } |
---|
184 | this.mouseDragStart = evt.xy.clone(); |
---|
185 | this.performedDrag = false; |
---|
186 | this.startViaKeyboard = false; |
---|
187 | if (evt.shiftKey && this.mode !="zoombox") { |
---|
188 | this.switchModeTo("zoombox"); |
---|
189 | this.startViaKeyboard = true; |
---|
190 | } else if (evt.altKey && this.mode !="measure") { |
---|
191 | this.switchModeTo("measure"); |
---|
192 | } else if (!this.mode) { |
---|
193 | this.switchModeTo("pan"); |
---|
194 | } |
---|
195 | |
---|
196 | switch (this.mode) { |
---|
197 | case "zoombox": |
---|
198 | this.map.div.style.cursor = "crosshair"; |
---|
199 | this.zoomBox = OpenLayers.Util.createDiv('zoomBox', |
---|
200 | this.mouseDragStart, |
---|
201 | null, |
---|
202 | null, |
---|
203 | "absolute", |
---|
204 | "2px solid red"); |
---|
205 | this.zoomBox.style.backgroundColor = "white"; |
---|
206 | this.zoomBox.style.filter = "alpha(opacity=50)"; // IE |
---|
207 | this.zoomBox.style.opacity = "0.50"; |
---|
208 | this.zoomBox.style.fontSize = "1px"; |
---|
209 | this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1; |
---|
210 | this.map.viewPortDiv.appendChild(this.zoomBox); |
---|
211 | this.performedDrag = true; |
---|
212 | break; |
---|
213 | case "measure": |
---|
214 | var distance = ""; |
---|
215 | if (this.measureStart) { |
---|
216 | var measureEnd = this.map.getLonLatFromViewPortPx(this.mouseDragStart); |
---|
217 | distance = OpenLayers.Util.distVincenty(this.measureStart, measureEnd); |
---|
218 | distance = Math.round(distance * 100) / 100; |
---|
219 | distance = distance + "km"; |
---|
220 | this.measureStartBox = this.measureBox; |
---|
221 | } |
---|
222 | this.measureStart = this.map.getLonLatFromViewPortPx(this.mouseDragStart);; |
---|
223 | this.measureBox = OpenLayers.Util.createDiv(null, |
---|
224 | this.mouseDragStart.add( |
---|
225 | -2-parseInt(this.map.layerContainerDiv.style.left), |
---|
226 | -2-parseInt(this.map.layerContainerDiv.style.top)), |
---|
227 | null, |
---|
228 | null, |
---|
229 | "absolute"); |
---|
230 | this.measureBox.style.width="4px"; |
---|
231 | this.measureBox.style.height="4px"; |
---|
232 | this.measureBox.style.fontSize = "1px"; |
---|
233 | this.measureBox.style.backgroundColor="red"; |
---|
234 | this.measureBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1; |
---|
235 | this.map.layerContainerDiv.appendChild(this.measureBox); |
---|
236 | if (distance) { |
---|
237 | this.measureBoxDistance = OpenLayers.Util.createDiv(null, |
---|
238 | this.mouseDragStart.add( |
---|
239 | -2-parseInt(this.map.layerContainerDiv.style.left), |
---|
240 | 2-parseInt(this.map.layerContainerDiv.style.top)), |
---|
241 | null, |
---|
242 | null, |
---|
243 | "absolute"); |
---|
244 | |
---|
245 | this.measureBoxDistance.innerHTML = distance; |
---|
246 | this.measureBoxDistance.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1; |
---|
247 | this.map.layerContainerDiv.appendChild(this.measureBoxDistance); |
---|
248 | this.measureDivs.push(this.measureBoxDistance); |
---|
249 | } |
---|
250 | this.measureBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1; |
---|
251 | this.map.layerContainerDiv.appendChild(this.measureBox); |
---|
252 | this.measureDivs.push(this.measureBox); |
---|
253 | break; |
---|
254 | default: |
---|
255 | this.map.div.style.cursor = "move"; |
---|
256 | break; |
---|
257 | } |
---|
258 | document.onselectstart = OpenLayers.Function.False; |
---|
259 | OpenLayers.Event.stop(evt); |
---|
260 | }, |
---|
261 | |
---|
262 | /** |
---|
263 | * Method: switchModeTo |
---|
264 | * |
---|
265 | * Parameters: |
---|
266 | * mode - {String} |
---|
267 | */ |
---|
268 | switchModeTo: function(mode) { |
---|
269 | if (mode != this.mode) { |
---|
270 | |
---|
271 | |
---|
272 | if (this.mode && this.buttons[this.mode]) { |
---|
273 | OpenLayers.Util.modifyAlphaImageDiv(this.buttons[this.mode], null, null, null, this.buttons[this.mode].imgLocation); |
---|
274 | } |
---|
275 | if (this.mode == "measure" && mode != "measure") { |
---|
276 | for(var i=0, len=this.measureDivs.length; i<len; i++) { |
---|
277 | if (this.measureDivs[i]) { |
---|
278 | this.map.layerContainerDiv.removeChild(this.measureDivs[i]); |
---|
279 | } |
---|
280 | } |
---|
281 | this.measureDivs = []; |
---|
282 | this.measureStart = null; |
---|
283 | } |
---|
284 | this.mode = mode; |
---|
285 | if (this.buttons[mode]) { |
---|
286 | OpenLayers.Util.modifyAlphaImageDiv(this.buttons[mode], null, null, null, this.buttons[mode].activeImgLocation); |
---|
287 | } |
---|
288 | switch (this.mode) { |
---|
289 | case "zoombox": |
---|
290 | this.map.div.style.cursor = "crosshair"; |
---|
291 | break; |
---|
292 | default: |
---|
293 | this.map.div.style.cursor = ""; |
---|
294 | break; |
---|
295 | } |
---|
296 | |
---|
297 | } |
---|
298 | }, |
---|
299 | |
---|
300 | /** |
---|
301 | * Method: leaveMode |
---|
302 | */ |
---|
303 | leaveMode: function() { |
---|
304 | this.switchModeTo("pan"); |
---|
305 | }, |
---|
306 | |
---|
307 | /** |
---|
308 | * Method: defaultMouseMove |
---|
309 | * |
---|
310 | * Parameters: |
---|
311 | * evt - {Event} |
---|
312 | */ |
---|
313 | defaultMouseMove: function (evt) { |
---|
314 | if (this.mouseDragStart != null) { |
---|
315 | switch (this.mode) { |
---|
316 | case "zoombox": |
---|
317 | var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x); |
---|
318 | var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y); |
---|
319 | this.zoomBox.style.width = Math.max(1, deltaX) + "px"; |
---|
320 | this.zoomBox.style.height = Math.max(1, deltaY) + "px"; |
---|
321 | if (evt.xy.x < this.mouseDragStart.x) { |
---|
322 | this.zoomBox.style.left = evt.xy.x+"px"; |
---|
323 | } |
---|
324 | if (evt.xy.y < this.mouseDragStart.y) { |
---|
325 | this.zoomBox.style.top = evt.xy.y+"px"; |
---|
326 | } |
---|
327 | break; |
---|
328 | default: |
---|
329 | var deltaX = this.mouseDragStart.x - evt.xy.x; |
---|
330 | var deltaY = this.mouseDragStart.y - evt.xy.y; |
---|
331 | var size = this.map.getSize(); |
---|
332 | var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX, |
---|
333 | size.h / 2 + deltaY); |
---|
334 | var newCenter = this.map.getLonLatFromViewPortPx( newXY ); |
---|
335 | this.map.setCenter(newCenter, null, true); |
---|
336 | this.mouseDragStart = evt.xy.clone(); |
---|
337 | } |
---|
338 | this.performedDrag = true; |
---|
339 | } |
---|
340 | }, |
---|
341 | |
---|
342 | /** |
---|
343 | * Method: defaultMouseUp |
---|
344 | * |
---|
345 | * Parameters: |
---|
346 | * evt - {Event} |
---|
347 | */ |
---|
348 | defaultMouseUp: function (evt) { |
---|
349 | if (!OpenLayers.Event.isLeftClick(evt)) { |
---|
350 | return; |
---|
351 | } |
---|
352 | switch (this.mode) { |
---|
353 | case "zoombox": |
---|
354 | this.zoomBoxEnd(evt); |
---|
355 | if (this.startViaKeyboard) { |
---|
356 | this.leaveMode(); |
---|
357 | } |
---|
358 | break; |
---|
359 | case "pan": |
---|
360 | if (this.performedDrag) { |
---|
361 | this.map.setCenter(this.map.center); |
---|
362 | } |
---|
363 | } |
---|
364 | document.onselectstart = null; |
---|
365 | this.mouseDragStart = null; |
---|
366 | this.map.div.style.cursor = "default"; |
---|
367 | }, |
---|
368 | |
---|
369 | /** |
---|
370 | * Method: defaultMouseOut |
---|
371 | * |
---|
372 | * Parameters: |
---|
373 | * evt - {Event} |
---|
374 | */ |
---|
375 | defaultMouseOut: function (evt) { |
---|
376 | if (this.mouseDragStart != null |
---|
377 | && OpenLayers.Util.mouseLeft(evt, this.map.div)) { |
---|
378 | if (this.zoomBox) { |
---|
379 | this.removeZoomBox(); |
---|
380 | if (this.startViaKeyboard) { |
---|
381 | this.leaveMode(); |
---|
382 | } |
---|
383 | } |
---|
384 | this.mouseDragStart = null; |
---|
385 | this.map.div.style.cursor = "default"; |
---|
386 | } |
---|
387 | }, |
---|
388 | |
---|
389 | /** |
---|
390 | * Method: defaultClick |
---|
391 | * |
---|
392 | * Parameters: |
---|
393 | * evt - {Event} |
---|
394 | */ |
---|
395 | defaultClick: function (evt) { |
---|
396 | if (this.performedDrag) { |
---|
397 | this.performedDrag = false; |
---|
398 | return false; |
---|
399 | } |
---|
400 | }, |
---|
401 | |
---|
402 | CLASS_NAME: "OpenLayers.Control.MouseToolbar" |
---|
403 | }); |
---|
404 | |
---|
405 | OpenLayers.Control.MouseToolbar.X = 6; |
---|
406 | OpenLayers.Control.MouseToolbar.Y = 300; |
---|