Guest User

Fix for Openlayers

a guest
Sep 21st, 2012
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Class: OpenLayers.Control.PanZoomBar
  3.  * The PanZoomBar is a visible control composed of a
  4.  * <OpenLayers.Control.PanPanel> and a <OpenLayers.Control.ZoomBar>.
  5.  * By default it is displayed in the upper left corner of the map as 4
  6.  * directional arrows above a vertical slider.
  7.  *
  8.  * Inherits from:
  9.  *  - <OpenLayers.Control.PanZoom>
  10.  */
  11. OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
  12.  
  13.     /**
  14.      * APIProperty: zoomStopWidth
  15.      */
  16.     zoomStopWidth: 18,
  17.  
  18.     /**
  19.      * APIProperty: zoomStopHeight
  20.      */
  21.     zoomStopHeight: 11,
  22.  
  23.     /**
  24.      * Property: slider
  25.      */
  26.     slider: null,
  27.  
  28.     /**
  29.      * Property: sliderEvents
  30.      * {<OpenLayers.Events>}
  31.      */
  32.     sliderEvents: null,
  33.  
  34.     /**
  35.      * Property: zoombarDiv
  36.      * {DOMElement}
  37.      */
  38.     zoombarDiv: null,
  39.  
  40.     /**
  41.      * APIProperty: zoomWorldIcon
  42.      * {Boolean}
  43.      */
  44.     zoomWorldIcon: false,
  45.  
  46.     /**
  47.      * APIProperty: panIcons
  48.      * {Boolean} Set this property to false not to display the pan icons. If
  49.      * false the zoom world icon is placed under the zoom bar. Defaults to
  50.      * true.
  51.      */
  52.     panIcons: true,
  53.  
  54.     /**
  55.      * APIProperty: forceFixedZoomLevel
  56.      * {Boolean} Force a fixed zoom level even though the map has
  57.      *     fractionalZoom
  58.      */
  59.     forceFixedZoomLevel: false,
  60.  
  61.     /**
  62.      * Property: mouseDragStart
  63.      * {<OpenLayers.Pixel>}
  64.      */
  65.     mouseDragStart: null,
  66.  
  67.     /**
  68.      * Property: deltaY
  69.      * {Number} The cumulative vertical pixel offset during a zoom bar drag.
  70.      */
  71.     deltaY: null,
  72.  
  73.     /**
  74.      * Property: zoomStart
  75.      * {<OpenLayers.Pixel>}
  76.      */
  77.     zoomStart: null,
  78.  
  79.     /**
  80.      * Constructor: OpenLayers.Control.PanZoomBar
  81.      */
  82.  
  83.     /**
  84.      * APIMethod: destroy
  85.      */
  86.     destroy: function() {
  87.  
  88.         this._removeZoomBar();
  89.  
  90.         this.map.events.un({
  91.             "changebaselayer": this.redraw,
  92.             scope: this
  93.         });
  94.  
  95.         OpenLayers.Control.PanZoom.prototype.destroy.apply(this, arguments);
  96.  
  97.         delete this.mouseDragStart;
  98.         delete this.zoomStart;
  99.     },
  100.  
  101.     /**
  102.      * Method: setMap
  103.      *
  104.      * Parameters:
  105.      * map - {<OpenLayers.Map>}
  106.      */
  107.     setMap: function(map) {
  108.         OpenLayers.Control.PanZoom.prototype.setMap.apply(this, arguments);
  109.         this.map.events.register("changebaselayer", this, this.redraw);
  110.     },
  111.  
  112.     /**
  113.      * Method: redraw
  114.      * clear the div and start over.
  115.      */
  116.     redraw: function() {
  117.         if (this.div != null) {
  118.             this.removeButtons();
  119.             this._removeZoomBar();
  120.         }
  121.         this.draw();
  122.     },
  123.  
  124.     /**
  125.     * Method: draw
  126.     *
  127.     * Parameters:
  128.     * px - {<OpenLayers.Pixel>}
  129.     */
  130.     draw: function(px) {
  131.         // initialize our internal div
  132.         OpenLayers.Control.prototype.draw.apply(this, arguments);
  133.         px = this.position.clone();
  134.  
  135.         // place the controls
  136.         this.buttons = [];
  137.  
  138.         var sz = {w: 18, h: 18};
  139.         if (this.panIcons) {
  140.             var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
  141.             var wposition = sz.w;
  142.  
  143.             if (this.zoomWorldIcon) {
  144.                 centered = new OpenLayers.Pixel(px.x+sz.w, px.y);
  145.             }
  146.  
  147.             this._addButton("panup", "north-mini.png", centered, sz);
  148.             px.y = centered.y+sz.h;
  149.             this._addButton("panleft", "west-mini.png", px, sz);
  150.             if (this.zoomWorldIcon) {
  151.                 this._addButton("zoomworld", "zoom-world-mini.png", px.add(sz.w, 0), sz);
  152.  
  153.                 wposition *= 2;
  154.             }
  155.             this._addButton("panright", "east-mini.png", px.add(wposition, 0), sz);
  156.             this._addButton("pandown", "south-mini.png", centered.add(0, sz.h*2), sz);
  157.             this._addButton("zoomin", "zoom-plus-mini.png", centered.add(0, sz.h*3+5), sz);
  158.             centered = this._addZoomBar(centered.add(0, sz.h*4 + 5));
  159.             this._addButton("zoomout", "zoom-minus-mini.png", centered, sz);
  160.         }
  161.         else {
  162.             this._addButton("zoomin", "zoom-plus-mini.png", px, sz);
  163.             centered = this._addZoomBar(px.add(0, sz.h));
  164.             this._addButton("zoomout", "zoom-minus-mini.png", centered, sz);
  165.             if (this.zoomWorldIcon) {
  166.                 centered = centered.add(0, sz.h+3);
  167.                 this._addButton("zoomworld", "zoom-world-mini.png", centered, sz);
  168.             }
  169.         }
  170.         return this.div;
  171.     },
  172.  
  173.     /**
  174.     * Method: _addZoomBar
  175.     *
  176.     * Parameters:
  177.     * centered - {<OpenLayers.Pixel>} where zoombar drawing is to start.
  178.     */
  179.     _addZoomBar:function(centered) {
  180.         var imgLocation = OpenLayers.Util.getImageLocation("slider.png");
  181.         var id = this.id + "_" + this.map.id;
  182.         var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
  183.         var slider = OpenLayers.Util.createAlphaImageDiv(id,
  184.                        centered.add(-1, zoomsToEnd * this.zoomStopHeight),
  185.                        {w: 20, h: 9},
  186.                        imgLocation,
  187.                        "absolute");
  188.         slider.style.cursor = "move";
  189.         this.slider = slider;
  190.  
  191.         this.sliderEvents = new OpenLayers.Events(this, slider, null, true,
  192.                                             {includeXY: true});
  193.         this.sliderEvents.on({
  194.             "touchstart": this.zoomBarDown,
  195.             "touchmove": this.zoomBarDrag,
  196.             "touchend": this.zoomBarUp,
  197.             "mousedown": this.zoomBarDown,
  198.             "mousemove": this.zoomBarDrag,
  199.             "mouseup": this.zoomBarUp
  200.         });
  201.  
  202.         var sz = {
  203.             w: this.zoomStopWidth,
  204.             h: this.zoomStopHeight * this.map.getNumZoomLevels()
  205.         };
  206.  
  207.         /*xxx*/
  208.         sz.h=this.zoomStopHeight*Math.max(this.map.getNumZoomLevels()-this.map.minZoom,1);
  209.  
  210.  
  211.         var imgLocation = OpenLayers.Util.getImageLocation("zoombar.png");
  212.         var div = null;
  213.  
  214.         if (OpenLayers.Util.alphaHack()) {
  215.             var id = this.id + "_" + this.map.id;
  216.             div = OpenLayers.Util.createAlphaImageDiv(id, centered,
  217.                                       {w: sz.w, h: this.zoomStopHeight},
  218.                                       imgLocation,
  219.                                       "absolute", null, "crop");
  220.             div.style.height = sz.h + "px";
  221.         } else {
  222.             div = OpenLayers.Util.createDiv(
  223.                         'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id,
  224.                         centered,
  225.                         sz,
  226.                         imgLocation);
  227.         }
  228.         div.style.cursor = "pointer";
  229.         div.className = "olButton";
  230.         this.zoombarDiv = div;
  231.  
  232.         this.div.appendChild(div);
  233.  
  234.         this.startTop = parseInt(div.style.top);
  235.         this.div.appendChild(slider);
  236.  
  237.         this.map.events.register("zoomend", this, this.moveZoomBar);
  238.  
  239.         centered = centered.add(0, sz.h);
  240.         return centered;
  241.     },
  242.  
  243.     /**
  244.      * Method: _removeZoomBar
  245.      */
  246.     _removeZoomBar: function() {
  247.         this.sliderEvents.un({
  248.             "touchstart": this.zoomBarDown,
  249.             "touchmove": this.zoomBarDrag,
  250.             "touchend": this.zoomBarUp,
  251.             "mousedown": this.zoomBarDown,
  252.             "mousemove": this.zoomBarDrag,
  253.             "mouseup": this.zoomBarUp
  254.         });
  255.         this.sliderEvents.destroy();
  256.  
  257.         this.div.removeChild(this.zoombarDiv);
  258.         this.zoombarDiv = null;
  259.         this.div.removeChild(this.slider);
  260.         this.slider = null;
  261.  
  262.         this.map.events.unregister("zoomend", this, this.moveZoomBar);
  263.     },
  264.  
  265.     /**
  266.      * Method: onButtonClick
  267.      *
  268.      * Parameters:
  269.      * evt - {Event}
  270.      */
  271.     onButtonClick: function(evt) {
  272.         OpenLayers.Control.PanZoom.prototype.onButtonClick.apply(this, arguments);
  273.         if (evt.buttonElement === this.zoombarDiv) {
  274.             var levels = evt.buttonXY.y / this.zoomStopHeight;
  275.             if(this.forceFixedZoomLevel || !this.map.fractionalZoom) {
  276.                 levels = Math.floor(levels);
  277.             }
  278.             var zoom = (this.map.getNumZoomLevels() - 1) - levels;
  279.             zoom = Math.min(Math.max(zoom, 0), this.map.getNumZoomLevels() - 1);
  280.             this.map.zoomTo(zoom);
  281.         }
  282.     },
  283.  
  284.     /**
  285.      * Method: passEventToSlider
  286.      * This function is used to pass events that happen on the div, or the map,
  287.      * through to the slider, which then does its moving thing.
  288.      *
  289.      * Parameters:
  290.      * evt - {<OpenLayers.Event>}
  291.      */
  292.     passEventToSlider:function(evt) {
  293.         this.sliderEvents.handleBrowserEvent(evt);
  294.     },
  295.  
  296.     /*
  297.      * Method: zoomBarDown
  298.      * event listener for clicks on the slider
  299.      *
  300.      * Parameters:
  301.      * evt - {<OpenLayers.Event>}
  302.      */
  303.     zoomBarDown:function(evt) {
  304.         if (!OpenLayers.Event.isLeftClick(evt) && !OpenLayers.Event.isSingleTouch(evt)) {
  305.             return;
  306.         }
  307.         this.map.events.on({
  308.             "touchmove": this.passEventToSlider,
  309.             "mousemove": this.passEventToSlider,
  310.             "mouseup": this.passEventToSlider,
  311.             scope: this
  312.         });
  313.         this.mouseDragStart = evt.xy.clone();
  314.         this.zoomStart = evt.xy.clone();
  315.         this.div.style.cursor = "move";
  316.         // reset the div offsets just in case the div moved
  317.         this.zoombarDiv.offsets = null;
  318.         OpenLayers.Event.stop(evt);
  319.     },
  320.  
  321.     /*
  322.      * Method: zoomBarDrag
  323.      * This is what happens when a click has occurred, and the client is
  324.      * dragging.  Here we must ensure that the slider doesn't go beyond the
  325.      * bottom/top of the zoombar div, as well as moving the slider to its new
  326.      * visual location
  327.      *
  328.      * Parameters:
  329.      * evt - {<OpenLayers.Event>}
  330.      */
  331.     zoomBarDrag:function(evt) {
  332.         if (this.mouseDragStart != null) {
  333.             var deltaY = this.mouseDragStart.y - evt.xy.y;
  334.             var offsets = OpenLayers.Util.pagePosition(this.zoombarDiv);
  335.             if ((evt.clientY - offsets[1]) > 0 &&
  336.                 (evt.clientY - offsets[1]) < parseInt(this.zoombarDiv.style.height) - 2) {
  337.                 var newTop = parseInt(this.slider.style.top) - deltaY;
  338.                 this.slider.style.top = newTop+"px";
  339.                 this.mouseDragStart = evt.xy.clone();
  340.             }
  341.             // set cumulative displacement
  342.             this.deltaY = this.zoomStart.y - evt.xy.y;
  343.             OpenLayers.Event.stop(evt);
  344.         }
  345.     },
  346.  
  347.     /*
  348.      * Method: zoomBarUp
  349.      * Perform cleanup when a mouseup event is received -- discover new zoom
  350.      * level and switch to it.
  351.      *
  352.      * Parameters:
  353.      * evt - {<OpenLayers.Event>}
  354.      */
  355.     zoomBarUp:function(evt) {
  356.         if (!OpenLayers.Event.isLeftClick(evt) && evt.type !== "touchend") {
  357.             return;
  358.         }
  359.         if (this.mouseDragStart) {
  360.             this.div.style.cursor="";
  361.             this.map.events.un({
  362.                 "touchmove": this.passEventToSlider,
  363.                 "mouseup": this.passEventToSlider,
  364.                 "mousemove": this.passEventToSlider,
  365.                 scope: this
  366.             });
  367.             var zoomLevel = this.map.zoom;
  368.             if (!this.forceFixedZoomLevel && this.map.fractionalZoom) {
  369.                 zoomLevel += this.deltaY/this.zoomStopHeight;
  370.                 zoomLevel = Math.min(Math.max(zoomLevel, 0),
  371.                                      this.map.getNumZoomLevels() - 1);
  372.             } else {
  373.                 zoomLevel += this.deltaY/this.zoomStopHeight;
  374.                 zoomLevel = Math.max(Math.round(zoomLevel), 0);
  375.             }
  376.             /*xxxx
  377.             this.map.zoomTo(zoomLevel);*/
  378.             this.map.zoomTo(Math.max(zoomLevel, this.map.minZoom));
  379.             this.mouseDragStart = null;
  380.             this.zoomStart = null;
  381.             this.deltaY = 0;
  382.             OpenLayers.Event.stop(evt);
  383.             this.moveZoomBar();
  384.         }
  385.     },
  386.  
  387.     /*
  388.     * Method: moveZoomBar
  389.     * Change the location of the slider to match the current zoom level.
  390.     */
  391.     moveZoomBar:function() {
  392.         /*xxxxx
  393.         var newTop =
  394.             ((this.map.getNumZoomLevels()-1) - this.map.getZoom()) *
  395.             this.zoomStopHeight + this.startTop + 1;
  396.  */
  397.         var newTop=((this.map.getNumZoomLevels()-1)-Math.max(this.map.getZoom(),this.map.minZoom))*this.zoomStopHeight+this.startTop+1;
  398.         this.slider.style.top = newTop + "px";
  399.     },
  400.  
  401.     CLASS_NAME: "OpenLayers.Control.PanZoomBar"
  402. });
Advertisement
Add Comment
Please, Sign In to add comment