Ticket #902: handler.evt.patch
| File handler.evt.patch, 8.4 kB (added by tschaub, 1 year ago) |
|---|
-
lib/OpenLayers/Handler.js
old new 198 198 */ 199 199 register: function (name, method) { 200 200 // TODO: deal with registerPriority in 3.0 201 this.map.events.registerPriority(name, this, method); 201 this.map.events.registerPriority(name, this, method); 202 this.map.events.registerPriority(name, this, this.setEvent); 202 203 }, 203 204 204 205 /** … … 207 208 */ 208 209 unregister: function (name, method) { 209 210 this.map.events.unregister(name, this, method); 211 this.map.events.unregister(name, this, this.setEvent); 210 212 }, 213 214 /** 215 * Method: setEvent 216 * With each registered browser event, the handler sets its own evt 217 * property. This property can be accessed by controls if needed 218 * to get more information about the event that the handler is 219 * processing. 220 * 221 * This allows modifier keys on the event to be checked (alt, shift, 222 * and ctrl cannot be checked with the keyboard handler). For a 223 * control to determine which modifier keys are associated with the 224 * event that a handler is currently processing, it should access 225 * (code)handler.evt.altKey || handler.evt.shiftKey || 226 * handler.evt.ctrlKey(end). 227 * 228 * Parameters: 229 * evt - {Event} The browser event. 230 */ 231 setEvent: function(evt) { 232 this.evt = evt; 233 return true; 234 }, 211 235 212 236 /** 213 237 * Method: destroy -
tests/Handler/test_Drag.html
old new 57 57 var events = ["mousedown", "mouseup", "mousemove", "mouseout", "click"]; 58 58 var nonevents = ["dblclick", "resize", "focus", "blur"]; 59 59 map.events.registerPriority = function(type, obj, func) { 60 t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, 61 "registered method is not one of the events " + 62 "that should not be handled"); 63 t.ok(OpenLayers.Util.indexOf(events, type) > -1, 64 "activate calls registerPriority with browser event: " + type); 65 t.eq(typeof func, "function", 66 "activate calls registerPriority with a function"); 67 t.eq(func(), type, 68 "activate calls registerPriority with the correct method"); 69 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Drag", 70 "activate calls registerPriority with the handler"); 60 var r = func(); 61 if(typeof r == "string") { 62 // this is one of the mock handler methods 63 t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, 64 "registered method is not one of the events " + 65 "that should not be handled"); 66 t.ok(OpenLayers.Util.indexOf(events, type) > -1, 67 "activate calls registerPriority with browser event: " + type); 68 t.eq(typeof func, "function", 69 "activate calls registerPriority with a function"); 70 t.eq(func(), type, 71 "activate calls registerPriority with the correct method"); 72 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Drag", 73 "activate calls registerPriority with the handler"); 74 } 71 75 } 72 76 73 77 // set browser event like properties on the handler -
tests/test_Handler.html
old new 22 22 } 23 23 24 24 function test_Handler_activate(t) { 25 t.plan( 42);25 t.plan(52); 26 26 var map = new OpenLayers.Map('map'); 27 27 var control = new OpenLayers.Control(); 28 28 map.addControl(control); … … 39 39 40 40 handler.active = false; 41 41 map.events.registerPriority = function(type, obj, func) { 42 t.ok(OpenLayers.Util.indexOf(events, type) > -1, 43 "activate calls registerPriority with browser event: " + type); 44 t.eq(typeof func, "function", 45 "activate calls registerPriority with a function"); 46 t.eq(func(), type, 47 "activate calls registerPriority with the correct method"); 48 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 49 "activate calls registerPriority with the handler"); 42 var r = func(); 43 if(typeof r == "string") { 44 // this is one of the mock handler methods 45 t.ok(OpenLayers.Util.indexOf(events, type) > -1, 46 "activate calls registerPriority with browser event: " + type); 47 t.eq(typeof func, "function", 48 "activate calls registerPriority with a function"); 49 t.eq(r, type, 50 "activate calls registerPriority with the correct method"); 51 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 52 "activate calls registerPriority with the handler"); 53 } else { 54 // this is the call with handler.setEvent as the func 55 t.ok(r, "activate calls registerPriority with handler.setEvent"); 56 } 50 57 } 51 58 52 59 // set browser event like properties on the handler … … 63 70 } 64 71 65 72 function test_Handler_deactivate(t) { 66 t.plan( 42);73 t.plan(52); 67 74 var map = new OpenLayers.Map('map'); 68 75 var control = new OpenLayers.Control(); 69 76 map.addControl(control); … … 80 87 81 88 handler.activate(); 82 89 map.events.unregister = function(type, obj, func) { 83 t.ok(OpenLayers.Util.indexOf(events, type) > -1, 84 "deactivate calls unregister with browser event: " + type); 85 t.eq(typeof func, "function", 86 "activate calls unregister with a function"); 87 t.eq(func(), type, 88 "activate calls unregister with the correct method"); 89 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 90 "activate calls unregister with the handler"); 90 var r = func(); 91 if(typeof r == "string") { 92 // this is one of the mock handler methods 93 t.ok(OpenLayers.Util.indexOf(events, type) > -1, 94 "deactivate calls unregister with browser event: " + type); 95 t.eq(typeof func, "function", 96 "activate calls unregister with a function"); 97 t.eq(func(), type, 98 "activate calls unregister with the correct method"); 99 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 100 "activate calls unregister with the handler"); 101 } else { 102 // this is the call with handler.setEvent as the func 103 t.ok(r, "activate calls registerPriority with handler.setEvent"); 104 } 91 105 } 92 106 93 107 // set browser event like properties on the handler … … 103 117 104 118 } 105 119 120 function test_Handler_setEvent(t) { 121 t.plan(4); 122 var map = new OpenLayers.Map('map'); 123 var control = new OpenLayers.Control(); 124 map.addControl(control); 125 var handler = new OpenLayers.Handler(control); 126 handler.click = function(evt) { 127 } 128 handler.activate(); 129 var testEvent = { 130 xy: new OpenLayers.Pixel(Math.random(), Math.random()), 131 altKey: (Math.random() > 0.5), 132 shiftKey: (Math.random() > 0.5), 133 ctrlKey: (Math.random() > 0.5) 134 } 135 map.events.triggerEvent("click", testEvent); 136 t.ok(handler.evt.xy.x == testEvent.xy.x && 137 handler.evt.xy.y == testEvent.xy.y, 138 "handler.evt has proper xy object"); 139 t.eq(handler.evt.altKey, testEvent.altKey, 140 "handler.evt.altKey correct"); 141 t.eq(handler.evt.shiftKey, testEvent.shiftKey, 142 "handler.evt.shiftKey correct"); 143 t.eq(handler.evt.ctrlKey, testEvent.ctrlKey, 144 "handler.evt.ctrlKey correct"); 145 } 146 106 147 function test_Handler_destroy(t) { 107 148 t.plan(4); 108 149 var map = new OpenLayers.Map('map');
