1 var SimpleLayerSwitcher = OpenLayers.Class(OpenLayers.Control, {
6 initialize: function(options) {
7 OpenLayers.Control.prototype.initialize.apply(this, arguments);
12 OpenLayers.Event.stopObservingElement(this.div);
14 //clear out layers info and unregister their events
16 "addlayer": this.redraw,
17 "changelayer": this.redraw,
18 "removelayer": this.redraw,
19 "changebaselayer": this.redraw,
22 OpenLayers.Control.prototype.destroy.apply(this, arguments);
25 setMap: function(map) {
26 OpenLayers.Control.prototype.setMap.apply(this, arguments);
29 "addlayer": this.redraw,
30 "changelayer": this.redraw,
31 "removelayer": this.redraw,
32 "changebaselayer": this.redraw,
38 OpenLayers.Control.prototype.draw.apply(this);
44 checkRedraw: function() {
46 if ( !this.layerStates.length ||
47 (this.map.layers.length != this.layerStates.length) ) {
50 for (var i=0, len=this.layerStates.length; i<len; i++) {
51 var layerState = this.layerStates[i];
52 var layer = this.map.layers[i];
53 if ( (layerState.name != layer.name) ||
54 (layerState.inRange != layer.inRange) ||
55 (layerState.id != layer.id) ||
56 (layerState.visibility != layer.visibility) ) {
66 if (!this.checkRedraw()) {
70 this.div.innerHTML = '';
71 var len = this.map.layers.length;
72 this.layerStates = [];
73 for (var i = 0; i < this.map.layers.length; i++) {
74 var layer = this.map.layers[i];
75 this.layerStates[i] = {
77 'visibility': layer.visibility,
78 'inRange': layer.inRange,
83 var layers = this.map.layers.slice();
84 if (!this.ascending) { layers.reverse(); }
85 for (var i = 0; i < layers.length; i++) {
86 var layer = layers[i];
87 var baseLayer = layer.isBaseLayer;
89 if (layer.displayInLayerSwitcher && baseLayer) {
90 var on = (baseLayer) ? (layer == this.map.baseLayer)
91 : layer.getVisibility();
92 var layerElem = document.createElement('a');
93 layerElem.id = this.id + '_input_' + layer.name;
94 layerElem.innerHTML = layer.name;
97 OpenLayers.Element.addClass(layerElem, 'basey');
98 OpenLayers.Element.addClass(layerElem,
99 'basey-' + (on ? 'on' : 'off'));
101 if (!baseLayer && !layer.inRange) {
102 layerElem.disabled = true;
107 OpenLayers.Event.observe(layerElem, 'mouseup',
108 OpenLayers.Function.bindAsEventListener(
113 this.div.appendChild(layerElem);
120 onInputClick: function(e) {
121 if (this.layer.isBaseLayer) {
122 this.layer.map.setBaseLayer(this.layer);
124 this.layer.setVisibility(!this.layer.getVisibility());
126 OpenLayers.Event.stop(e);
129 updateMap: function() {
131 // set the newly selected base layer
132 for(var i=0, len=this.baseLayers.length; i<len; i++) {
133 var layerEntry = this.baseLayers[i];
134 if (layerEntry.inputElem.checked) {
135 this.map.setBaseLayer(layerEntry.layer, false);
139 // set the correct visibilities for the overlays
140 for(var i=0, len=this.dataLayers.length; i<len; i++) {
141 var layerEntry = this.dataLayers[i];
142 layerEntry.layer.setVisibility(layerEntry.inputElem.checked);
147 loadContents: function() {
149 OpenLayers.Event.observe(this.div, 'mouseup',
150 OpenLayers.Function.bindAsEventListener(this.mouseUp, this));
151 OpenLayers.Event.observe(this.div, 'click',
153 OpenLayers.Event.observe(this.div, 'mousedown',
154 OpenLayers.Function.bindAsEventListener(this.mouseDown, this));
155 OpenLayers.Event.observe(this.div, 'dblclick', this.ignoreEvent);
158 ignoreEvent: function(evt) {
159 OpenLayers.Event.stop(evt);
162 mouseDown: function(evt) {
163 this.isMouseDown = true;
164 this.ignoreEvent(evt);
167 mouseUp: function(evt) {
168 if (this.isMouseDown) {
169 this.isMouseDown = false;
170 this.ignoreEvent(evt);
174 CLASS_NAME: "SimpleLayerSwitcher"