Ticket #1346: control_events.patch
| File control_events.patch, 9.4 kB (added by tschaub, 1 year ago) |
|---|
-
tests/Control/test_Navigation.html
old new 17 17 } 18 18 19 19 function test_Control_Navigation_destroy (t) { 20 t.plan( 9);20 t.plan(10); 21 21 22 22 var temp = OpenLayers.Control.prototype.destroy; 23 23 OpenLayers.Control.prototype.destroy = function() { … … 26 26 }; 27 27 28 28 var control = { 29 events: { 30 destroy: function() { 31 t.ok(true, "events destroyed"); 32 } 33 }, 29 34 'deactivate': function() { 30 35 t.ok(true, "navigation control deactivated before being destroyed"); 31 36 }, -
lib/OpenLayers/Control.js
old new 104 104 */ 105 105 handler: null, 106 106 107 /** 108 * Property: events 109 * {<OpenLayers.Events>} Events instance for triggering control specific 110 * events. 111 */ 112 events: null, 113 107 114 /** 115 * Constant: EVENT_TYPES 116 * {Array(String)} Supported application event types. Register a listener 117 * for a particular event with the following syntax: 118 * (code) 119 * control.events.register(type, obj, listener); 120 * (end) 121 * 122 * Listeners will be called with a reference to an event object. The 123 * properties of this event depends on exactly what happened. 124 * 125 * All event objects have at least the following properties: 126 * - *object* {Object} A reference to control.events.object (a reference 127 * to the control). 128 * - *element* {DOMElement} A reference to control.events.element (which 129 * will be null unless documented otherwise). 130 * 131 * Supported map event types: 132 * - *activate* Triggered when activated. 133 * - *deactivate* Triggered when deactivated. 134 */ 135 EVENT_TYPES: ["activate", "deactivate"], 136 137 /** 108 138 * Constructor: OpenLayers.Control 109 139 * Create an OpenLayers Control. The options passed as a parameter 110 140 * directly extend the control. For example passing the following: … … 124 154 125 155 OpenLayers.Util.extend(this, options); 126 156 157 this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES); 127 158 this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); 128 159 }, 129 160 … … 134 165 * to prevent memory leaks. 135 166 */ 136 167 destroy: function () { 168 this.events.destroy(); 137 169 // eliminate circular references 138 170 if (this.handler) { 139 171 this.handler.destroy(); … … 232 264 this.handler.activate(); 233 265 } 234 266 this.active = true; 267 this.events.triggerEvent("activate"); 235 268 return true; 236 269 }, 237 270 … … 250 283 this.handler.deactivate(); 251 284 } 252 285 this.active = false; 286 this.events.triggerEvent("deactivate"); 253 287 return true; 254 288 } 255 289 return false; -
lib/OpenLayers/Control/NavigationHistory.js
old new 80 80 clearOnDeactivate: false, 81 81 82 82 /** 83 * Property: events84 * {<OpenLayers.Events>} An events object that will be used for registering85 * listeners. Defaults to the map events for this control.86 */87 events: null,88 89 /**90 83 * Property: registry 91 84 * {Object} An object with keys corresponding to event types. Values 92 85 * are functions that return an object representing the current state. … … 144 137 145 138 var previousOptions = { 146 139 trigger: OpenLayers.Function.bind(this.previousTrigger, this), 147 displayClass: this.displayClass + "Previous", 148 onActivate: function() {}, 149 onDeactivate: function() {} 140 displayClass: this.displayClass + "Previous" 150 141 }; 151 142 if(options) { 152 143 OpenLayers.Util.extend(previousOptions, options.previousOptions); … … 155 146 156 147 var nextOptions = { 157 148 trigger: OpenLayers.Function.bind(this.nextTrigger, this), 158 displayClass: this.displayClass + "Next", 159 onActivate: function() {}, 160 onDeactivate: function() {} 149 displayClass: this.displayClass + "Next" 161 150 }; 162 151 if(options) { 163 152 OpenLayers.Util.extend(nextOptions, options.nextOptions); … … 179 168 onPreviousChange: function(state, length) { 180 169 if(state && !this.previous.active) { 181 170 this.previous.activate(); 182 this.previous.onActivate();183 171 } else if(!state && this.previous.active) { 184 172 this.previous.deactivate(); 185 this.previous.onDeactivate();186 173 } 187 174 }, 188 175 … … 199 186 onNextChange: function(state, length) { 200 187 if(state && !this.next.active) { 201 188 this.next.activate(); 202 this.next.onActivate();203 189 } else if(!state && this.next.active) { 204 190 this.next.deactivate(); 205 this.next.onDeactivate();206 191 } 207 192 }, 208 193 … … 364 349 if(this.listeners == null) { 365 350 this.setListeners(); 366 351 } 367 if(!this.events) {368 this.events = this.map.events;369 }370 352 for(var type in this.listeners) { 371 this. events.register(type, this, this.listeners[type]);353 this.map.events.register(type, this, this.listeners[type]); 372 354 } 373 355 activated = true; 374 356 if(this.previousStack.length == 0) { … … 402 384 if(this.map) { 403 385 if(OpenLayers.Control.prototype.deactivate.apply(this)) { 404 386 for(var type in this.listeners) { 405 this. events.unregister(387 this.map.events.unregister( 406 388 type, this, this.listeners[type] 407 389 ); 408 390 } -
lib/OpenLayers/Control/Panel.js
old new 45 45 destroy: function() { 46 46 OpenLayers.Control.prototype.destroy.apply(this, arguments); 47 47 for(var i = this.controls.length - 1 ; i >= 0; i--) { 48 this.controls[i].events.un({ 49 "activate": this.redraw, 50 "deactivate": this.redraw, 51 scope: this 52 }); 48 53 OpenLayers.Event.stopObservingElement(this.controls[i].panel_div); 49 54 this.controls[i].panel_div = null; 50 55 } … … 75 80 for(var i = 0; i < this.controls.length; i++) { 76 81 this.controls[i].deactivate(); 77 82 } 78 this.redraw();79 83 return true; 80 84 } else { 81 85 return false; … … 93 97 for (var i = 0; i < this.controls.length; i++) { 94 98 this.map.addControl(this.controls[i]); 95 99 this.controls[i].deactivate(); 100 this.controls[i].events.on({ 101 "activate": this.redraw, 102 "deactivate": this.redraw, 103 scope: this 104 }); 96 105 } 97 106 this.activate(); 98 107 return this.div; … … 145 154 } 146 155 } 147 156 } 148 this.redraw();149 157 }, 150 158 151 159 /** … … 185 193 for (var i = 0; i < controls.length; i++) { 186 194 this.map.addControl(controls[i]); 187 195 controls[i].deactivate(); 196 controls[i].events.on({ 197 "activate": this.redraw, 198 "deactivate": this.redraw, 199 scope: this 200 }); 188 201 } 189 202 this.redraw(); 190 203 } -
examples/navigation-history.html
old new 36 36 function init(){ 37 37 map = new OpenLayers.Map('map'); 38 38 39 // set any application specific behavior here 40 // this will become unnecessary when controls have better event handling 41 var previousOptions = { 42 onActivate: function() {panel.redraw();}, 43 onDeactivate: function() {panel.redraw();} 44 }; 45 var nextOptions = { 46 onActivate: function() {panel.redraw();}, 47 onDeactivate: function() {panel.redraw();} 48 }; 49 var options = { 50 previousOptions: previousOptions, 51 nextOptions: nextOptions 52 }; 53 nav = new OpenLayers.Control.NavigationHistory(options); 39 nav = new OpenLayers.Control.NavigationHistory(); 40 // parent control must be added to the map 54 41 map.addControl(nav); 55 42 56 43 panel = new OpenLayers.Control.Panel(
