Layer.Vector.removeMap must deactivate the strategies, r=fredj (closes #1649)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@7708 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2008-08-05 13:59:12 +00:00
parent 39201a0427
commit d7ab2c0f60
6 changed files with 140 additions and 13 deletions

View File

@@ -229,13 +229,19 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
*/
destroy: function() {
if (this.strategies) {
for(var i=0, len=this.strategies.length; i<len; i++) {
this.strategies[i].destroy();
var strategy, i, len;
for(i=0, len=this.strategies.length; i<len; i++) {
strategy = this.strategies[i];
if(strategy.autoDestroy) {
strategy.destroy();
}
}
this.strategies = null;
}
if (this.protocol) {
this.protocol.destroy();
if(this.protocol.autoDestroy) {
this.protocol.destroy();
}
this.protocol = null;
}
this.destroyFeatures();
@@ -296,8 +302,31 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
this.renderer.setSize(this.map.getSize());
}
if(this.strategies) {
for(var i=0, len=this.strategies.length; i<len; i++) {
this.strategies[i].activate();
var strategy, i, len;
for(i=0, len=this.strategies.length; i<len; i++) {
strategy = this.strategies[i];
if(strategy.autoActivate) {
strategy.activate();
}
}
}
},
/**
* Method: removeMap
* The layer has been removed from the map.
*
* Parameters:
* map - {<OpenLayers.Map>}
*/
removeMap: function(map) {
if(this.strategies) {
var strategy, i, len;
for(i=0, len=this.strategies.length; i<len; i++) {
strategy = this.strategies[i];
if(strategy.autoActivate) {
strategy.deactivate();
}
}
}
},

View File

@@ -20,7 +20,15 @@ OpenLayers.Protocol = OpenLayers.Class({
* Any options sent to the constructor.
*/
options: null,
/**
* Property: autoDestroy
* {Boolean} The creator of the protocol can set autoDestroy to false
* to fully control when the protocol is destroyed. Defaults to
* true.
*/
autoDestroy: true,
/**
* Constructor: OpenLayers.Protocol
* Abstract class for vector protocols. Create instances of a subclass.

View File

@@ -21,6 +21,28 @@ OpenLayers.Strategy = OpenLayers.Class({
*/
options: null,
/**
* Property: active
* {Boolean} The control is active.
*/
active: null,
/**
* Property: autoActivate
* {Boolean} The creator of the strategy can set autoActivate to false
* to fully control when the protocol is activated and deactivated.
* Defaults to true.
*/
autoActivate: true,
/**
* Property: autoDestroy
* {Boolean} The creator of the strategy can set autoDestroy to false
* to fully control when the strategy is destroyed. Defaults to
* true.
*/
autoDestroy: true,
/**
* Constructor: OpenLayers.Strategy
* Abstract class for vector strategies. Create instances of a subclass.
@@ -32,6 +54,8 @@ OpenLayers.Strategy = OpenLayers.Class({
initialize: function(options) {
OpenLayers.Util.extend(this, options);
this.options = options;
// set the active property here, so that user cannot override it
this.active = false;
},
/**
@@ -57,16 +81,34 @@ OpenLayers.Strategy = OpenLayers.Class({
/**
* Method: activate
* Activate the strategy. Register any listeners, do appropriate setup.
*
* Returns:
* {Boolean} True if the strategy was successfully activated or false if
* the strategy was already active.
*/
activate: function() {
if (!this.active) {
this.active = true;
return true;
}
return false;
},
/**
* Method: deactivate
* Deactivate the strategy. Unregister any listeners, do appropriate
* tear-down.
*
* Returns:
* {Boolean} True if the strategy was successfully deactivated or false if
* the strategy was already inactive.
*/
deactivate: function() {
if (this.active) {
this.active = false;
return true;
}
return false;
},
CLASS_NAME: "OpenLayers.Strategy"

View File

@@ -39,13 +39,20 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
* Method: activate
* Activate the strategy: reads all features from the protocol and add them
* to the layer.
*
* Returns:
* {Boolean} True if the strategy was successfully activated or false if
* the strategy was already active.
*/
activate: function() {
OpenLayers.Strategy.prototype.activate.apply(this, arguments);
this.layer.protocol.read({
callback: this.merge,
scope: this
});
if(OpenLayers.Strategy.prototype.activate.apply(this, arguments)) {
this.layer.protocol.read({
callback: this.merge,
scope: this
});
return true;
}
return false;
},
/**