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/PanZoom.js
 
   7 // default zoom/pan controls
 
   9 OpenLayers.Control.PanZoomBar = Class.create();
 
  10 OpenLayers.Control.PanZoomBar.X = 4;
 
  11 OpenLayers.Control.PanZoomBar.Y = 4;
 
  12 OpenLayers.Control.PanZoomBar.prototype = 
 
  13   Object.extend( new OpenLayers.Control.PanZoom(), {
 
  14     /** @type Array(...) */
 
  23     initialize: function() {
 
  24         OpenLayers.Control.PanZoom.prototype.initialize.apply(this, arguments);
 
  25         this.position = new OpenLayers.Pixel(OpenLayers.Control.PanZoomBar.X,
 
  26                                              OpenLayers.Control.PanZoomBar.Y);
 
  30     * @param {OpenLayers.Pixel} px
 
  33         // initialize our internal div
 
  34         OpenLayers.Control.prototype.draw.apply(this, arguments);
 
  38         this.buttons = new Array();
 
  40         var sz = new OpenLayers.Size(18,18);
 
  41         var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
 
  43         this._addButton("panup", "north-mini.png", centered, sz);
 
  44         px.y = centered.y+sz.h;
 
  45         this._addButton("panleft", "west-mini.png", px, sz);
 
  46         this._addButton("panright", "east-mini.png", px.add(sz.w, 0), sz);
 
  47         this._addButton("pandown", "south-mini.png", centered.add(0, sz.h*2), sz);
 
  48         this._addButton("zoomin", "zoom-plus-mini.png", centered.add(0, sz.h*3+5), sz);
 
  49         centered = this._addZoomBar(centered.add(0, sz.h*4 + 5));
 
  50         this._addButton("zoomout", "zoom-minus-mini.png", centered, sz);
 
  55     * @param {OpenLayers.Pixel} location where zoombar drawing is to start.
 
  57     _addZoomBar:function(centered) {
 
  58         var imgLocation = OpenLayers.Util.getImagesLocation();
 
  60         var id = "OpenLayers_Control_PanZoomBar_Slider" + this.map.id;
 
  61         var slider = OpenLayers.Util.createAlphaImageDiv(id,
 
  63                          (this.map.getZoomLevels())*this.zoomStopHeight), 
 
  64                        new OpenLayers.Size(20,9), 
 
  65                        imgLocation+"slider.png",
 
  69         this.sliderEvents = new OpenLayers.Events(this, slider);
 
  70         this.sliderEvents.register("mousedown", this, this.zoomBarDown);
 
  71         this.sliderEvents.register("mousemove", this, this.zoomBarDrag);
 
  72         this.sliderEvents.register("mouseup", this, this.zoomBarUp);
 
  73         this.sliderEvents.register("dblclick", this, this.doubleClick);
 
  74         this.sliderEvents.register("click", this, this.doubleClick);
 
  76         sz = new OpenLayers.Size();
 
  77         sz.h = this.zoomStopHeight*(this.map.getZoomLevels()+1);
 
  78         sz.w = this.zoomStopWidth;
 
  81         if (OpenLayers.Util.alphaHack()) {
 
  82             var id = "OpenLayers_Control_PanZoomBar" + this.map.id;
 
  83             div = OpenLayers.Util.createAlphaImageDiv(id, centered,
 
  84                                       new OpenLayers.Size(sz.w, 
 
  86                                       imgLocation + "zoombar.png", 
 
  87                                       "absolute", null, "crop");
 
  88             div.style.height = sz.h;
 
  90             div = OpenLayers.Util.createDiv(
 
  91                         'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id,
 
  94                         imgLocation+"zoombar.png");
 
  97         this.zoombarDiv = div;
 
  99         this.divEvents = new OpenLayers.Events(this, div);
 
 100         this.divEvents.register("mousedown", this, this.divClick);
 
 101         this.divEvents.register("mousemove", this, this.passEventToSlider);
 
 102         this.divEvents.register("dblclick", this, this.doubleClick);
 
 103         this.divEvents.register("click", this, this.doubleClick);
 
 105         this.div.appendChild(div);
 
 107         this.startTop = parseInt(div.style.top);
 
 108         this.div.appendChild(slider);
 
 110         this.map.events.register("zoomend", this, this.moveZoomBar);
 
 112         centered = centered.add(0, 
 
 113             this.zoomStopHeight*(this.map.getZoomLevels()+1));
 
 118      * This function is used to pass events that happen on the div, or the map,
 
 119      * through to the slider, which then does its moving thing.
 
 121     passEventToSlider:function(evt) {
 
 122         this.sliderEvents.handleBrowserEvent(evt);
 
 126      * divClick: Picks up on clicks directly on the zoombar div
 
 127      *           and sets the zoom level appropriately.
 
 129     divClick: function (evt) {
 
 130         if (!Event.isLeftClick(evt)) return;
 
 132         var top = Position.page(evt.object)[1];
 
 133         var levels = Math.floor((y - top)/this.zoomStopHeight);
 
 134         this.map.zoomTo(this.map.getZoomLevels() - levels);
 
 140      * event listener for clicks on the slider
 
 142     zoomBarDown:function(evt) {
 
 143         if (!Event.isLeftClick(evt)) return;
 
 144         this.map.events.register("mousemove", this, this.passEventToSlider);
 
 145         this.map.events.register("mouseup", this, this.passEventToSlider);
 
 146         this.mouseDragStart = evt.xy.copyOf();
 
 147         this.zoomStart = evt.xy.copyOf();
 
 148         this.div.style.cursor = "move";
 
 154      * This is what happens when a click has occurred, and the client is dragging.
 
 155      * Here we must ensure that the slider doesn't go beyond the bottom/top of the 
 
 156      * zoombar div, as well as moving the slider to its new visual location
 
 158     zoomBarDrag:function(evt) {
 
 159         if (this.mouseDragStart != null) {
 
 160             var deltaY = this.mouseDragStart.y - evt.xy.y
 
 161             var offsets = Position.page(this.zoombarDiv);
 
 162             if ((evt.clientY - offsets[1]) > 0 && 
 
 163                 (evt.clientY - offsets[1]) < parseInt(this.zoombarDiv.style.height) - 2) {
 
 164                 var newTop = parseInt(this.slider.style.top) - deltaY;
 
 165                 this.slider.style.top = newTop+"px";
 
 167             this.mouseDragStart = evt.xy.copyOf();
 
 174      * Perform cleanup when a mouseup event is received -- discover new zoom level
 
 177     zoomBarUp:function(evt) {
 
 178         if (!Event.isLeftClick(evt)) return;
 
 179         if (this.zoomStart) {
 
 180             this.div.style.cursor="default";
 
 181             this.map.events.remove("mousemove");
 
 182             this.map.events.remove("mouseup");
 
 183             var deltaY = this.zoomStart.y - evt.xy.y
 
 184             this.map.zoomTo(this.map.zoom + Math.round(deltaY/this.zoomStopHeight));
 
 186             this.mouseDragStart = null;
 
 192     * Change the location of the slider to match the current zoom level.
 
 194     moveZoomBar:function() {
 
 196             (this.map.getZoomLevels() - this.map.getZoom()) * this.zoomStopHeight
 
 198         this.slider.style.top = newTop + "px";
 
 201     CLASS_NAME: "OpenLayers.Control.PanZoomBar"