From d2f9ebae41eac2edf3722d682be0eed087c5c4eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Sep 2012 15:16:14 +0200 Subject: [PATCH] Add mouseWheelZoomDelta option to MapOptions --- externs/ol.js | 6 ++++ src/ol/interaction/mousewheelzoom.js | 17 ++++++----- src/ol/mapoptions.js | 6 +++- test/spec/ol/mapoptions.test.js | 43 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/externs/ol.js b/externs/ol.js index 4042c932d1..1a50100ff7 100644 --- a/externs/ol.js +++ b/externs/ol.js @@ -70,6 +70,12 @@ olx.MapOptionsExtern.prototype.maxResolution; olx.MapOptionsExtern.prototype.mouseWheelZoom; +/** + * @type {number|undefined} + */ +olx.MapOptionsExtern.prototype.mouseWheelZoomDelta; + + /** * @type {number|undefined} */ diff --git a/src/ol/interaction/mousewheelzoom.js b/src/ol/interaction/mousewheelzoom.js index 2b2be23448..c9b9ec47a7 100644 --- a/src/ol/interaction/mousewheelzoom.js +++ b/src/ol/interaction/mousewheelzoom.js @@ -9,8 +9,15 @@ goog.require('ol.MapBrowserEvent'); /** * @constructor * @extends {ol.interaction.Interaction} + * @param {number} delta The zoom delta applied on each mousewheel. */ -ol.interaction.MouseWheelZoom = function() { +ol.interaction.MouseWheelZoom = function(delta) { + /** + * @private + * @type {number} + */ + this.delta_ = delta; + goog.base(this); }; goog.inherits(ol.interaction.MouseWheelZoom, ol.interaction.Interaction); @@ -28,12 +35,8 @@ ol.interaction.MouseWheelZoom.prototype.handleMapBrowserEvent = mapBrowserEvent.browserEvent; goog.asserts.assert(mouseWheelEvent instanceof goog.events.MouseWheelEvent); var anchor = mapBrowserEvent.getCoordinate(); - var oldResolution = map.getResolution(); - var factor = Math.exp(Math.log(2) / 4); - if (mouseWheelEvent.deltaY < 0) { - factor = 1 / factor; - } - map.zoomToResolution(oldResolution * factor, anchor); + var delta = mouseWheelEvent.deltaY < 0 ? this.delta_ : -this.delta_; + map.zoom(delta, anchor); mapBrowserEvent.preventDefault(); mouseWheelEvent.preventDefault(); } diff --git a/src/ol/mapoptions.js b/src/ol/mapoptions.js index 2ad5392dda..7030c03118 100644 --- a/src/ol/mapoptions.js +++ b/src/ol/mapoptions.js @@ -64,6 +64,7 @@ ol.DEFAULT_RENDERER_HINTS = [ * layers: (ol.Collection|undefined), * maxResolution: (number|undefined), * mouseWheelZoom: (boolean|undefined), + * mouseWheelZoomDelta: (number|undefined), * numZoomLevels: (number|undefined), * projection: (ol.Projection|string|undefined), * renderer: (ol.RendererHint|undefined), @@ -241,7 +242,10 @@ ol.MapOptions.createInteractions_ = function(mapOptionsLiteral) { var mouseWheelZoom = goog.isDef(mapOptionsLiteral.mouseWheelZoom) ? mapOptionsLiteral.mouseWheelZoom : true; if (mouseWheelZoom) { - interactions.push(new ol.interaction.MouseWheelZoom()); + var mouseWheelZoomDelta = + goog.isDef(mapOptionsLiteral.mouseWheelZoomDelta) ? + mapOptionsLiteral.mouseWheelZoomDelta : 1; + interactions.push(new ol.interaction.MouseWheelZoom(mouseWheelZoomDelta)); } var shiftDragZoom = goog.isDef(mapOptionsLiteral.shiftDragZoom) ? diff --git a/test/spec/ol/mapoptions.test.js b/test/spec/ol/mapoptions.test.js index cfdb6a9815..e1ef3d2e72 100644 --- a/test/spec/ol/mapoptions.test.js +++ b/test/spec/ol/mapoptions.test.js @@ -51,4 +51,47 @@ describe('ol.MapOptions', function() { }); }); + + describe('create interactions', function() { + + var options; + + beforeEach(function() { + options = { + rotate: false, + doubleClickZoom: false, + dragPan: false, + keyboard: false, + mouseWheelZoom: false, + shiftDragZoom: false + }; + }); + + describe('create mousewheel interaction', function() { + + beforeEach(function() { + options.mouseWheelZoom = true; + }); + + describe('default mouseWheelZoomDelta', function() { + it('create mousewheel interaction with default delta', function() { + var interactions = ol.MapOptions.createInteractions_(options); + expect(interactions.getLength()).toEqual(1); + expect(interactions.getAt(0)).toBeA(ol.interaction.MouseWheelZoom); + expect(interactions.getAt(0).delta_).toEqual(1); + }); + }); + + describe('set mouseWheelZoomDelta', function() { + it('create mousewheel interaction with default delta', function() { + options.mouseWheelZoomDelta = 7; + var interactions = ol.MapOptions.createInteractions_(options); + expect(interactions.getLength()).toEqual(1); + expect(interactions.getAt(0)).toBeA(ol.interaction.MouseWheelZoom); + expect(interactions.getAt(0).delta_).toEqual(7); + }); + }); + }); + }); + });