diff --git a/lib/OpenLayers/Control/PanZoom.js b/lib/OpenLayers/Control/PanZoom.js index f335dc2e6d..e92036059b 100644 --- a/lib/OpenLayers/Control/PanZoom.js +++ b/lib/OpenLayers/Control/PanZoom.js @@ -21,10 +21,20 @@ OpenLayers.Control.PanZoom = OpenLayers.Class(OpenLayers.Control, { /** * APIProperty: slideFactor * {Integer} Number of pixels by which we'll pan the map in any direction - * on clicking the arrow buttons. + * on clicking the arrow buttons. If you want to pan by some ratio + * of the map dimensions, use instead. */ slideFactor: 50, + /** + * APIProperty: slideRatio + * {Number} The fraction of map width/height by which we'll pan the map + * on clicking the arrow buttons. Default is null. If set, will + * override . E.g. if slideRatio is .5, then the Pan Up + * button will pan up half the map height. + */ + slideRatio: null, + /** * Property: buttons * {Array(DOMElement)} Array of Button Divs @@ -124,7 +134,20 @@ OpenLayers.Control.PanZoom = OpenLayers.Class(OpenLayers.Control, { OpenLayers.Function.bindAsEventListener(this.doubleClick, btn)); btn.action = id; btn.map = this.map; - btn.slideFactor = this.slideFactor; + + if(!this.slideRatio){ + var slideFactorPixels = this.slideFactor; + var getSlideFactor = function() { + return slideFactorPixels; + }; + } else { + var slideRatio = this.slideRatio; + var getSlideFactor = function(dim) { + return this.map.getSize()[dim] * slideRatio; + }; + } + + btn.getSlideFactor = getSlideFactor; //we want to remember/reference the outer div this.buttons.push(btn); @@ -180,16 +203,16 @@ OpenLayers.Control.PanZoom = OpenLayers.Class(OpenLayers.Control, { switch (this.action) { case "panup": - this.map.pan(0, -this.slideFactor); + this.map.pan(0, -this.getSlideFactor("h")); break; case "pandown": - this.map.pan(0, this.slideFactor); + this.map.pan(0, this.getSlideFactor("h")); break; case "panleft": - this.map.pan(-this.slideFactor, 0); + this.map.pan(-this.getSlideFactor("w"), 0); break; case "panright": - this.map.pan(this.slideFactor, 0); + this.map.pan(this.getSlideFactor("w"), 0); break; case "zoomin": this.map.zoomIn(); diff --git a/tests/Control/PanZoom.html b/tests/Control/PanZoom.html index 65fb84e623..1f2deec0ea 100644 --- a/tests/Control/PanZoom.html +++ b/tests/Control/PanZoom.html @@ -138,6 +138,56 @@ } } + function test_slideRatio(t) { + t.plan(4); + + var control = new OpenLayers.Control.PanZoom({ + slideRatio: .5 + }); + + var map = new OpenLayers.Map(); + + map.addControl(control); + control.draw(); + control.activate(); + + map.getSize = function() { + return { + w: 250, + h: 100 + } + }; + + var delta, dir; + var evt = {which: 1}; // a fake left click + var buttons = control.buttons; + map.pan = function(dx, dy){ + t.eq([dx,dy],delta,"Panning " + dir + " sets right delta with slideRatio"); + }; + + //up + var delta = [0, -50]; + var dir = "up"; + control.buttonDown.call(buttons[0], evt); + + //left + var delta = [-125, 0]; + var dir = "left"; + control.buttonDown.call(buttons[1], evt); + + //right + var delta = [125, 0]; + var dir = "right"; + control.buttonDown.call(buttons[2], evt); + + //down + var delta = [0, 50]; + var dir = "down"; + control.buttonDown.call(buttons[3], evt); + + map.destroy(); + } + function simulateClick(wnd, elem) { var evt = wnd.document.createEvent("MouseEvents"); evt.initMouseEvent("mousedown", true, true, wnd, 0, 0, 0, 0, 0, false, false, false, false, 0, null);