Add a cumulative mode in MouseWheel handler. r=ahocevar (closes #2450)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10002 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Frédéric Junod
2010-02-01 08:42:17 +00:00
parent 9b728d048d
commit 6223d7fd82
3 changed files with 56 additions and 3 deletions
+14
View File
@@ -6,6 +6,13 @@
<script src="../lib/OpenLayers.js"></script> <script src="../lib/OpenLayers.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var map, layer; var map, layer;
function setCumulative() {
var nav = map.getControlsByClass("OpenLayers.Control.Navigation")[0];
var cumulative = document.getElementById("cumulative");
nav.handlers.wheel.cumulative = cumulative.checked;
}
function init(){ function init(){
map = new OpenLayers.Map( 'map', {controls: [ map = new OpenLayers.Map( 'map', {controls: [
new OpenLayers.Control.Navigation( new OpenLayers.Control.Navigation(
@@ -38,6 +45,13 @@
spin the mousewheel, no request will be sent to the server. Instead, spin the mousewheel, no request will be sent to the server. Instead,
the zoomlevel delta will be recorded. After a delay (in this example the zoomlevel delta will be recorded. After a delay (in this example
100ms), a zoom action with the cumulated delta will be performed.</p> 100ms), a zoom action with the cumulated delta will be performed.</p>
<div>
<input id="cumulative" type="checkbox" checked="checked"
onchange="setCumulative()"/>
<label for="cumulative">Cumulative mode. If this mode is deactivated,
only one zoom event will be performed after the delay.</label>
</div>
</div> </div>
</body> </body>
</html> </html>
+11 -2
View File
@@ -42,9 +42,18 @@ OpenLayers.Handler.MouseWheel = OpenLayers.Class(OpenLayers.Handler, {
* Property: delta * Property: delta
* {Integer} When interval is set, delta collects the mousewheel z-deltas * {Integer} When interval is set, delta collects the mousewheel z-deltas
* of the events that occur within the interval. * of the events that occur within the interval.
* See also the cumulative option
*/ */
delta: 0, delta: 0,
/**
* Property: cumulative
* {Boolean} When interval is set: true to collect all the mousewheel
* z-deltas, false to only record the delta direction (positive or
* negative)
*/
cumulative: true,
/** /**
* Constructor: OpenLayers.Handler.MouseWheel * Constructor: OpenLayers.Handler.MouseWheel
* *
@@ -216,9 +225,9 @@ OpenLayers.Handler.MouseWheel = OpenLayers.Class(OpenLayers.Handler, {
); );
} }
if (delta < 0) { if (delta < 0) {
this.callback("down", [e, delta]); this.callback("down", [e, this.cumulative ? delta : -1]);
} else { } else {
this.callback("up", [e, delta]); this.callback("up", [e, this.cumulative ? delta : 1]);
} }
} }
}, },
+30
View File
@@ -114,8 +114,38 @@
t.delay_call(1, function() { t.delay_call(1, function() {
t.eq(deltaZ, 2, "Multiple scroll actions triggered one event when interval is set"); t.eq(deltaZ, 2, "Multiple scroll actions triggered one event when interval is set");
}); });
}
function test_Handler_MouseWheel_cumulative(t) {
t.plan(1);
var deltaUp = 0;
var callbacks = {
up: function(evt, delta) {
deltaUp += delta;
}
};
var map = new OpenLayers.Map('map');
map.addLayer(new OpenLayers.Layer.WMS("","",{}));
map.zoomToMaxExtent();
var control = new OpenLayers.Control({});
map.addControl(control);
var handler = new OpenLayers.Handler.MouseWheel(control, callbacks, {
interval: 150,
cumulative: false
});
var delta = 120;
// generate 20 scroll up in non cumulative mode
if (window.opera && window.opera.version() < 9.2) delta = -delta;
for (var i=0; i < 20; i++) {
handler.onWheelEvent({'target':map.layers[0].div, wheelDelta: delta});
}
t.delay_call(1, function() {
t.eq(deltaUp, 1, "Non cumulative mode works");
});
} }
function test_Handler_MouseWheel_deactivate(t) { function test_Handler_MouseWheel_deactivate(t) {