From 661d643b4fb20129e1255136f7f980f330fc086d Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 27 Aug 2007 19:55:04 +0000 Subject: [PATCH] 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 --- lib/OpenLayers/Handler.js | 36 +++++++++++++- tests/Handler/test_Drag.html | 26 +++++----- tests/Handler/test_MouseWheel.html | 25 +++++----- tests/test_Handler.html | 77 +++++++++++++++++++++++------- 4 files changed, 123 insertions(+), 41 deletions(-) diff --git a/lib/OpenLayers/Handler.js b/lib/OpenLayers/Handler.js index c1dfe4cba0..ce2f3883f1 100644 --- a/lib/OpenLayers/Handler.js +++ b/lib/OpenLayers/Handler.js @@ -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; }, /** diff --git a/tests/Handler/test_Drag.html b/tests/Handler/test_Drag.html index 47236b643c..6f45a22ecb 100644 --- a/tests/Handler/test_Drag.html +++ b/tests/Handler/test_Drag.html @@ -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 diff --git a/tests/Handler/test_MouseWheel.html b/tests/Handler/test_MouseWheel.html index a91c58eb16..34b431f49c 100644 --- a/tests/Handler/test_MouseWheel.html +++ b/tests/Handler/test_MouseWheel.html @@ -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 diff --git a/tests/test_Handler.html b/tests/test_Handler.html index d2f6c49e38..7cd12250ef 100644 --- a/tests/test_Handler.html +++ b/tests/test_Handler.html @@ -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');