Add mouseWheelZoomDelta option to MapOptions

This commit is contained in:
Éric Lemoine
2012-09-27 15:16:14 +02:00
parent dd425ff922
commit d2f9ebae41
4 changed files with 64 additions and 8 deletions
+6
View File
@@ -70,6 +70,12 @@ olx.MapOptionsExtern.prototype.maxResolution;
olx.MapOptionsExtern.prototype.mouseWheelZoom; olx.MapOptionsExtern.prototype.mouseWheelZoom;
/**
* @type {number|undefined}
*/
olx.MapOptionsExtern.prototype.mouseWheelZoomDelta;
/** /**
* @type {number|undefined} * @type {number|undefined}
*/ */
+10 -7
View File
@@ -9,8 +9,15 @@ goog.require('ol.MapBrowserEvent');
/** /**
* @constructor * @constructor
* @extends {ol.interaction.Interaction} * @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.base(this);
}; };
goog.inherits(ol.interaction.MouseWheelZoom, ol.interaction.Interaction); goog.inherits(ol.interaction.MouseWheelZoom, ol.interaction.Interaction);
@@ -28,12 +35,8 @@ ol.interaction.MouseWheelZoom.prototype.handleMapBrowserEvent =
mapBrowserEvent.browserEvent; mapBrowserEvent.browserEvent;
goog.asserts.assert(mouseWheelEvent instanceof goog.events.MouseWheelEvent); goog.asserts.assert(mouseWheelEvent instanceof goog.events.MouseWheelEvent);
var anchor = mapBrowserEvent.getCoordinate(); var anchor = mapBrowserEvent.getCoordinate();
var oldResolution = map.getResolution(); var delta = mouseWheelEvent.deltaY < 0 ? this.delta_ : -this.delta_;
var factor = Math.exp(Math.log(2) / 4); map.zoom(delta, anchor);
if (mouseWheelEvent.deltaY < 0) {
factor = 1 / factor;
}
map.zoomToResolution(oldResolution * factor, anchor);
mapBrowserEvent.preventDefault(); mapBrowserEvent.preventDefault();
mouseWheelEvent.preventDefault(); mouseWheelEvent.preventDefault();
} }
+5 -1
View File
@@ -64,6 +64,7 @@ ol.DEFAULT_RENDERER_HINTS = [
* layers: (ol.Collection|undefined), * layers: (ol.Collection|undefined),
* maxResolution: (number|undefined), * maxResolution: (number|undefined),
* mouseWheelZoom: (boolean|undefined), * mouseWheelZoom: (boolean|undefined),
* mouseWheelZoomDelta: (number|undefined),
* numZoomLevels: (number|undefined), * numZoomLevels: (number|undefined),
* projection: (ol.Projection|string|undefined), * projection: (ol.Projection|string|undefined),
* renderer: (ol.RendererHint|undefined), * renderer: (ol.RendererHint|undefined),
@@ -241,7 +242,10 @@ ol.MapOptions.createInteractions_ = function(mapOptionsLiteral) {
var mouseWheelZoom = goog.isDef(mapOptionsLiteral.mouseWheelZoom) ? var mouseWheelZoom = goog.isDef(mapOptionsLiteral.mouseWheelZoom) ?
mapOptionsLiteral.mouseWheelZoom : true; mapOptionsLiteral.mouseWheelZoom : true;
if (mouseWheelZoom) { 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) ? var shiftDragZoom = goog.isDef(mapOptionsLiteral.shiftDragZoom) ?
+43
View File
@@ -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);
});
});
});
});
}); });