1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
7 * @requires OpenLayers/Control.js
8 * @requires OpenLayers/Control/MouseDefaults.js
12 * Class: OpenLayers.Control.MouseToolbar
13 * This class is DEPRECATED in 2.4 and will be removed by 3.0.
14 * If you need this functionality, use <OpenLayers.Control.NavToolbar>
17 OpenLayers.Control.MouseToolbar = OpenLayers.Class(
18 OpenLayers.Control.MouseDefaults, {
30 * APIProperty: direction
31 * {String} 'vertical' or 'horizontal'
33 direction: "vertical",
36 * Property: buttonClicked
42 * Constructor: OpenLayers.Control.MouseToolbar
45 * position - {<OpenLayers.Pixel>}
46 * direction - {String}
48 initialize: function(position, direction) {
49 OpenLayers.Control.prototype.initialize.apply(this, arguments);
50 this.position = new OpenLayers.Pixel(OpenLayers.Control.MouseToolbar.X,
51 OpenLayers.Control.MouseToolbar.Y);
53 this.position = position;
56 this.direction = direction;
58 this.measureDivs = [];
65 for( var btnId in this.buttons) {
66 var btn = this.buttons[btnId];
70 OpenLayers.Control.MouseDefaults.prototype.destroy.apply(this,
78 OpenLayers.Control.prototype.draw.apply(this, arguments);
79 OpenLayers.Control.MouseDefaults.prototype.draw.apply(this, arguments);
81 var sz = new OpenLayers.Size(28,28);
82 var centered = new OpenLayers.Pixel(OpenLayers.Control.MouseToolbar.X,0);
83 this._addButton("zoombox", "drag-rectangle-off.png", "drag-rectangle-on.png", centered, sz, "Shift->Drag to zoom to area");
84 centered = centered.add((this.direction == "vertical" ? 0 : sz.w), (this.direction == "vertical" ? sz.h : 0));
85 this._addButton("pan", "panning-hand-off.png", "panning-hand-on.png", centered, sz, "Drag the map to pan.");
86 centered = centered.add((this.direction == "vertical" ? 0 : sz.w), (this.direction == "vertical" ? sz.h : 0));
87 this.switchModeTo("pan");
95 _addButton:function(id, img, activeImg, xy, sz, title) {
96 var imgLocation = OpenLayers.Util.getImagesLocation() + img;
97 var activeImgLocation = OpenLayers.Util.getImagesLocation() + activeImg;
98 // var btn = new ol.AlphaImage("_"+id, imgLocation, xy, sz);
99 var btn = OpenLayers.Util.createAlphaImageDiv(
100 "OpenLayers_Control_MouseToolbar_" + id,
101 xy, sz, imgLocation, "absolute");
103 //we want to add the outer div
104 this.div.appendChild(btn);
105 btn.imgLocation = imgLocation;
106 btn.activeImgLocation = activeImgLocation;
108 btn.events = new OpenLayers.Events(this, btn, null, true);
110 "mousedown": this.buttonDown,
111 "mouseup": this.buttonUp,
112 "dblclick": OpenLayers.Event.stop,
120 //we want to remember/reference the outer div
121 this.buttons[id] = btn;
131 buttonDown: function(evt) {
132 if (!OpenLayers.Event.isLeftClick(evt)) {
135 this.buttonClicked = evt.element.action;
136 OpenLayers.Event.stop(evt);
145 buttonUp: function(evt) {
146 if (!OpenLayers.Event.isLeftClick(evt)) {
149 if (this.buttonClicked != null) {
150 if (this.buttonClicked == evt.element.action) {
151 this.switchModeTo(evt.element.action);
153 OpenLayers.Event.stop(evt);
154 this.buttonClicked = null;
159 * Method: defaultDblClick
164 defaultDblClick: function (evt) {
165 this.switchModeTo("pan");
166 this.performedDrag = false;
167 var newCenter = this.map.getLonLatFromViewPortPx( evt.xy );
168 this.map.setCenter(newCenter, this.map.zoom + 1);
169 OpenLayers.Event.stop(evt);
174 * Method: defaultMouseDown
179 defaultMouseDown: function (evt) {
180 if (!OpenLayers.Event.isLeftClick(evt)) {
183 this.mouseDragStart = evt.xy.clone();
184 this.performedDrag = false;
185 this.startViaKeyboard = false;
186 if (evt.shiftKey && this.mode !="zoombox") {
187 this.switchModeTo("zoombox");
188 this.startViaKeyboard = true;
189 } else if (evt.altKey && this.mode !="measure") {
190 this.switchModeTo("measure");
191 } else if (!this.mode) {
192 this.switchModeTo("pan");
197 this.map.div.style.cursor = "crosshair";
198 this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
204 this.zoomBox.style.backgroundColor = "white";
205 this.zoomBox.style.filter = "alpha(opacity=50)"; // IE
206 this.zoomBox.style.opacity = "0.50";
207 this.zoomBox.style.fontSize = "1px";
208 this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
209 this.map.viewPortDiv.appendChild(this.zoomBox);
210 this.performedDrag = true;
214 if (this.measureStart) {
215 var measureEnd = this.map.getLonLatFromViewPortPx(this.mouseDragStart);
216 distance = OpenLayers.Util.distVincenty(this.measureStart, measureEnd);
217 distance = Math.round(distance * 100) / 100;
218 distance = distance + "km";
219 this.measureStartBox = this.measureBox;
221 this.measureStart = this.map.getLonLatFromViewPortPx(this.mouseDragStart);;
222 this.measureBox = OpenLayers.Util.createDiv(null,
223 this.mouseDragStart.add(
224 -2-parseInt(this.map.layerContainerDiv.style.left),
225 -2-parseInt(this.map.layerContainerDiv.style.top)),
229 this.measureBox.style.width="4px";
230 this.measureBox.style.height="4px";
231 this.measureBox.style.fontSize = "1px";
232 this.measureBox.style.backgroundColor="red";
233 this.measureBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
234 this.map.layerContainerDiv.appendChild(this.measureBox);
236 this.measureBoxDistance = OpenLayers.Util.createDiv(null,
237 this.mouseDragStart.add(
238 -2-parseInt(this.map.layerContainerDiv.style.left),
239 2-parseInt(this.map.layerContainerDiv.style.top)),
244 this.measureBoxDistance.innerHTML = distance;
245 this.measureBoxDistance.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
246 this.map.layerContainerDiv.appendChild(this.measureBoxDistance);
247 this.measureDivs.push(this.measureBoxDistance);
249 this.measureBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
250 this.map.layerContainerDiv.appendChild(this.measureBox);
251 this.measureDivs.push(this.measureBox);
254 this.map.div.style.cursor = "move";
257 document.onselectstart = function() { return false; };
258 OpenLayers.Event.stop(evt);
262 * Method: switchModeTo
267 switchModeTo: function(mode) {
268 if (mode != this.mode) {
271 if (this.mode && this.buttons[this.mode]) {
272 OpenLayers.Util.modifyAlphaImageDiv(this.buttons[this.mode], null, null, null, this.buttons[this.mode].imgLocation);
274 if (this.mode == "measure" && mode != "measure") {
275 for(var i=0, len=this.measureDivs.length; i<len; i++) {
276 if (this.measureDivs[i]) {
277 this.map.layerContainerDiv.removeChild(this.measureDivs[i]);
280 this.measureDivs = [];
281 this.measureStart = null;
284 if (this.buttons[mode]) {
285 OpenLayers.Util.modifyAlphaImageDiv(this.buttons[mode], null, null, null, this.buttons[mode].activeImgLocation);
289 this.map.div.style.cursor = "crosshair";
292 this.map.div.style.cursor = "";
302 leaveMode: function() {
303 this.switchModeTo("pan");
307 * Method: defaultMouseMove
312 defaultMouseMove: function (evt) {
313 if (this.mouseDragStart != null) {
316 var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x);
317 var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y);
318 this.zoomBox.style.width = Math.max(1, deltaX) + "px";
319 this.zoomBox.style.height = Math.max(1, deltaY) + "px";
320 if (evt.xy.x < this.mouseDragStart.x) {
321 this.zoomBox.style.left = evt.xy.x+"px";
323 if (evt.xy.y < this.mouseDragStart.y) {
324 this.zoomBox.style.top = evt.xy.y+"px";
328 var deltaX = this.mouseDragStart.x - evt.xy.x;
329 var deltaY = this.mouseDragStart.y - evt.xy.y;
330 var size = this.map.getSize();
331 var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX,
332 size.h / 2 + deltaY);
333 var newCenter = this.map.getLonLatFromViewPortPx( newXY );
334 this.map.setCenter(newCenter, null, true);
335 this.mouseDragStart = evt.xy.clone();
337 this.performedDrag = true;
342 * Method: defaultMouseUp
347 defaultMouseUp: function (evt) {
348 if (!OpenLayers.Event.isLeftClick(evt)) {
353 this.zoomBoxEnd(evt);
354 if (this.startViaKeyboard) {
359 if (this.performedDrag) {
360 this.map.setCenter(this.map.center);
363 document.onselectstart = null;
364 this.mouseDragStart = null;
365 this.map.div.style.cursor = "default";
369 * Method: defaultMouseOut
374 defaultMouseOut: function (evt) {
375 if (this.mouseDragStart != null
376 && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
378 this.removeZoomBox();
379 if (this.startViaKeyboard) {
383 this.mouseDragStart = null;
384 this.map.div.style.cursor = "default";
389 * Method: defaultClick
394 defaultClick: function (evt) {
395 if (this.performedDrag) {
396 this.performedDrag = false;
401 CLASS_NAME: "OpenLayers.Control.MouseToolbar"
404 OpenLayers.Control.MouseToolbar.X = 6;
405 OpenLayers.Control.MouseToolbar.Y = 300;