Implemented activate, deactivate and destroy methods; use autoActivate option with a default of true. p=jorix,r=me (closes #2567)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10510 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2010-07-23 14:38:21 +00:00
parent b2f810361d
commit 7398216346
4 changed files with 112 additions and 19 deletions
+18 -9
View File
@@ -23,19 +23,20 @@
<script type="text/javascript"> <script type="text/javascript">
Proj4js.defs["EPSG:42304"]="+title=Atlas of Canada, LCC +proj=lcc +lat_1=49 +lat_2=77 +lat_0=49 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"; Proj4js.defs["EPSG:42304"]="+title=Atlas of Canada, LCC +proj=lcc +lat_1=49 +lat_2=77 +lat_0=49 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs";
var graticuleCtl1, graticuleCtl2;
var map1, map2; var map1, map2;
function init(){ function init(){
initLonLat(); initLonLat();
initProjected(); initProjected();
} }
function initLonLat(){ function initLonLat(){
graticuleCtl1 = new OpenLayers.Control.Graticule({
numPoints: 2,
labelled: true
});
map1 = new OpenLayers.Map('map', { map1 = new OpenLayers.Map('map', {
controls: [ controls: [
new OpenLayers.Control.Graticule({ graticuleCtl1,
numPoints: 2,
labelled: true,
visible: true
}),
new OpenLayers.Control.LayerSwitcher(), new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.Navigation() new OpenLayers.Control.Navigation()
@@ -52,12 +53,13 @@
function initProjected(){ function initProjected(){
var extent = new OpenLayers.Bounds(-2200000,-712631,3072800,3840000); var extent = new OpenLayers.Bounds(-2200000,-712631,3072800,3840000);
graticuleCtl2 = new OpenLayers.Control.Graticule({
labelled: true,
targetSize: 200
});
var mapOptions = { var mapOptions = {
controls: [ controls: [
new OpenLayers.Control.Graticule({ graticuleCtl2,
labelled: true,
targetSize: 200
}),
new OpenLayers.Control.LayerSwitcher(), new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.Navigation() new OpenLayers.Control.Navigation()
@@ -98,5 +100,12 @@
<div id="map2" class="smallmap"></div> <div id="map2" class="smallmap"></div>
<div id="docs"></div> <div id="docs"></div>
<br style="clear:both" />
<ul>
<li><a href="#"
onclick="graticuleCtl1.activate(); graticuleCtl2.activate(); return false;">Activate graticule controls</a></li>
<li><a href="#"
onclick="graticuleCtl1.deactivate(); graticuleCtl2.deactivate(); return false;">Deactivate graticule controls</a></li>
</ul>
</body> </body>
</html> </html>
+52 -7
View File
@@ -17,6 +17,13 @@
*/ */
OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, { OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
/**
* APIProperty: autoActivate
* {Boolean} Activate the control when it is added to a map. Default is
* true.
*/
autoActivate: true,
/** /**
* APIProperty: intervals * APIProperty: intervals
* {Array(Float)} A list of possible graticule widths in degrees. * {Array(Float)} A list of possible graticule widths in degrees.
@@ -27,8 +34,8 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
/** /**
* APIProperty: displayInLayerSwitcher * APIProperty: displayInLayerSwitcher
* {Boolean} Allows the Graticule control to be switched on and off. * {Boolean} Allows the Graticule control to be switched on and off by
* defaults to true. * LayerSwitcher control. Defaults is true.
*/ */
displayInLayerSwitcher: true, displayInLayerSwitcher: true,
@@ -53,9 +60,10 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
/** /**
* APIProperty: layerName * APIProperty: layerName
* {String} the name to be displayed in the layer switcher * {String} The name to be displayed in the layer switcher, default is set
* by {<OpenLayers.Lang>}.
*/ */
layerName: "Graticule", layerName: null,
/** /**
* APIProperty: labelled * APIProperty: labelled
@@ -102,6 +110,8 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
* to extend the control. * to extend the control.
*/ */
initialize: function(options) { initialize: function(options) {
options = options || {};
options.layerName = options.layerName || OpenLayers.i18n("graticule");
OpenLayers.Control.prototype.initialize.apply(this, [options]); OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.labelSymbolizer.stroke = false; this.labelSymbolizer.stroke = false;
@@ -112,6 +122,18 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
this.labelSymbolizer.labelYOffset = "${yOffset}"; this.labelSymbolizer.labelYOffset = "${yOffset}";
}, },
/**
* APIMethod: destroy
*/
destroy: function() {
this.deactivate();
OpenLayers.Control.prototype.destroy.apply(this, arguments);
if (this.gratLayer) {
this.gratLayer.destroy();
this.gratLayer = null;
}
},
/** /**
* Method: draw * Method: draw
* *
@@ -134,13 +156,36 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
visibility: this.visible, visibility: this.visible,
displayInLayerSwitcher: this.displayInLayerSwitcher displayInLayerSwitcher: this.displayInLayerSwitcher
}); });
this.map.addLayer(this.gratLayer);
} }
this.map.events.register('moveend', this, this.update);
this.update();
return this.div; return this.div;
}, },
/**
* APIMethod: activate
*/
activate: function() {
if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
this.map.addLayer(this.gratLayer);
this.map.events.register('moveend', this, this.update);
this.update();
return true;
} else {
return false;
}
},
/**
* APIMethod: deactivate
*/
deactivate: function() {
if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
this.map.events.unregister('moveend', this, this.update);
this.map.removeLayer(this.gratLayer);
return true;
} else {
return false;
}
},
/** /**
* Method: update * Method: update
* *
+6 -3
View File
@@ -87,6 +87,7 @@ OpenLayers.Lang.en = {
'E': 'E', 'E': 'E',
'N': 'N', 'N': 'N',
'S': 'S', 'S': 'S',
'graticule': 'Graticule',
// console message // console message
'layerAlreadyAdded': 'layerAlreadyAdded':
@@ -121,9 +122,11 @@ OpenLayers.Lang.en = {
// console message // console message
'pagePositionFailed': 'pagePositionFailed':
"OpenLayers.Util.pagePosition failed: element with id ${elemId} may be misplaced.", "OpenLayers.Util.pagePosition failed: element with id ${elemId} may be misplaced.",
'end': '',
// console message // console message
'filterEvaluateNotImplemented': "evaluate is not implemented for this filter type." 'filterEvaluateNotImplemented': "evaluate is not implemented for this filter type.",
// **** end ****
'end': ''
}; };
+36
View File
@@ -21,6 +21,42 @@
"graticule has features"); "graticule has features");
control.destroy(); control.destroy();
} }
function test_activate(t) {
t.plan(7);
var map = new OpenLayers.Map("map",{projection:"EPSG:4326"});
var layer = new OpenLayers.Layer.WMS();
map.addLayers([layer]);
var control = new OpenLayers.Control.Graticule({});
map.addControl(control);
map.zoomToMaxExtent();
t.ok(control.gratLayer.visibility, "Graticule layer is visible by default");
control.deactivate();
t.ok(control.gratLayer.map == null,
"Graticule layer is not in map when control is deactivated");
control.destroy();
var control2 = new OpenLayers.Control.Graticule({autoActivate:false});
map.addControl(control2);
t.ok(control2.gratLayer.map == null,
"Graticule layer is not in map when autoActivate:false");
t.ok(control2.gratLayer.features.length == 0,
"Graticule layer is empty when autoActivate:false");
control2.activate();
t.ok(control2.gratLayer.map != null,
"Graticule layer is on map when control is activated");
t.ok(control2.gratLayer.features.length > 0,
"Graticule features refreshed after control is activated");
control2.gratLayer.setVisibility(false);
control2.destroy();
t.ok(control2.gratLayer == null,
"Graticule layer is destroyed when control is destroyed");
map.destroy();
}
</script> </script>
</head> </head>