Give handlers a non-API evt property - this to be used by other controls in the library only. Eventually, we may decide to restructure this. (Closes #902)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@4062 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -72,6 +72,16 @@ OpenLayers.Handler = OpenLayers.Class({
|
||||
* {Boolean}
|
||||
*/
|
||||
active: false,
|
||||
|
||||
/**
|
||||
* Property: evt
|
||||
* {Event} This property references the last event handled by the handler.
|
||||
* Note that this property is not part of the stable API. Use of the
|
||||
* evt property should be restricted to controls in the library
|
||||
* or other applications that are willing to update with changes to
|
||||
* the OpenLayers code.
|
||||
*/
|
||||
evt: null,
|
||||
|
||||
/**
|
||||
* Constructor: OpenLayers.Handler
|
||||
@@ -198,7 +208,8 @@ OpenLayers.Handler = OpenLayers.Class({
|
||||
*/
|
||||
register: function (name, method) {
|
||||
// TODO: deal with registerPriority in 3.0
|
||||
this.map.events.registerPriority(name, this, method);
|
||||
this.map.events.registerPriority(name, this, method);
|
||||
this.map.events.registerPriority(name, this, this.setEvent);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -207,6 +218,29 @@ OpenLayers.Handler = OpenLayers.Class({
|
||||
*/
|
||||
unregister: function (name, method) {
|
||||
this.map.events.unregister(name, this, method);
|
||||
this.map.events.unregister(name, this, this.setEvent);
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: setEvent
|
||||
* With each registered browser event, the handler sets its own evt
|
||||
* property. This property can be accessed by controls if needed
|
||||
* to get more information about the event that the handler is
|
||||
* processing.
|
||||
*
|
||||
* This allows modifier keys on the event to be checked (alt, shift,
|
||||
* and ctrl cannot be checked with the keyboard handler). For a
|
||||
* control to determine which modifier keys are associated with the
|
||||
* event that a handler is currently processing, it should access
|
||||
* (code)handler.evt.altKey || handler.evt.shiftKey ||
|
||||
* handler.evt.ctrlKey(end).
|
||||
*
|
||||
* Parameters:
|
||||
* evt - {Event} The browser event.
|
||||
*/
|
||||
setEvent: function(evt) {
|
||||
this.evt = evt;
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -57,17 +57,21 @@
|
||||
var events = ["mousedown", "mouseup", "mousemove", "mouseout", "click"];
|
||||
var nonevents = ["dblclick", "resize", "focus", "blur"];
|
||||
map.events.registerPriority = function(type, obj, func) {
|
||||
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.Drag",
|
||||
"activate calls registerPriority with the handler");
|
||||
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.Drag",
|
||||
"activate calls registerPriority with the handler");
|
||||
}
|
||||
}
|
||||
|
||||
// set browser event like properties on the handler
|
||||
|
||||
@@ -72,17 +72,20 @@
|
||||
var events = ["mousemove"];
|
||||
var nonevents = ["dblclick", "resize", "focus", "blur"];
|
||||
map.events.registerPriority = function(type, obj, func) {
|
||||
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.MouseWheel",
|
||||
"activate calls registerPriority with the handler");
|
||||
var r = func();
|
||||
if(typeof r == "string") {
|
||||
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.MouseWheel",
|
||||
"activate calls registerPriority with the handler");
|
||||
}
|
||||
}
|
||||
|
||||
// set browser event like properties on the handler
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
}
|
||||
|
||||
function test_Handler_activate(t) {
|
||||
t.plan(42);
|
||||
t.plan(52);
|
||||
var map = new OpenLayers.Map('map');
|
||||
var control = new OpenLayers.Control();
|
||||
map.addControl(control);
|
||||
@@ -39,14 +39,21 @@
|
||||
|
||||
handler.active = false;
|
||||
map.events.registerPriority = function(type, obj, func) {
|
||||
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",
|
||||
"activate calls registerPriority with the handler");
|
||||
var r = func();
|
||||
if(typeof r == "string") {
|
||||
// this is one of the mock handler methods
|
||||
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(r, type,
|
||||
"activate calls registerPriority with the correct method");
|
||||
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
|
||||
"activate calls registerPriority with the handler");
|
||||
} else {
|
||||
// this is the call with handler.setEvent as the func
|
||||
t.ok(r, "activate calls registerPriority with handler.setEvent");
|
||||
}
|
||||
}
|
||||
|
||||
// set browser event like properties on the handler
|
||||
@@ -63,7 +70,7 @@
|
||||
}
|
||||
|
||||
function test_Handler_deactivate(t) {
|
||||
t.plan(42);
|
||||
t.plan(52);
|
||||
var map = new OpenLayers.Map('map');
|
||||
var control = new OpenLayers.Control();
|
||||
map.addControl(control);
|
||||
@@ -80,14 +87,21 @@
|
||||
|
||||
handler.activate();
|
||||
map.events.unregister = function(type, obj, func) {
|
||||
t.ok(OpenLayers.Util.indexOf(events, type) > -1,
|
||||
"deactivate calls unregister with browser event: " + type);
|
||||
t.eq(typeof func, "function",
|
||||
"activate calls unregister with a function");
|
||||
t.eq(func(), type,
|
||||
"activate calls unregister with the correct method");
|
||||
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
|
||||
"activate calls unregister with the handler");
|
||||
var r = func();
|
||||
if(typeof r == "string") {
|
||||
// this is one of the mock handler methods
|
||||
t.ok(OpenLayers.Util.indexOf(events, type) > -1,
|
||||
"deactivate calls unregister with browser event: " + type);
|
||||
t.eq(typeof func, "function",
|
||||
"activate calls unregister with a function");
|
||||
t.eq(func(), type,
|
||||
"activate calls unregister with the correct method");
|
||||
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
|
||||
"activate calls unregister with the handler");
|
||||
} else {
|
||||
// this is the call with handler.setEvent as the func
|
||||
t.ok(r, "activate calls registerPriority with handler.setEvent");
|
||||
}
|
||||
}
|
||||
|
||||
// set browser event like properties on the handler
|
||||
@@ -103,6 +117,33 @@
|
||||
|
||||
}
|
||||
|
||||
function test_Handler_setEvent(t) {
|
||||
t.plan(4);
|
||||
var map = new OpenLayers.Map('map');
|
||||
var control = new OpenLayers.Control();
|
||||
map.addControl(control);
|
||||
var handler = new OpenLayers.Handler(control);
|
||||
handler.click = function(evt) {
|
||||
}
|
||||
handler.activate();
|
||||
var testEvent = {
|
||||
xy: new OpenLayers.Pixel(Math.random(), Math.random()),
|
||||
altKey: (Math.random() > 0.5),
|
||||
shiftKey: (Math.random() > 0.5),
|
||||
ctrlKey: (Math.random() > 0.5)
|
||||
}
|
||||
map.events.triggerEvent("click", testEvent);
|
||||
t.ok(handler.evt.xy.x == testEvent.xy.x &&
|
||||
handler.evt.xy.y == testEvent.xy.y,
|
||||
"handler.evt has proper xy object");
|
||||
t.eq(handler.evt.altKey, testEvent.altKey,
|
||||
"handler.evt.altKey correct");
|
||||
t.eq(handler.evt.shiftKey, testEvent.shiftKey,
|
||||
"handler.evt.shiftKey correct");
|
||||
t.eq(handler.evt.ctrlKey, testEvent.ctrlKey,
|
||||
"handler.evt.ctrlKey correct");
|
||||
}
|
||||
|
||||
function test_Handler_destroy(t) {
|
||||
t.plan(5);
|
||||
var map = new OpenLayers.Map('map');
|
||||
|
||||
Reference in New Issue
Block a user