Skip to content

Commit

Permalink
Implemented activate, deactivate and destroy methods; use autoActivat…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
ahocevar committed Jul 23, 2010
1 parent b2f8103 commit 7398216
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 19 deletions.
27 changes: 18 additions & 9 deletions examples/graticule.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@
<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";

var graticuleCtl1, graticuleCtl2;
var map1, map2;
function init(){
initLonLat();
initProjected();
}
function initLonLat(){
graticuleCtl1 = new OpenLayers.Control.Graticule({
numPoints: 2,
labelled: true
});
map1 = new OpenLayers.Map('map', {
controls: [
new OpenLayers.Control.Graticule({
numPoints: 2,
labelled: true,
visible: true
}),
graticuleCtl1,
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.Navigation()
Expand All @@ -52,12 +53,13 @@

function initProjected(){
var extent = new OpenLayers.Bounds(-2200000,-712631,3072800,3840000);
graticuleCtl2 = new OpenLayers.Control.Graticule({
labelled: true,
targetSize: 200
});
var mapOptions = {
controls: [
new OpenLayers.Control.Graticule({
labelled: true,
targetSize: 200
}),
graticuleCtl2,
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.Navigation()
Expand Down Expand Up @@ -98,5 +100,12 @@ <h1 id="title">Graticule Example</h1>
<div id="map2" class="smallmap"></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>
</html>
59 changes: 52 additions & 7 deletions lib/OpenLayers/Control/Graticule.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
*/
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
* {Array(Float)} A list of possible graticule widths in degrees.
Expand All @@ -27,8 +34,8 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {

/**
* APIProperty: displayInLayerSwitcher
* {Boolean} Allows the Graticule control to be switched on and off.
* defaults to true.
* {Boolean} Allows the Graticule control to be switched on and off by
* LayerSwitcher control. Defaults is true.
*/
displayInLayerSwitcher: true,

Expand All @@ -53,9 +60,10 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {

/**
* 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
Expand Down Expand Up @@ -102,6 +110,8 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
* to extend the control.
*/
initialize: function(options) {
options = options || {};
options.layerName = options.layerName || OpenLayers.i18n("graticule");
OpenLayers.Control.prototype.initialize.apply(this, [options]);

this.labelSymbolizer.stroke = false;
Expand All @@ -112,6 +122,18 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
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
*
Expand All @@ -134,13 +156,36 @@ OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, {
visibility: this.visible,
displayInLayerSwitcher: this.displayInLayerSwitcher
});
this.map.addLayer(this.gratLayer);
}
this.map.events.register('moveend', this, this.update);
this.update();
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
*
Expand Down
9 changes: 6 additions & 3 deletions lib/OpenLayers/Lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ OpenLayers.Lang.en = {
'E': 'E',
'N': 'N',
'S': 'S',
'graticule': 'Graticule',

// console message
'layerAlreadyAdded':
Expand Down Expand Up @@ -121,9 +122,11 @@ OpenLayers.Lang.en = {
// console message
'pagePositionFailed':
"OpenLayers.Util.pagePosition failed: element with id ${elemId} may be misplaced.",

'end': '',

// console message
'filterEvaluateNotImplemented': "evaluate is not implemented for this filter type."
'filterEvaluateNotImplemented': "evaluate is not implemented for this filter type.",

// **** end ****
'end': ''

};
36 changes: 36 additions & 0 deletions tests/Control/Graticule.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@
"graticule has features");
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>
</head>
Expand Down

0 comments on commit 7398216

Please sign in to comment.