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
+34 -5
View File
@@ -229,13 +229,19 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
*/ */
destroy: function() { destroy: function() {
if (this.strategies) { if (this.strategies) {
for(var i=0, len=this.strategies.length; i<len; i++) { var strategy, i, len;
this.strategies[i].destroy(); for(i=0, len=this.strategies.length; i<len; i++) {
strategy = this.strategies[i];
if(strategy.autoDestroy) {
strategy.destroy();
}
} }
this.strategies = null; this.strategies = null;
} }
if (this.protocol) { if (this.protocol) {
this.protocol.destroy(); if(this.protocol.autoDestroy) {
this.protocol.destroy();
}
this.protocol = null; this.protocol = null;
} }
this.destroyFeatures(); this.destroyFeatures();
@@ -296,8 +302,31 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
this.renderer.setSize(this.map.getSize()); this.renderer.setSize(this.map.getSize());
} }
if(this.strategies) { if(this.strategies) {
for(var i=0, len=this.strategies.length; i<len; i++) { var strategy, i, len;
this.strategies[i].activate(); 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();
}
} }
} }
}, },
+9 -1
View File
@@ -20,7 +20,15 @@ OpenLayers.Protocol = OpenLayers.Class({
* Any options sent to the constructor. * Any options sent to the constructor.
*/ */
options: null, 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 * Constructor: OpenLayers.Protocol
* Abstract class for vector protocols. Create instances of a subclass. * Abstract class for vector protocols. Create instances of a subclass.
+42
View File
@@ -21,6 +21,28 @@ OpenLayers.Strategy = OpenLayers.Class({
*/ */
options: null, 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 * Constructor: OpenLayers.Strategy
* Abstract class for vector strategies. Create instances of a subclass. * Abstract class for vector strategies. Create instances of a subclass.
@@ -32,6 +54,8 @@ OpenLayers.Strategy = OpenLayers.Class({
initialize: function(options) { initialize: function(options) {
OpenLayers.Util.extend(this, options); OpenLayers.Util.extend(this, options);
this.options = 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 * Method: activate
* Activate the strategy. Register any listeners, do appropriate setup. * 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() { activate: function() {
if (!this.active) {
this.active = true;
return true;
}
return false;
}, },
/** /**
* Method: deactivate * Method: deactivate
* Deactivate the strategy. Unregister any listeners, do appropriate * Deactivate the strategy. Unregister any listeners, do appropriate
* tear-down. * tear-down.
*
* Returns:
* {Boolean} True if the strategy was successfully deactivated or false if
* the strategy was already inactive.
*/ */
deactivate: function() { deactivate: function() {
if (this.active) {
this.active = false;
return true;
}
return false;
}, },
CLASS_NAME: "OpenLayers.Strategy" CLASS_NAME: "OpenLayers.Strategy"
+12 -5
View File
@@ -39,13 +39,20 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
* Method: activate * Method: activate
* Activate the strategy: reads all features from the protocol and add them * Activate the strategy: reads all features from the protocol and add them
* to the layer. * to the layer.
*
* Returns:
* {Boolean} True if the strategy was successfully activated or false if
* the strategy was already active.
*/ */
activate: function() { activate: function() {
OpenLayers.Strategy.prototype.activate.apply(this, arguments); if(OpenLayers.Strategy.prototype.activate.apply(this, arguments)) {
this.layer.protocol.read({ this.layer.protocol.read({
callback: this.merge, callback: this.merge,
scope: this scope: this
}); });
return true;
}
return false;
}, },
/** /**
+2 -1
View File
@@ -4,13 +4,14 @@
<script type="text/javascript"> <script type="text/javascript">
function test_initialize(t) { function test_initialize(t) {
t.plan(2); t.plan(3);
var options = {}; var options = {};
var protocol = new OpenLayers.Protocol(options); var protocol = new OpenLayers.Protocol(options);
t.ok(protocol instanceof OpenLayers.Protocol, t.ok(protocol instanceof OpenLayers.Protocol,
"new OpenLayers.Protocol returns object" ); "new OpenLayers.Protocol returns object" );
t.eq(protocol.options, options, "constructor sets this.options"); t.eq(protocol.options, options, "constructor sets this.options");
t.eq(protocol.autoDestroy, true, "constructor does not modify this.autoDestroy");
} }
function test_destroy(t) { function test_destroy(t) {
+41 -1
View File
@@ -4,13 +4,16 @@
<script type="text/javascript"> <script type="text/javascript">
function test_initialize(t) { function test_initialize(t) {
t.plan(2); t.plan(5);
var options = {}; var options = {};
var strategy = new OpenLayers.Strategy(options); var strategy = new OpenLayers.Strategy(options);
t.ok(strategy instanceof OpenLayers.Strategy, t.ok(strategy instanceof OpenLayers.Strategy,
"new OpenLayers.Strategy returns object" ); "new OpenLayers.Strategy returns object" );
t.eq(strategy.options, options, "constructor sets this.options"); t.eq(strategy.options, options, "constructor sets this.options");
t.eq(strategy.active, false, "constructor sets this.active to false");
t.eq(strategy.autoActivate, true, "constructor does not modify this.autoActivate");
t.eq(strategy.autoDestroy, true, "constructor does not modify this.autoDestroy");
} }
function test_activate(t) { function test_activate(t) {
@@ -46,6 +49,43 @@
t.eq(strategy.options, null, "destroy nullify protocol.options"); t.eq(strategy.options, null, "destroy nullify protocol.options");
} }
function test_activate(t) {
t.plan(4);
var strategy = new OpenLayers.Strategy({
layer: 'foo'
});
var ret;
ret = strategy.activate();
t.eq(strategy.active, true, "activate sets this.active to true on first call");
t.eq(ret, true, "activate returns true on first call");
ret = strategy.activate();
t.eq(strategy.active, true, "activate does not modify this.active on second call");
t.eq(ret, false, "activate returns false on second call");
}
function test_deactivate(t) {
t.plan(4);
var strategy = new OpenLayers.Strategy({
layer: 'foo'
});
strategy.activate();
var ret;
ret = strategy.deactivate();
t.eq(strategy.active, false, "deactivate sets this.active to false on first call");
t.eq(ret, true, "deactivate returns true on first call");
ret = strategy.deactivate();
t.eq(strategy.active, false, "deactivate does not modify this.active on second call");
t.eq(ret, false, "deactivate returns false on second call");
}
</script> </script>
</head> </head>
<body> <body>