]> git.openstreetmap.org Git - rails.git/blob - app/assets/openlayers/SimpleLayerSwitcher.js
Fix some issues with the new edit menu code
[rails.git] / app / assets / openlayers / SimpleLayerSwitcher.js
1 var SimpleLayerSwitcher = OpenLayers.Class(OpenLayers.Control, {
2     layerStates: null,
3     layersDiv: null,
4     ascending: true,
5
6     initialize: function(options) {
7         OpenLayers.Control.prototype.initialize.apply(this, arguments);
8         this.layerStates = [];
9     },
10
11     destroy: function() {
12         OpenLayers.Event.stopObservingElement(this.div);
13
14         //clear out layers info and unregister their events 
15         this.map.events.un({
16             "addlayer": this.redraw,
17             "changelayer": this.redraw,
18             "removelayer": this.redraw,
19             "changebaselayer": this.redraw,
20             scope: this
21         });
22         OpenLayers.Control.prototype.destroy.apply(this, arguments);
23     },
24
25     setMap: function(map) {
26         OpenLayers.Control.prototype.setMap.apply(this, arguments);
27
28         this.map.events.on({
29             "addlayer": this.redraw,
30             "changelayer": this.redraw,
31             "removelayer": this.redraw,
32             "changebaselayer": this.redraw,
33             scope: this
34         });
35     },
36
37     draw: function() {
38         OpenLayers.Control.prototype.draw.apply(this);
39         this.loadContents();
40         this.redraw();
41         return this.div;
42     },
43
44     checkRedraw: function() {
45         var redraw = false;
46         if ( !this.layerStates.length ||
47              (this.map.layers.length != this.layerStates.length) ) {
48             redraw = true;
49         } else {
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) ) {
57                     redraw = true;
58                     break;
59                 }
60             }
61         }
62         return redraw;
63     },
64
65     redraw: function() {
66         if (!this.checkRedraw()) {
67             return this.div;
68         }
69
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] = {
76                 'name': layer.name,
77                 'visibility': layer.visibility,
78                 'inRange': layer.inRange,
79                 'id': layer.id
80             };
81         }
82
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;
88
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;
95                 layerElem.href = '#';
96
97                 OpenLayers.Element.addClass(layerElem, 'basey');
98                 OpenLayers.Element.addClass(layerElem,
99                     'basey-' + (on ? 'on' : 'off'));
100
101                 if (!baseLayer && !layer.inRange) {
102                     layerElem.disabled = true;
103                 }
104                 var context = {
105                     'layer': layer
106                 };
107                 OpenLayers.Event.observe(layerElem, 'mouseup',
108                     OpenLayers.Function.bindAsEventListener(
109                         this.onInputClick,
110                         context)
111                 );
112
113                 this.div.appendChild(layerElem);
114             }
115         }
116
117         return this.div;
118     },
119
120     onInputClick: function(e) {
121         if (this.layer.isBaseLayer) {
122             this.layer.map.setBaseLayer(this.layer);
123         } else {
124             this.layer.setVisibility(!this.layer.getVisibility());
125         }
126         OpenLayers.Event.stop(e);
127     },
128
129     updateMap: function() {
130
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);
136             }
137         }
138
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);
143         }
144
145     },
146
147     loadContents: function() {
148         //configure main div
149         OpenLayers.Event.observe(this.div, 'mouseup',
150             OpenLayers.Function.bindAsEventListener(this.mouseUp, this));
151         OpenLayers.Event.observe(this.div, 'click',
152                       this.ignoreEvent);
153         OpenLayers.Event.observe(this.div, 'mousedown',
154             OpenLayers.Function.bindAsEventListener(this.mouseDown, this));
155         OpenLayers.Event.observe(this.div, 'dblclick', this.ignoreEvent);
156     },
157
158     ignoreEvent: function(evt) {
159         OpenLayers.Event.stop(evt);
160     },
161
162     mouseDown: function(evt) {
163         this.isMouseDown = true;
164         this.ignoreEvent(evt);
165     },
166
167     mouseUp: function(evt) {
168         if (this.isMouseDown) {
169             this.isMouseDown = false;
170             this.ignoreEvent(evt);
171         }
172     },
173
174     CLASS_NAME: "SimpleLayerSwitcher"
175 });