Bienvenue sur PostGIS.fr

Bienvenue sur PostGIS.fr , le site de la communauté des utilisateurs francophones de PostGIS.

PostGIS ajoute le support d'objets géographique à la base de données PostgreSQL. En effet, PostGIS "spatialise" le serverur PostgreSQL, ce qui permet de l'utiliser comme une base de données SIG.

Maintenu à jour, en fonction de nos disponibilités et des diverses sorties des outils que nous testons, nous vous proposons l'ensemble de nos travaux publiés en langue française.

source: trunk/workshop-routing-foss4g/web/OpenLayers/lib/OpenLayers/Control/LayerSwitcher.js @ 76

Revision 76, 19.2 KB checked in by djay, 12 years ago (diff)

Ajout du répertoire web

  • Property svn:executable set to *
Line 
1/* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for
2 * full list of contributors). Published under the Clear BSD license. 
3 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
4 * full text of the license. */
5
6/**
7 * @requires OpenLayers/Control.js
8 */
9
10/**
11 * Class: OpenLayers.Control.LayerSwitcher
12 * The LayerSwitcher control displays a table of contents for the map. This
13 * allows the user interface to switch between BaseLasyers and to show or hide
14 * Overlays. By default the switcher is shown minimized on the right edge of
15 * the map, the user may expand it by clicking on the handle.
16 *
17 * To create the LayerSwitcher outside of the map, pass the Id of a html div
18 * as the first argument to the constructor.
19 *
20 * Inherits from:
21 *  - <OpenLayers.Control>
22 */
23OpenLayers.Control.LayerSwitcher = 
24  OpenLayers.Class(OpenLayers.Control, {
25
26    /**
27     * APIProperty: roundedCorner
28     * {Boolean} If true the Rico library is used for rounding the corners
29     *     of the layer switcher div, defaults to true.
30     */
31    roundedCorner: true,
32
33    /** 
34     * APIProperty: roundedCornerColor
35     * {String} The color of the rounded corners, only applies if roundedCorner
36     *     is true, defaults to "darkblue".
37     */
38    roundedCornerColor: "darkblue",
39   
40    /** 
41     * Property: layerStates
42     * {Array(Object)} Basically a copy of the "state" of the map's layers
43     *     the last time the control was drawn. We have this in order to avoid
44     *     unnecessarily redrawing the control.
45     */
46    layerStates: null,
47   
48
49  // DOM Elements
50 
51    /**
52     * Property: layersDiv
53     * {DOMElement}
54     */
55    layersDiv: null,
56   
57    /**
58     * Property: baseLayersDiv
59     * {DOMElement}
60     */
61    baseLayersDiv: null,
62
63    /**
64     * Property: baseLayers
65     * {Array(<OpenLayers.Layer>)}
66     */
67    baseLayers: null,
68   
69   
70    /**
71     * Property: dataLbl
72     * {DOMElement}
73     */
74    dataLbl: null,
75   
76    /**
77     * Property: dataLayersDiv
78     * {DOMElement}
79     */
80    dataLayersDiv: null,
81
82    /**
83     * Property: dataLayers
84     * {Array(<OpenLayers.Layer>)}
85     */
86    dataLayers: null,
87
88
89    /**
90     * Property: minimizeDiv
91     * {DOMElement}
92     */
93    minimizeDiv: null,
94
95    /**
96     * Property: maximizeDiv
97     * {DOMElement}
98     */
99    maximizeDiv: null,
100   
101    /**
102     * APIProperty: ascending
103     * {Boolean}
104     */
105    ascending: true,
106 
107    /**
108     * Constructor: OpenLayers.Control.LayerSwitcher
109     *
110     * Parameters:
111     * options - {Object}
112     */
113    initialize: function(options) {
114        OpenLayers.Control.prototype.initialize.apply(this, arguments);
115        this.layerStates = [];
116    },
117
118    /**
119     * APIMethod: destroy
120     */   
121    destroy: function() {
122       
123        OpenLayers.Event.stopObservingElement(this.div);
124
125        OpenLayers.Event.stopObservingElement(this.minimizeDiv);
126        OpenLayers.Event.stopObservingElement(this.maximizeDiv);
127
128        //clear out layers info and unregister their events
129        this.clearLayersArray("base");
130        this.clearLayersArray("data");
131       
132        this.map.events.un({
133            "addlayer": this.redraw,
134            "changelayer": this.redraw,
135            "removelayer": this.redraw,
136            "changebaselayer": this.redraw,
137            scope: this
138        });
139       
140        OpenLayers.Control.prototype.destroy.apply(this, arguments);
141    },
142
143    /**
144     * Method: setMap
145     *
146     * Properties:
147     * map - {<OpenLayers.Map>}
148     */
149    setMap: function(map) {
150        OpenLayers.Control.prototype.setMap.apply(this, arguments);
151
152        this.map.events.on({
153            "addlayer": this.redraw,
154            "changelayer": this.redraw,
155            "removelayer": this.redraw,
156            "changebaselayer": this.redraw,
157            scope: this
158        });
159    },
160
161    /**
162     * Method: draw
163     *
164     * Returns:
165     * {DOMElement} A reference to the DIV DOMElement containing the
166     *     switcher tabs.
167     */ 
168    draw: function() {
169        OpenLayers.Control.prototype.draw.apply(this);
170
171        // create layout divs
172        this.loadContents();
173
174        // set mode to minimize
175        if(!this.outsideViewport) {
176            this.minimizeControl();
177        }
178
179        // populate div with current info
180        this.redraw();   
181
182        return this.div;
183    },
184
185    /**
186     * Method: clearLayersArray
187     * User specifies either "base" or "data". we then clear all the
188     *     corresponding listeners, the div, and reinitialize a new array.
189     *
190     * Parameters:
191     * layersType - {String} 
192     */
193    clearLayersArray: function(layersType) {
194        var layers = this[layersType + "Layers"];
195        if (layers) {
196            for(var i=0, len=layers.length; i<len ; i++) {
197                var layer = layers[i];
198                OpenLayers.Event.stopObservingElement(layer.inputElem);
199                OpenLayers.Event.stopObservingElement(layer.labelSpan);
200            }
201        }
202        this[layersType + "LayersDiv"].innerHTML = "";
203        this[layersType + "Layers"] = [];
204    },
205
206
207    /**
208     * Method: checkRedraw
209     * Checks if the layer state has changed since the last redraw() call.
210     *
211     * Returns:
212     * {Boolean} The layer state changed since the last redraw() call.
213     */
214    checkRedraw: function() {
215        var redraw = false;
216        if ( !this.layerStates.length ||
217             (this.map.layers.length != this.layerStates.length) ) {
218            redraw = true;
219        } else {
220            for (var i=0, len=this.layerStates.length; i<len; i++) {
221                var layerState = this.layerStates[i];
222                var layer = this.map.layers[i];
223                if ( (layerState.name != layer.name) || 
224                     (layerState.inRange != layer.inRange) || 
225                     (layerState.id != layer.id) || 
226                     (layerState.visibility != layer.visibility) ) {
227                    redraw = true;
228                    break;
229                }   
230            }
231        }   
232        return redraw;
233    },
234   
235    /**
236     * Method: redraw
237     * Goes through and takes the current state of the Map and rebuilds the
238     *     control to display that state. Groups base layers into a
239     *     radio-button group and lists each data layer with a checkbox.
240     *
241     * Returns:
242     * {DOMElement} A reference to the DIV DOMElement containing the control
243     */ 
244    redraw: function() {
245        //if the state hasn't changed since last redraw, no need
246        // to do anything. Just return the existing div.
247        if (!this.checkRedraw()) { 
248            return this.div; 
249        } 
250
251        //clear out previous layers
252        this.clearLayersArray("base");
253        this.clearLayersArray("data");
254       
255        var containsOverlays = false;
256        var containsBaseLayers = false;
257       
258        // Save state -- for checking layer if the map state changed.
259        // We save this before redrawing, because in the process of redrawing
260        // we will trigger more visibility changes, and we want to not redraw
261        // and enter an infinite loop.
262        var len = this.map.layers.length;
263        this.layerStates = new Array(len);
264        for (var i=0; i <len; i++) {
265            var layer = this.map.layers[i];
266            this.layerStates[i] = {
267                'name': layer.name, 
268                'visibility': layer.visibility,
269                'inRange': layer.inRange,
270                'id': layer.id
271            };
272        }   
273
274        var layers = this.map.layers.slice();
275        if (!this.ascending) { layers.reverse(); }
276        for(var i=0, len=layers.length; i<len; i++) {
277            var layer = layers[i];
278            var baseLayer = layer.isBaseLayer;
279
280            if (layer.displayInLayerSwitcher) {
281
282                if (baseLayer) {
283                    containsBaseLayers = true;
284                } else {
285                    containsOverlays = true;
286                }   
287
288                // only check a baselayer if it is *the* baselayer, check data
289                //  layers if they are visible
290                var checked = (baseLayer) ? (layer == this.map.baseLayer)
291                                          : layer.getVisibility();
292   
293                // create input element
294                var inputElem = document.createElement("input");
295                inputElem.id = this.id + "_input_" + layer.name;
296                inputElem.name = (baseLayer) ? this.id + "_baseLayers" : layer.name;
297                inputElem.type = (baseLayer) ? "radio" : "checkbox";
298                inputElem.value = layer.name;
299                inputElem.checked = checked;
300                inputElem.defaultChecked = checked;
301
302                if (!baseLayer && !layer.inRange) {
303                    inputElem.disabled = true;
304                }
305                var context = {
306                    'inputElem': inputElem,
307                    'layer': layer,
308                    'layerSwitcher': this
309                };
310                OpenLayers.Event.observe(inputElem, "mouseup", 
311                    OpenLayers.Function.bindAsEventListener(this.onInputClick,
312                                                            context)
313                );
314               
315                // create span
316                var labelSpan = document.createElement("span");
317                OpenLayers.Element.addClass(labelSpan, "labelSpan")
318                if (!baseLayer && !layer.inRange) {
319                    labelSpan.style.color = "gray";
320                }
321                labelSpan.innerHTML = layer.name;
322                labelSpan.style.verticalAlign = (baseLayer) ? "bottom" 
323                                                            : "baseline";
324                OpenLayers.Event.observe(labelSpan, "click", 
325                    OpenLayers.Function.bindAsEventListener(this.onInputClick,
326                                                            context)
327                );
328                // create line break
329                var br = document.createElement("br");
330   
331               
332                var groupArray = (baseLayer) ? this.baseLayers
333                                             : this.dataLayers;
334                groupArray.push({
335                    'layer': layer,
336                    'inputElem': inputElem,
337                    'labelSpan': labelSpan
338                });
339                                                     
340   
341                var groupDiv = (baseLayer) ? this.baseLayersDiv
342                                           : this.dataLayersDiv;
343                groupDiv.appendChild(inputElem);
344                groupDiv.appendChild(labelSpan);
345                groupDiv.appendChild(br);
346            }
347        }
348
349        // if no overlays, dont display the overlay label
350        this.dataLbl.style.display = (containsOverlays) ? "" : "none";       
351       
352        // if no baselayers, dont display the baselayer label
353        this.baseLbl.style.display = (containsBaseLayers) ? "" : "none";       
354
355        return this.div;
356    },
357
358    /**
359     * Method:
360     * A label has been clicked, check or uncheck its corresponding input
361     *
362     * Parameters:
363     * e - {Event}
364     *
365     * Context: 
366     *  - {DOMElement} inputElem
367     *  - {<OpenLayers.Control.LayerSwitcher>} layerSwitcher
368     *  - {<OpenLayers.Layer>} layer
369     */
370
371    onInputClick: function(e) {
372
373        if (!this.inputElem.disabled) {
374            if (this.inputElem.type == "radio") {
375                this.inputElem.checked = true;
376                this.layer.map.setBaseLayer(this.layer);
377            } else {
378                this.inputElem.checked = !this.inputElem.checked;
379                this.layerSwitcher.updateMap();
380            }
381        }
382        OpenLayers.Event.stop(e);
383    },
384   
385    /**
386     * Method: onLayerClick
387     * Need to update the map accordingly whenever user clicks in either of
388     *     the layers.
389     *
390     * Parameters:
391     * e - {Event}
392     */
393    onLayerClick: function(e) {
394        this.updateMap();
395    },
396
397
398    /**
399     * Method: updateMap
400     * Cycles through the loaded data and base layer input arrays and makes
401     *     the necessary calls to the Map object such that that the map's
402     *     visual state corresponds to what the user has selected in
403     *     the control.
404     */
405    updateMap: function() {
406
407        // set the newly selected base layer       
408        for(var i=0, len=this.baseLayers.length; i<len; i++) {
409            var layerEntry = this.baseLayers[i];
410            if (layerEntry.inputElem.checked) {
411                this.map.setBaseLayer(layerEntry.layer, false);
412            }
413        }
414
415        // set the correct visibilities for the overlays
416        for(var i=0, len=this.dataLayers.length; i<len; i++) {
417            var layerEntry = this.dataLayers[i];   
418            layerEntry.layer.setVisibility(layerEntry.inputElem.checked);
419        }
420
421    },
422
423    /**
424     * Method: maximizeControl
425     * Set up the labels and divs for the control
426     *
427     * Parameters:
428     * e - {Event}
429     */
430    maximizeControl: function(e) {
431
432        // set the div's width and height to empty values, so
433        // the div dimensions can be controlled by CSS
434        this.div.style.width = "";
435        this.div.style.height = "";
436
437        this.showControls(false);
438
439        if (e != null) {
440            OpenLayers.Event.stop(e);                                           
441        }
442    },
443   
444    /**
445     * Method: minimizeControl
446     * Hide all the contents of the control, shrink the size,
447     *     add the maximize icon
448     *
449     * Parameters:
450     * e - {Event}
451     */
452    minimizeControl: function(e) {
453
454        // to minimize the control we set its div's width
455        // and height to 0px, we cannot just set "display"
456        // to "none" because it would hide the maximize
457        // div
458        this.div.style.width = "0px";
459        this.div.style.height = "0px";
460
461        this.showControls(true);
462
463        if (e != null) {
464            OpenLayers.Event.stop(e);                                           
465        }
466    },
467
468    /**
469     * Method: showControls
470     * Hide/Show all LayerSwitcher controls depending on whether we are
471     *     minimized or not
472     *
473     * Parameters:
474     * minimize - {Boolean}
475     */
476    showControls: function(minimize) {
477
478        this.maximizeDiv.style.display = minimize ? "" : "none";
479        this.minimizeDiv.style.display = minimize ? "none" : "";
480
481        this.layersDiv.style.display = minimize ? "none" : "";
482    },
483   
484    /**
485     * Method: loadContents
486     * Set up the labels and divs for the control
487     */
488    loadContents: function() {
489
490        //configure main div
491
492        OpenLayers.Event.observe(this.div, "mouseup", 
493            OpenLayers.Function.bindAsEventListener(this.mouseUp, this));
494        OpenLayers.Event.observe(this.div, "click",
495                      this.ignoreEvent);
496        OpenLayers.Event.observe(this.div, "mousedown",
497            OpenLayers.Function.bindAsEventListener(this.mouseDown, this));
498        OpenLayers.Event.observe(this.div, "dblclick", this.ignoreEvent);
499
500        // layers list div       
501        this.layersDiv = document.createElement("div");
502        this.layersDiv.id = this.id + "_layersDiv";
503        OpenLayers.Element.addClass(this.layersDiv, "layersDiv");
504
505        this.baseLbl = document.createElement("div");
506        this.baseLbl.innerHTML = OpenLayers.i18n("baseLayer");
507        OpenLayers.Element.addClass(this.baseLbl, "baseLbl");
508       
509        this.baseLayersDiv = document.createElement("div");
510        OpenLayers.Element.addClass(this.baseLayersDiv, "baseLayersDiv");
511
512        this.dataLbl = document.createElement("div");
513        this.dataLbl.innerHTML = OpenLayers.i18n("overlays");
514        OpenLayers.Element.addClass(this.dataLbl, "dataLbl");
515       
516        this.dataLayersDiv = document.createElement("div");
517        OpenLayers.Element.addClass(this.dataLayersDiv, "dataLayersDiv");
518
519        if (this.ascending) {
520            this.layersDiv.appendChild(this.baseLbl);
521            this.layersDiv.appendChild(this.baseLayersDiv);
522            this.layersDiv.appendChild(this.dataLbl);
523            this.layersDiv.appendChild(this.dataLayersDiv);
524        } else {
525            this.layersDiv.appendChild(this.dataLbl);
526            this.layersDiv.appendChild(this.dataLayersDiv);
527            this.layersDiv.appendChild(this.baseLbl);
528            this.layersDiv.appendChild(this.baseLayersDiv);
529        }   
530 
531        this.div.appendChild(this.layersDiv);
532
533        if(this.roundedCorner) {
534            OpenLayers.Rico.Corner.round(this.div, {
535                corners: "tl bl",
536                bgColor: "transparent",
537                color: this.roundedCornerColor,
538                blend: false
539            });
540            OpenLayers.Rico.Corner.changeOpacity(this.layersDiv, 0.75);
541        }
542
543        var imgLocation = OpenLayers.Util.getImagesLocation();
544        var sz = new OpenLayers.Size(18,18);       
545
546        // maximize button div
547        var img = imgLocation + 'layer-switcher-maximize.png';
548        this.maximizeDiv = OpenLayers.Util.createAlphaImageDiv(
549                                    "OpenLayers_Control_MaximizeDiv", 
550                                    null, 
551                                    sz, 
552                                    img, 
553                                    "absolute");
554        OpenLayers.Element.addClass(this.maximizeDiv, "maximizeDiv");
555        this.maximizeDiv.style.display = "none";
556        OpenLayers.Event.observe(this.maximizeDiv, "click", 
557            OpenLayers.Function.bindAsEventListener(this.maximizeControl, this)
558        );
559       
560        this.div.appendChild(this.maximizeDiv);
561
562        // minimize button div
563        var img = imgLocation + 'layer-switcher-minimize.png';
564        var sz = new OpenLayers.Size(18,18);       
565        this.minimizeDiv = OpenLayers.Util.createAlphaImageDiv(
566                                    "OpenLayers_Control_MinimizeDiv", 
567                                    null, 
568                                    sz, 
569                                    img, 
570                                    "absolute");
571        OpenLayers.Element.addClass(this.minimizeDiv, "minimizeDiv");
572        this.minimizeDiv.style.display = "none";
573        OpenLayers.Event.observe(this.minimizeDiv, "click", 
574            OpenLayers.Function.bindAsEventListener(this.minimizeControl, this)
575        );
576
577        this.div.appendChild(this.minimizeDiv);
578    },
579   
580    /**
581     * Method: ignoreEvent
582     *
583     * Parameters:
584     * evt - {Event}
585     */
586    ignoreEvent: function(evt) {
587        OpenLayers.Event.stop(evt);
588    },
589
590    /**
591     * Method: mouseDown
592     * Register a local 'mouseDown' flag so that we'll know whether or not
593     *     to ignore a mouseUp event
594     *
595     * Parameters:
596     * evt - {Event}
597     */
598    mouseDown: function(evt) {
599        this.isMouseDown = true;
600        this.ignoreEvent(evt);
601    },
602
603    /**
604     * Method: mouseUp
605     * If the 'isMouseDown' flag has been set, that means that the drag was
606     *     started from within the LayerSwitcher control, and thus we can
607     *     ignore the mouseup. Otherwise, let the Event continue.
608     * 
609     * Parameters:
610     * evt - {Event}
611     */
612    mouseUp: function(evt) {
613        if (this.isMouseDown) {
614            this.isMouseDown = false;
615            this.ignoreEvent(evt);
616        }
617    },
618
619    CLASS_NAME: "OpenLayers.Control.LayerSwitcher"
620});
Note: See TracBrowser for help on using the repository browser.