Ticket #1404: eventListeners.patch
| File eventListeners.patch, 13.6 kB (added by tschaub, 10 months ago) |
|---|
-
tests/test_Map.html
old new 84 84 t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hashtable" ); 85 85 t.eq( map.theme, 'foo', "map theme set correctly." ); 86 86 } 87 88 function test_eventListeners(t) { 89 t.plan(1); 90 91 var method = OpenLayers.Events.prototype.on; 92 // test that events.on is called at map construction 93 var options = { 94 eventListeners: {foo: "bar"}, 95 controls: [] 96 }; 97 OpenLayers.Events.prototype.on = function(obj) { 98 t.eq(obj, options.eventListeners, "events.on called with eventListeners"); 99 } 100 var map = new OpenLayers.Map('map', options); 101 OpenLayers.Events.prototype.on = method; 102 map.destroy(); 87 103 104 // if events.on is called again, this will fail due to an extra test 105 // test map without eventListeners 106 OpenLayers.Events.prototype.on = function(obj) { 107 t.fail("events.on called without eventListeners"); 108 } 109 var map2 = new OpenLayers.Map("map", {controls: []}); 110 OpenLayers.Events.prototype.on = method; 111 map2.destroy(); 112 } 113 88 114 function test_05_Map_center(t) { 89 115 t.plan(4); 90 116 map = new OpenLayers.Map('map'); -
tests/test_Control.html
old new 29 29 t.eq( control.title, titleText, "control.title set correctly" ); 30 30 } 31 31 32 function test_eventListeners(t) { 33 t.plan(1); 34 35 var method = OpenLayers.Events.prototype.on; 36 // test that events.on is called at control construction 37 var options = { 38 eventListeners: {foo: "bar"} 39 }; 40 OpenLayers.Events.prototype.on = function(obj) { 41 t.eq(obj, options.eventListeners, "events.on called with eventListeners"); 42 } 43 var control = new OpenLayers.Control(options); 44 OpenLayers.Events.prototype.on = method; 45 control.destroy(); 46 47 // if events.on is called again, this will fail due to an extra test 48 // test control without eventListeners 49 OpenLayers.Events.prototype.on = function(obj) { 50 t.fail("events.on called without eventListeners"); 51 } 52 var control2 = new OpenLayers.Control(); 53 OpenLayers.Events.prototype.on = method; 54 control2.destroy(); 55 } 56 32 57 function test_Control_destroy(t) { 33 58 t.plan(3); 34 59 -
tests/test_Layer.html
old new 119 119 t.eq(layer.numZoomLevels, numZoomLevels, "numZoomLevels set correctly"); 120 120 } 121 121 122 function test_eventListeners(t) { 123 t.plan(1); 124 125 var method = OpenLayers.Events.prototype.on; 126 // test that events.on is called at layer construction 127 var options = { 128 eventListeners: {foo: "bar"} 129 }; 130 OpenLayers.Events.prototype.on = function(obj) { 131 t.eq(obj, options.eventListeners, "events.on called with eventListeners"); 132 } 133 var layer = new OpenLayers.Layer("test", options); 134 OpenLayers.Events.prototype.on = method; 135 layer.destroy(); 136 137 // if events.on is called again, this will fail due to an extra test 138 // test layer without eventListeners 139 OpenLayers.Events.prototype.on = function(obj) { 140 t.fail("events.on called without eventListeners"); 141 } 142 var layer2 = new OpenLayers.Layer("test"); 143 OpenLayers.Events.prototype.on = method; 144 layer2.destroy(); 145 } 146 122 147 function test_Layer_initResolutions(t) { 123 148 t.plan(12); 124 149 var map = new OpenLayers.Map("map"); -
lib/OpenLayers/Map.js
old new 326 326 panTween: null, 327 327 328 328 /** 329 * APIProperty: eventListeners 330 * {Object} If set as an option at construction, the eventListeners 331 * object will be registered with <OpenLayers.Events.on>. Object 332 * structure must be a listeners object as shown in the example for 333 * the events.on method. 334 */ 335 eventListeners: null, 336 337 /** 329 338 * Property: panMethod 330 339 * {Function} The Easing function to be used for tweening. Default is 331 340 * OpenLayers.Easing.Expo.easeOut. Setting this to 'null' turns off … … 397 406 this.EVENT_TYPES, 398 407 this.fallThrough); 399 408 this.updateSize(); 409 if(this.eventListeners instanceof Object) { 410 this.events.on(this.eventListeners); 411 } 400 412 401 413 // update the map size and location before the map moves 402 414 this.events.register("movestart", this, this.updateSize); -
lib/OpenLayers/Control.js
old new 104 104 */ 105 105 handler: null, 106 106 107 /** 108 * APIProperty: eventListeners 109 * {Object} If set as an option at construction, the eventListeners 110 * object will be registered with <OpenLayers.Events.on>. Object 111 * structure must be a listeners object as shown in the example for 112 * the events.on method. 113 */ 114 eventListeners: null, 115 107 116 /** 108 117 * Property: events 109 118 * {<OpenLayers.Events>} Events instance for triggering control specific … … 155 164 OpenLayers.Util.extend(this, options); 156 165 157 166 this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES); 167 if(this.eventListeners instanceof Object) { 168 this.events.on(this.eventListeners); 169 } 158 170 this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); 159 171 }, 160 172 … … 230 242 if (px != null) { 231 243 this.position = px.clone(); 232 244 } 233 this.moveTo(this.position); 245 this.moveTo(this.position); 234 246 return this.div; 235 247 }, 236 248 -
lib/OpenLayers/Events.js
old new 429 429 if (this.element != null) { 430 430 this.attachToElement(element); 431 431 } 432 432 433 }, 433 434 434 435 /** … … 651 652 } 652 653 evt.object = this.object; 653 654 evt.element = this.element; 655 if(!evt.type) { 656 evt.type = type; 657 } 654 658 655 659 // execute all callbacks registered for specified type 656 660 // get a clone of the listeners array to -
lib/OpenLayers/Layer.js
old new 139 139 options: null, 140 140 141 141 /** 142 * APIProperty: eventListeners 143 * {Object} If set as an option at construction, the eventListeners 144 * object will be registered with <OpenLayers.Events.on>. Object 145 * structure must be a listeners object as shown in the example for 146 * the events.on method. 147 */ 148 eventListeners: null, 149 150 /** 142 151 * APIProperty: gutter 143 152 * {Integer} Determines the width (in pixels) of the gutter around image 144 153 * tiles to ignore. By setting this property to a non-zero value, … … 272 281 273 282 this.events = new OpenLayers.Events(this, this.div, 274 283 this.EVENT_TYPES); 284 if(this.eventListeners instanceof Object) { 285 this.events.on(this.eventListeners); 286 } 287 275 288 } 276 289 277 290 if (this.wrapDateLine) { -
examples/events.html
old new 1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <title>OpenLayers Event Handling</title> 4 <style type="text/css"> 5 #map { 6 width: 512px; 7 height: 256px; 8 border: 1px solid gray; 9 } 10 #panel { 11 margin: 5px; 12 height: 30px; 13 width: 200px; 14 } 15 #panel div { 16 float: left; 17 margin-left: 5px; 18 width: 25px; 19 height: 25px; 20 border: 1px solid gray; 21 } 22 #output { 23 position: absolute; 24 left: 550px; 25 top: 40px; 26 width: 350px; 27 height: 400px; 28 } 29 div.blueItemInactive { 30 background-color: #aac; 31 } 32 div.blueItemActive { 33 background-color: #33c; 34 } 35 div.orangeItemInactive { 36 background-color: #ca6; 37 } 38 div.orangeItemActive { 39 background-color: #ea0; 40 } 41 div.greenItemInactive { 42 background-color: #aca; 43 } 44 div.greenItemActive { 45 background-color: #3c3; 46 } 47 48 </style> 49 <script src="../lib/OpenLayers.js"></script> 50 <script type="text/javascript"> 51 var map, panel; 52 53 function init(){ 54 55 // define custom map event listeners 56 function mapEvent(event) { 57 log(event.type); 58 } 59 function mapBaseLayerChanged(event) { 60 log(event.type + " " + event.layer.name); 61 } 62 function mapLayerChanged(event) { 63 log(event.type + " " + event.layer.name + " " + event.property); 64 } 65 map = new OpenLayers.Map('map', { 66 eventListeners: { 67 "moveend": mapEvent, 68 "zoomend": mapEvent, 69 "changelayer": mapLayerChanged, 70 "changebaselayer": mapBaseLayerChanged 71 } 72 }); 73 74 panel = new OpenLayers.Control.Panel( 75 {div: document.getElementById("panel")} 76 ); 77 78 // define custom event listeners 79 function toolActivate(event) { 80 log("activate " + event.object.displayClass); 81 } 82 function toolDeactivate(event) { 83 log("deactivate " + event.object.displayClass); 84 } 85 86 // Multiple objects can share listeners with the same scope 87 var toolListeners = { 88 "activate": toolActivate, 89 "deactivate": toolDeactivate 90 }; 91 var blue = new OpenLayers.Control({ 92 type: OpenLayers.Control.TYPE_TOGGLE, 93 eventListeners: toolListeners, 94 displayClass: "blue" 95 }); 96 var orange = new OpenLayers.Control({ 97 type: OpenLayers.Control.TYPE_TOGGLE, 98 eventListeners: toolListeners, 99 displayClass: "orange" 100 }); 101 var green = new OpenLayers.Control({ 102 type: OpenLayers.Control.TYPE_TOGGLE, 103 eventListeners: toolListeners, 104 displayClass: "green" 105 }); 106 107 // add buttons to a panel 108 panel.addControls([blue, orange, green]); 109 map.addControl(panel); 110 111 var vmap = new OpenLayers.Layer.WMS( 112 "OpenLayers WMS", 113 "http://labs.metacarta.com/wms/vmap0", 114 {layers: 'basic'} 115 ); 116 var landsat = new OpenLayers.Layer.WMS( 117 "NASA Global Mosaic", 118 "http://t1.hypercube.telascience.org/cgi-bin/landsat7", 119 {layers: "landsat7"} 120 ); 121 var nexrad = new OpenLayers.Layer.WMS( 122 "Nexrad", 123 "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi", 124 {layers:"nexrad-n0r-wmst", transparent: "TRUE", format: 'image/png'}, 125 {isBaseLayer: false} 126 ); 127 128 129 map.addLayers([vmap, landsat, nexrad]); 130 map.addControl(new OpenLayers.Control.LayerSwitcher()); 131 map.zoomToMaxExtent(); 132 133 } 134 function log(msg) { 135 document.getElementById("output").innerHTML += msg + "\n"; 136 } 137 </script> 138 </head> 139 <body onload="init()"> 140 <h1 id="title">Event Handling</h1> 141 142 <div id="tags"> 143 </div> 144 145 <p id="shortdesc"> 146 Demonstrating various styles of event handling in OpenLayers. 147 </p> 148 149 <div id="map"></div> 150 <div id="panel"></div> 151 <textarea id="output"></textarea> 152 <div id="docs"></div> 153 </body> 154 </html>
