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/SelectFeature.js @ 76

Revision 76, 19.1 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/**
8 * @requires OpenLayers/Control.js
9 * @requires OpenLayers/Feature/Vector.js
10 * @requires OpenLayers/Handler/Feature.js
11 * @requires OpenLayers/Layer/Vector/RootContainer.js
12 */
13
14/**
15 * Class: OpenLayers.Control.SelectFeature
16 * The SelectFeature control selects vector features from a given layer on
17 * click or hover.
18 *
19 * Inherits from:
20 *  - <OpenLayers.Control>
21 */
22OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
23
24    /**
25     * Constant: EVENT_TYPES
26     *
27     * Supported event types:
28     *  - *beforefeaturehighlighted* Triggered before a feature is highlighted
29     *  - *featurehighlighted* Triggered when a feature is highlighted
30     *  - *featureunhighlighted* Triggered when a feature is unhighlighted
31     */
32    EVENT_TYPES: ["beforefeaturehighlighted", "featurehighlighted", "featureunhighlighted"],
33   
34    /**
35     * Property: multipleKey
36     * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets
37     *     the <multiple> property to true.  Default is null.
38     */
39    multipleKey: null,
40   
41    /**
42     * Property: toggleKey
43     * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets
44     *     the <toggle> property to true.  Default is null.
45     */
46    toggleKey: null,
47   
48    /**
49     * APIProperty: multiple
50     * {Boolean} Allow selection of multiple geometries.  Default is false.
51     */
52    multiple: false, 
53
54    /**
55     * APIProperty: clickout
56     * {Boolean} Unselect features when clicking outside any feature.
57     *     Default is true.
58     */
59    clickout: true,
60
61    /**
62     * APIProperty: toggle
63     * {Boolean} Unselect a selected feature on click.  Default is false.  Only
64     *     has meaning if hover is false.
65     */
66    toggle: false,
67
68    /**
69     * APIProperty: hover
70     * {Boolean} Select on mouse over and deselect on mouse out.  If true, this
71     * ignores clicks and only listens to mouse moves.
72     */
73    hover: false,
74
75    /**
76     * APIProperty: highlightOnly
77     * {Boolean} If true do not actually select features (i.e. place them in the
78     * layer's selected features array), just highlight them. This property has
79     * no effect if hover is false. Defaults to false.
80     */
81    highlightOnly: false,
82   
83    /**
84     * APIProperty: box
85     * {Boolean} Allow feature selection by drawing a box.
86     */
87    box: false,
88   
89    /**
90     * Property: onBeforeSelect
91     * {Function} Optional function to be called before a feature is selected.
92     *     The function should expect to be called with a feature.
93     */
94    onBeforeSelect: function() {},
95   
96    /**
97     * APIProperty: onSelect
98     * {Function} Optional function to be called when a feature is selected.
99     *     The function should expect to be called with a feature.
100     */
101    onSelect: function() {},
102
103    /**
104     * APIProperty: onUnselect
105     * {Function} Optional function to be called when a feature is unselected.
106     *     The function should expect to be called with a feature.
107     */
108    onUnselect: function() {},
109   
110    /**
111     * Property: scope
112     * {Object} The scope to use with the onBeforeSelect, onSelect, onUnselect
113     *     callbacks. If null the scope will be this control.
114     */
115    scope: null,
116
117    /**
118     * APIProperty: geometryTypes
119     * {Array(String)} To restrict selecting to a limited set of geometry types,
120     *     send a list of strings corresponding to the geometry class names.
121     */
122    geometryTypes: null,
123
124    /**
125     * Property: layer
126     * {<OpenLayers.Layer.Vector>} The vector layer with a common renderer
127     * root for all layers this control is configured with (if an array of
128     * layers was passed to the constructor), or the vector layer the control
129     * was configured with (if a single layer was passed to the constructor).
130     */
131    layer: null,
132   
133    /**
134     * Property: layers
135     * {Array(<OpenLayers.Layer.Vector>} The layers this control will work on,
136     * or null if the control was configured with a single layer
137     */
138    layers: null,
139   
140    /**
141     * APIProperty: callbacks
142     * {Object} The functions that are sent to the handlers.feature for callback
143     */
144    callbacks: null,
145   
146    /**
147     * APIProperty: selectStyle
148     * {Object} Hash of styles
149     */
150    selectStyle: null,
151   
152    /**
153     * Property: renderIntent
154     * {String} key used to retrieve the select style from the layer's
155     * style map.
156     */
157    renderIntent: "select",
158
159    /**
160     * Property: handlers
161     * {Object} Object with references to multiple <OpenLayers.Handler>
162     *     instances.
163     */
164    handlers: null,
165
166    /**
167     * Constructor: OpenLayers.Control.SelectFeature
168     * Create a new control for selecting features.
169     *
170     * Parameters:
171     * layers - {<OpenLayers.Layer.Vector>}, or an array of vector layers. The
172     *     layer(s) this control will select features from.
173     * options - {Object}
174     */
175    initialize: function(layers, options) {
176        // concatenate events specific to this control with those from the base
177        this.EVENT_TYPES =
178            OpenLayers.Control.SelectFeature.prototype.EVENT_TYPES.concat(
179            OpenLayers.Control.prototype.EVENT_TYPES
180        );
181        OpenLayers.Control.prototype.initialize.apply(this, [options]);
182       
183        if(this.scope === null) {
184            this.scope = this;
185        }
186        this.initLayer(layers);
187        var callbacks = {
188            click: this.clickFeature,
189            clickout: this.clickoutFeature
190        };
191        if (this.hover) {
192            callbacks.over = this.overFeature;
193            callbacks.out = this.outFeature;
194        }
195             
196        this.callbacks = OpenLayers.Util.extend(callbacks, this.callbacks);
197        this.handlers = {
198            feature: new OpenLayers.Handler.Feature(
199                this, this.layer, this.callbacks,
200                {geometryTypes: this.geometryTypes}
201            )
202        };
203
204        if (this.box) {
205            this.handlers.box = new OpenLayers.Handler.Box(
206                this, {done: this.selectBox},
207                {boxDivClassName: "olHandlerBoxSelectFeature"}
208            ); 
209        }
210    },
211
212    /**
213     * Method: initLayer
214     * Assign the layer property. If layers is an array, we need to use
215     *     a RootContainer.
216     *
217     * Parameters:
218     * layers - {<OpenLayers.Layer.Vector>}, or an array of vector layers.
219     */
220    initLayer: function(layers) {
221        if(layers instanceof Array) {
222            this.layers = layers;
223            this.layer = new OpenLayers.Layer.Vector.RootContainer(
224                this.id + "_container", {
225                    layers: layers
226                }
227            );
228        } else {
229            this.layer = layers;
230        }
231    },
232   
233    /**
234     * Method: destroy
235     */
236    destroy: function() {
237        if(this.active && this.layers) {
238            this.map.removeLayer(this.layer);
239        }
240        OpenLayers.Control.prototype.destroy.apply(this, arguments);
241        if(this.layers) {
242            this.layer.destroy();
243        }
244    },
245
246    /**
247     * Method: activate
248     * Activates the control.
249     *
250     * Returns:
251     * {Boolean} The control was effectively activated.
252     */
253    activate: function () {
254        if (!this.active) {
255            if(this.layers) {
256                this.map.addLayer(this.layer);
257            }
258            this.handlers.feature.activate();
259            if(this.box && this.handlers.box) {
260                this.handlers.box.activate();
261            }
262        }
263        return OpenLayers.Control.prototype.activate.apply(
264            this, arguments
265        );
266    },
267
268    /**
269     * Method: deactivate
270     * Deactivates the control.
271     *
272     * Returns:
273     * {Boolean} The control was effectively deactivated.
274     */
275    deactivate: function () {
276        if (this.active) {
277            this.handlers.feature.deactivate();
278            if(this.handlers.box) {
279                this.handlers.box.deactivate();
280            }
281            if(this.layers) {
282                this.map.removeLayer(this.layer);
283            }
284        }
285        return OpenLayers.Control.prototype.deactivate.apply(
286            this, arguments
287        );
288    },
289
290    /**
291     * Method: unselectAll
292     * Unselect all selected features.  To unselect all except for a single
293     *     feature, set the options.except property to the feature.
294     *
295     * Parameters:
296     * options - {Object} Optional configuration object.
297     */
298    unselectAll: function(options) {
299        // we'll want an option to supress notification here
300        var layers = this.layers || [this.layer];
301        var layer, feature;
302        for(var l=0; l<layers.length; ++l) {
303            layer = layers[l];
304            for(var i=layer.selectedFeatures.length-1; i>=0; --i) {
305                feature = layer.selectedFeatures[i];
306                if(!options || options.except != feature) {
307                    this.unselect(feature);
308                }
309            }
310        }
311    },
312
313    /**
314     * Method: clickFeature
315     * Called on click in a feature
316     * Only responds if this.hover is false.
317     *
318     * Parameters:
319     * feature - {<OpenLayers.Feature.Vector>}
320     */
321    clickFeature: function(feature) {
322        if(!this.hover) {
323            var selected = (OpenLayers.Util.indexOf(
324                feature.layer.selectedFeatures, feature) > -1);
325            if(selected) {
326                if(this.toggleSelect()) {
327                    this.unselect(feature);
328                } else if(!this.multipleSelect()) {
329                    this.unselectAll({except: feature});
330                }
331            } else {
332                if(!this.multipleSelect()) {
333                    this.unselectAll({except: feature});
334                }
335                this.select(feature);
336            }
337        }
338    },
339
340    /**
341     * Method: multipleSelect
342     * Allow for multiple selected features based on <multiple> property and
343     *     <multipleKey> event modifier.
344     *
345     * Returns:
346     * {Boolean} Allow for multiple selected features.
347     */
348    multipleSelect: function() {
349        return this.multiple || (this.handlers.feature.evt &&
350                                 this.handlers.feature.evt[this.multipleKey]);
351    },
352   
353    /**
354     * Method: toggleSelect
355     * Event should toggle the selected state of a feature based on <toggle>
356     *     property and <toggleKey> event modifier.
357     *
358     * Returns:
359     * {Boolean} Toggle the selected state of a feature.
360     */
361    toggleSelect: function() {
362        return this.toggle || (this.handlers.feature.evt &&
363                               this.handlers.feature.evt[this.toggleKey]);
364    },
365
366    /**
367     * Method: clickoutFeature
368     * Called on click outside a previously clicked (selected) feature.
369     * Only responds if this.hover is false.
370     *
371     * Parameters:
372     * feature - {<OpenLayers.Vector.Feature>}
373     */
374    clickoutFeature: function(feature) {
375        if(!this.hover && this.clickout) {
376            this.unselectAll();
377        }
378    },
379
380    /**
381     * Method: overFeature
382     * Called on over a feature.
383     * Only responds if this.hover is true.
384     *
385     * Parameters:
386     * feature - {<OpenLayers.Feature.Vector>}
387     */
388    overFeature: function(feature) {
389        var layer = feature.layer;
390        if(this.hover) {
391            if(this.highlightOnly) {
392                this.highlight(feature);
393            } else if(OpenLayers.Util.indexOf(
394                layer.selectedFeatures, feature) == -1) {
395                this.select(feature);
396            }
397        }
398    },
399
400    /**
401     * Method: outFeature
402     * Called on out of a selected feature.
403     * Only responds if this.hover is true.
404     *
405     * Parameters:
406     * feature - {<OpenLayers.Feature.Vector>}
407     */
408    outFeature: function(feature) {
409        if(this.hover) {
410            if(this.highlightOnly) {
411                // we do nothing if we're not the last highlighter of the
412                // feature
413                if(feature._lastHighlighter == this.id) {
414                    // if another select control had highlighted the feature before
415                    // we did it ourself then we use that control to highlight the
416                    // feature as it was before we highlighted it, else we just
417                    // unhighlight it
418                    if(feature._prevHighlighter &&
419                       feature._prevHighlighter != this.id) {
420                        delete feature._lastHighlighter;
421                        var control = this.map.getControl(
422                            feature._prevHighlighter);
423                        if(control) {
424                            control.highlight(feature);
425                        }
426                    } else {
427                        this.unhighlight(feature);
428                    }
429                }
430            } else {
431                this.unselect(feature);
432            }
433        }
434    },
435
436    /**
437     * Method: highlight
438     * Redraw feature with the select style.
439     *
440     * Parameters:
441     * feature - {<OpenLayers.Feature.Vector>}
442     */
443    highlight: function(feature) {
444        var layer = feature.layer;
445        var cont = this.events.triggerEvent("beforefeaturehighlighted", {
446            feature : feature
447        });
448        if(cont !== false) {
449            feature._prevHighlighter = feature._lastHighlighter;
450            feature._lastHighlighter = this.id;
451            var style = this.selectStyle || this.renderIntent;
452            layer.drawFeature(feature, style);
453            this.events.triggerEvent("featurehighlighted", {feature : feature});
454        }
455    },
456
457    /**
458     * Method: unhighlight
459     * Redraw feature with the "default" style
460     *
461     * Parameters:
462     * feature - {<OpenLayers.Feature.Vector>}
463     */
464    unhighlight: function(feature) {
465        var layer = feature.layer;
466        feature._lastHighlighter = feature._prevHighlighter;
467        delete feature._prevHighlighter;
468        layer.drawFeature(feature, feature.style || feature.layer.style ||
469            "default");
470        this.events.triggerEvent("featureunhighlighted", {feature : feature});
471    },
472   
473    /**
474     * Method: select
475     * Add feature to the layer's selectedFeature array, render the feature as
476     * selected, and call the onSelect function.
477     *
478     * Parameters:
479     * feature - {<OpenLayers.Feature.Vector>}
480     */
481    select: function(feature) {
482        var cont = this.onBeforeSelect.call(this.scope, feature);
483        var layer = feature.layer;
484        if(cont !== false) {
485            cont = layer.events.triggerEvent("beforefeatureselected", {
486                feature: feature
487            });
488            if(cont !== false) {
489                layer.selectedFeatures.push(feature);
490                this.highlight(feature);
491                // if the feature handler isn't involved in the feature
492                // selection (because the box handler is used or the
493                // feature is selected programatically) we fake the
494                // feature handler to allow unselecting on click
495                if(!this.handlers.feature.lastFeature) {
496                    this.handlers.feature.lastFeature = layer.selectedFeatures[0];
497                }
498                layer.events.triggerEvent("featureselected", {feature: feature});
499                this.onSelect.call(this.scope, feature);
500            }
501        }
502    },
503
504    /**
505     * Method: unselect
506     * Remove feature from the layer's selectedFeature array, render the feature as
507     * normal, and call the onUnselect function.
508     *
509     * Parameters:
510     * feature - {<OpenLayers.Feature.Vector>}
511     */
512    unselect: function(feature) {
513        var layer = feature.layer;
514        // Store feature style for restoration later
515        this.unhighlight(feature);
516        OpenLayers.Util.removeItem(layer.selectedFeatures, feature);
517        layer.events.triggerEvent("featureunselected", {feature: feature});
518        this.onUnselect.call(this.scope, feature);
519    },
520   
521    /**
522     * Method: selectBox
523     * Callback from the handlers.box set up when <box> selection is true
524     *     on.
525     *
526     * Parameters:
527     * position - {<OpenLayers.Bounds> || <OpenLayers.Pixel> } 
528     */
529    selectBox: function(position) {
530        if (position instanceof OpenLayers.Bounds) {
531            var minXY = this.map.getLonLatFromPixel(
532                new OpenLayers.Pixel(position.left, position.bottom)
533            );
534            var maxXY = this.map.getLonLatFromPixel(
535                new OpenLayers.Pixel(position.right, position.top)
536            );
537            var bounds = new OpenLayers.Bounds(
538                minXY.lon, minXY.lat, maxXY.lon, maxXY.lat
539            );
540           
541            // if multiple is false, first deselect currently selected features
542            if (!this.multipleSelect()) {
543                this.unselectAll();
544            }
545           
546            // because we're using a box, we consider we want multiple selection
547            var prevMultiple = this.multiple;
548            this.multiple = true;
549            var layers = this.layers || [this.layer];
550            var layer;
551            for(var l=0; l<layers.length; ++l) {
552                layer = layers[l];
553                for(var i=0, len = layer.features.length; i<len; ++i) {
554                    var feature = layer.features[i];
555                    // check if the feature is displayed
556                    if (!feature.getVisibility()) {
557                        continue;
558                    }
559
560                    if (this.geometryTypes == null || OpenLayers.Util.indexOf(
561                            this.geometryTypes, feature.geometry.CLASS_NAME) > -1) {
562                        if (bounds.toGeometry().intersects(feature.geometry)) {
563                            if (OpenLayers.Util.indexOf(layer.selectedFeatures, feature) == -1) {
564                                this.select(feature);
565                            }
566                        }
567                    }
568                }
569            }
570            this.multiple = prevMultiple;
571        }
572    },
573
574    /**
575     * Method: setMap
576     * Set the map property for the control.
577     *
578     * Parameters:
579     * map - {<OpenLayers.Map>}
580     */
581    setMap: function(map) {
582        this.handlers.feature.setMap(map);
583        if (this.box) {
584            this.handlers.box.setMap(map);
585        }
586        OpenLayers.Control.prototype.setMap.apply(this, arguments);
587    },
588   
589    /**
590     * APIMethod: setLayer
591     * Attach a new layer to the control, overriding any existing layers.
592     *
593     * Parameters:
594     * layers - Array of {<OpenLayers.Layer.Vector>} or a single
595     *     {<OpenLayers.Layer.Vector>}
596     */
597    setLayer: function(layers) {
598        var isActive = this.active;
599        this.unselectAll();
600        this.deactivate();
601        if(this.layers) {
602            this.layer.destroy();
603            this.layers = null;
604        }
605        this.initLayer(layers);
606        this.handlers.feature.layer = this.layer;
607        if (isActive) {
608            this.activate();
609        }
610    },
611   
612    CLASS_NAME: "OpenLayers.Control.SelectFeature"
613});
Note: See TracBrowser for help on using the repository browser.