1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
 
   2  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
 
   3  * text of the license. */
 
   4 // @require: OpenLayers/Control.js
 
   5 OpenLayers.Control.MouseDefaults = Class.create();
 
   6 OpenLayers.Control.MouseDefaults.prototype = 
 
   7   Object.extend( new OpenLayers.Control(), {
 
  11     initialize: function() {
 
  12         OpenLayers.Control.prototype.initialize.apply(this, arguments);
 
  16         this.map.events.register( "click", this, this.defaultClick );
 
  17         this.map.events.register( "dblclick", this, this.defaultDblClick );
 
  18         this.map.events.register( "mousedown", this, this.defaultMouseDown );
 
  19         this.map.events.register( "mouseup", this, this.defaultMouseUp );
 
  20         this.map.events.register( "mousemove", this, this.defaultMouseMove );
 
  21         this.map.events.register( "mouseout", this, this.defaultMouseOut );
 
  24     defaultClick: function (evt) {
 
  25         if (!Event.isLeftClick(evt)) return;
 
  26         var notAfterDrag = !this.performedDrag;
 
  27         this.performedDrag = false;
 
  34     defaultDblClick: function (evt) {
 
  35         var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); 
 
  36         this.map.setCenter(newCenter, this.map.zoom + 1);
 
  42     defaultMouseDown: function (evt) {
 
  43         if (!Event.isLeftClick(evt)) return;
 
  44         this.mouseDragStart = evt.xy.copyOf();
 
  45         this.performedDrag  = false;
 
  47             this.map.div.style.cursor = "crosshair";
 
  48             this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
 
  54             this.zoomBox.style.backgroundColor = "white";
 
  55             this.zoomBox.style.filter = "alpha(opacity=50)"; // IE
 
  56             this.zoomBox.style.opacity = "0.50";
 
  57             this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
 
  58             this.map.viewPortDiv.appendChild(this.zoomBox);
 
  60         document.onselectstart=function() { return false; }
 
  67     defaultMouseMove: function (evt) {
 
  68         if (this.mouseDragStart != null) {
 
  70                 var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x);
 
  71                 var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y);
 
  72                 this.zoomBox.style.width = deltaX+"px";
 
  73                 this.zoomBox.style.height = deltaY+"px";
 
  74                 if (evt.xy.x < this.mouseDragStart.x) {
 
  75                     this.zoomBox.style.left = evt.xy.x+"px";
 
  77                 if (evt.xy.y < this.mouseDragStart.y) {
 
  78                     this.zoomBox.style.top = evt.xy.y+"px";
 
  81                 var deltaX = this.mouseDragStart.x - evt.xy.x;
 
  82                 var deltaY = this.mouseDragStart.y - evt.xy.y;
 
  83                 var size = this.map.getSize();
 
  84                 var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX,
 
  86                 var newCenter = this.map.getLonLatFromViewPortPx( newXY ); 
 
  87                 this.map.setCenter(newCenter, null, true);
 
  88                 this.mouseDragStart = evt.xy.copyOf();
 
  89                 this.map.div.style.cursor = "move";
 
  91             this.performedDrag = true;
 
  98     defaultMouseUp: function (evt) {
 
  99         if (!Event.isLeftClick(evt)) return;
 
 101             var start = this.map.getLonLatFromViewPortPx( this.mouseDragStart ); 
 
 102             var end = this.map.getLonLatFromViewPortPx( evt.xy );
 
 103             var top = Math.max(start.lat, end.lat);
 
 104             var bottom = Math.min(start.lat, end.lat);
 
 105             var left = Math.min(start.lon, end.lon);
 
 106             var right = Math.max(start.lon, end.lon);
 
 107             var bounds = new OpenLayers.Bounds(left, bottom, right, top);
 
 108             var zoom = this.map.getZoomForExtent(bounds);
 
 109             this.map.setCenter(new OpenLayers.LonLat(
 
 110               (start.lon + end.lon) / 2,
 
 111               (start.lat + end.lat) / 2
 
 113             this.map.viewPortDiv.removeChild(document.getElementById("zoomBox"));
 
 116             this.map.setCenter(this.map.center);
 
 118         document.onselectstart=null;
 
 119         this.mouseDragStart = null;
 
 120         this.map.div.style.cursor = "default";
 
 123     defaultMouseOut: function (evt) {
 
 124         if (this.mouseDragStart != null
 
 125             && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
 
 126                 this.defaultMouseUp(evt);