Add hover handler. With this handler user-defined actions can be triggered as the mouse moves over the map and pauses. An example of use is send WMS/GetFeatureInfo requests. r=crschmidt,tschaub (closes #1255)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@5746 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
136
tests/Handler/test_Hover.html
Normal file
136
tests/Handler/test_Hover.html
Normal file
@@ -0,0 +1,136 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="../../lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript">
|
||||
function test_Handler_Hover_events(t) {
|
||||
t.plan(10);
|
||||
|
||||
var map = new OpenLayers.Map('map');
|
||||
var control = {
|
||||
map: map
|
||||
};
|
||||
map.events.registerPriority = function(type, obj, func) {
|
||||
var r = func();
|
||||
if(typeof r == "string") {
|
||||
// this is one of the mock handler methods
|
||||
t.eq(OpenLayers.Util.indexOf(nonevents, type), -1,
|
||||
"registered method is not one of the events " +
|
||||
"that should not be handled");
|
||||
t.ok(OpenLayers.Util.indexOf(events, type) > -1,
|
||||
"activate calls registerPriority with browser event: " + type);
|
||||
t.eq(typeof func, "function",
|
||||
"activate calls registerPriority with a function");
|
||||
t.eq(func(), type,
|
||||
"activate calls registerPriority with the correct method");
|
||||
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Hover",
|
||||
"activate calls registerPriority with the handler");
|
||||
}
|
||||
}
|
||||
function setMethod(key) {
|
||||
handler[key] = function() {return key};
|
||||
}
|
||||
|
||||
// list below events that should be handled (events) and those
|
||||
// that should not be handled (nonevents) by the handler
|
||||
var events = ["mousemove", "mouseout"];
|
||||
var nonevents = ["mousedown", "mouseup", "click", "dblclick", "resize", "focus", "blur"];
|
||||
var handler = new OpenLayers.Handler.Hover(control);
|
||||
// set browser event like properties on the handler
|
||||
for(var i=0; i<events.length; ++i) {
|
||||
setMethod(events[i]);
|
||||
}
|
||||
handler.activate();
|
||||
}
|
||||
|
||||
function test_Handler_Hover_callbacks(t) {
|
||||
t.plan(8);
|
||||
|
||||
var map = new OpenLayers.Map('map', {controls: []});
|
||||
|
||||
var control = {
|
||||
map: map
|
||||
};
|
||||
|
||||
var timers = {};
|
||||
var sto = window.setTimeout;
|
||||
window.setTimeout = function(func, delay) {
|
||||
var key = Math.random();
|
||||
timers[key] = true;
|
||||
t.ok(typeof func == "function",
|
||||
"setTimeout called with a function");
|
||||
t.eq(delay, handler.delay,
|
||||
"setTimeout called with proper delay");
|
||||
// execute function that is supposed to be delayed
|
||||
func();
|
||||
return key;
|
||||
}
|
||||
var cto = window.clearTimeout;
|
||||
window.clearTimeout = function(key) {
|
||||
if(timers[key] === true) {
|
||||
delete timers[key];
|
||||
} else {
|
||||
t.fail("clearTimeout called with non-existent timerId");
|
||||
}
|
||||
}
|
||||
|
||||
var handler = new OpenLayers.Handler.Hover(control, {});
|
||||
handler.activate();
|
||||
var testEvt;
|
||||
|
||||
// test pause and move callbacks - four tests here (2 from setTimeout above)
|
||||
testEvt = Math.random();
|
||||
handler.callbacks = {
|
||||
"pause": function(evt) {
|
||||
t.eq(evt, testEvt,
|
||||
"pause callback called with correct evt");
|
||||
},
|
||||
"move": function(evt) {
|
||||
t.eq(evt, testEvt,
|
||||
"move callback called with correct evt");
|
||||
}
|
||||
};
|
||||
map.events.triggerEvent("mousemove", testEvt);
|
||||
handler.clearTimer();
|
||||
|
||||
// test pixelTolerance - four tests here (2 from setTimeout above)
|
||||
handler.pixelTolerance = 2;
|
||||
handler.px = new OpenLayers.Pixel(0, 0);
|
||||
testEvt = {
|
||||
xy: new OpenLayers.Pixel(0, 1)
|
||||
};
|
||||
// mouse moves one pixel, callbacks shouldn't be called
|
||||
handler.callbacks = {
|
||||
"pause": function(evt) {
|
||||
t.fail("(pixelTolerance met) pause callback shouldn't be called");
|
||||
},
|
||||
"move": function(evt) {
|
||||
t.fail("(pixelTolerance met) move callback shoudln't be called");
|
||||
}
|
||||
};
|
||||
map.events.triggerEvent("mousemove", testEvt);
|
||||
handler.clearTimer();
|
||||
handler.px = new OpenLayers.Pixel(0, 0);
|
||||
testEvt = {
|
||||
xy: new OpenLayers.Pixel(3, 3)
|
||||
};
|
||||
// mouse moves 3x3 pixels, callbacks should be called
|
||||
handler.callbacks = {
|
||||
"pause": function(evt) {
|
||||
t.ok(evt == testEvt, "(pixelTolerance unmet) pause callback called");
|
||||
},
|
||||
"move": function(evt) {
|
||||
t.ok(evt == testEvt, "(pixelTolerance unmet) move callback called");
|
||||
}
|
||||
};
|
||||
map.events.triggerEvent("mousemove", testEvt);
|
||||
handler.clearTimer();
|
||||
|
||||
window.setTimeout = sto;
|
||||
window.clearTimeout = cto;
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map" style="width: 300px; height: 150px;"/>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user