[PLUGINS] ~maj globale
[lhc/web/www.git] / www / plugins / gis / javascript / leaflet.gis.js
index f55cd4a..87a8992 100644 (file)
@@ -1,4 +1,4 @@
-(function() {
+(function () {
 // Plugin Leaflet L.Map.Gis
 L.Map.Gis = L.Map.extend({
        
@@ -31,38 +31,40 @@ L.Map.Gis = L.Map.extend({
                openId: false,
                affiche_points: true,
                json_points: {
-                       url: "",
-                       objets: "",
+                       url: '',
+                       objets: '',
                        limit: 500,
                        env: [],
-                       titre: "",
-                       description: "",
-                       icone: ""
+                       titre: '',
+                       description: '',
+                       icone: ''
                },
                localize_visitor: false,
                localize_visitor_zoom: 0,
                centrer_fichier: true,
                kml: false,
                gpx: false,
-               geojson: false
+               geojson: false,
+               topojson: false,
+               langue: false
        },
        
        initialize: function (id,options) {
                L.Util.setOptions(this, options);
                
-               this.on('load',function(e){
+               this.on('load', function () {
                        // Affecter sur l'objet du DOM
-                       jQuery("#"+this._container.id).get(0).map = this;
+                       jQuery('#'+this._container.id).get(0).map = this;
                        // Appeler l'éventuelle fonction de callback
-                       if (this.options.callback && typeof(this.options.callback) === "function")
+                       if (this.options.callback && typeof(this.options.callback) === 'function')
                                this.options.callback(this);
                        // trigger load sur l'objet du DOM
-                       jQuery("#"+this._container.id).trigger('load',this);
+                       jQuery('#'+this._container.id).trigger('load',this);
                });
                
                L.Map.prototype.initialize.call(this, id, options);
                
-               if (this.options.utiliser_bb){
+               if (this.options.utiliser_bb) {
                        this.fitBounds(
                                L.latLngBounds(
                                        [this.options.sw_lat, this.options.sw_lon],
@@ -79,9 +81,9 @@ L.Map.Gis = L.Map.extend({
                
                this.addOverlays();
                
-               if (this.options.localize_visitor){
+               if (this.options.localize_visitor) {
                        var maxZoom = this.options.localize_visitor_zoom;
-                       this.on('locationerror',function(e){
+                       this.on('locationerror',function (e) {
                                maxZoom = this.options.zoom;
                                alert(e.message);
                        });
@@ -89,22 +91,27 @@ L.Map.Gis = L.Map.extend({
                }
                
                // Si pas de points affichés trigger ready ici
-               if (!this.options.affiche_points || !this.options.json_points.length)
-                       jQuery("#"+this._container.id).trigger('ready',this);
+               if (!this.options.affiche_points || !Object.keys(this.options.json_points).length) {
+                       jQuery('#'+this._container.id).trigger('ready', this);
+               }
        },
 
-       populateTileLayers: function (tilelayers) {
+       populateTileLayers: function () {
                // Fond de carte par défaut
-               var default_layer = this.createTileLayer(this.options.default_layer);
-               this.addLayer(default_layer);
+               if (this.options.default_layer != 'none') {
+                       var default_layer = this.createTileLayer(this.options.default_layer);
+                       this.addLayer(default_layer);
+               }
                // Fonds de carte supplémentaires
-               if (this.options.layersControl && !this.options.noControl && this.options.affiche_layers.length>1){
+               if (this.options.layersControl && !this.options.noControl && this.options.affiche_layers.length>1) {
                        var layers_control = L.control.layers('','',this.options.layersControlOptions);
-                       layers_control.addBaseLayer(default_layer,this.options.gis_layers[this.options.default_layer].nom);
-                       for(var l in this.options.affiche_layers){
-                               if (this.options.affiche_layers[l]!==this.options.default_layer){
+                       if (this.options.default_layer != 'none') {
+                               layers_control.addBaseLayer(default_layer,this.options.gis_layers[this.options.default_layer].nom);
+                       }
+                       for (var l in this.options.affiche_layers) {
+                               if (this.options.affiche_layers[l]!==this.options.default_layer) {
                                        var layer = this.createTileLayer(this.options.affiche_layers[l]);
-                                       if (typeof layer!=="undefined")
+                                       if (typeof layer!=='undefined')
                                                layers_control.addBaseLayer(layer,this.options.gis_layers[this.options.affiche_layers[l]].nom);
                                }
                        }
@@ -120,7 +127,7 @@ L.Map.Gis = L.Map.extend({
                this.attributionControl.setPrefix('');
                if (this.options.scaleControl)
                        L.control.scale().addTo(this);
-               if (this.options.overviewControl){
+               if (this.options.overviewControl && this.options.default_layer != 'none') {
                        // todo ajouter une option pour permettre de choisir la couche à afficher dans la minimap
                        var minimap_layer = this.createTileLayer(this.options.default_layer);
                        L.control.minimap(minimap_layer,{width: 100,height: 100, toggleDisplay: true}).addTo(this);
@@ -129,15 +136,15 @@ L.Map.Gis = L.Map.extend({
        
        createTileLayer: function (name) {
                var layer;
-               if (typeof this.options.gis_layers[name]!=="undefined")
-                       eval("layer=new "+ this.options.gis_layers[name].layer +";");
+               if (typeof this.options.gis_layers[name]!=='undefined')
+                       eval('layer=new '+ this.options.gis_layers[name].layer +';');
                return layer;
        },
 
        // API setGeoJsonFeatureIcon : Pour Ajouter l'icone d'un point (feature = item d'un GeoJson)
        setGeoJsonFeatureIcon: function (feature, layer) {
                // Déclarer l'icone du points, si défini
-               if (feature.properties && feature.properties.icon){
+               if (feature.properties && feature.properties.icon) {
                        icon_options = {
                                'iconUrl': feature.properties.icon,
                                'iconSize': [feature.properties.icon_size[0], feature.properties.icon_size[1]],
@@ -156,12 +163,26 @@ L.Map.Gis = L.Map.extend({
        // API setGeoJsonFeaturePopup : Pour Ajouter le texte de popup d'un point (feature = item d'un GeoJson)
        setGeoJsonFeaturePopup: function (feature, layer) {
                // Déclarer le contenu de la popup s'il y en a
-               if (feature.properties && (feature.properties.title || feature.properties.description)){
-                       var popupContent = '';
-                       var popupOptions = '';
-                       if (feature.properties.title)
+               if (feature.properties 
+                       && !feature.properties.noclick 
+                       && (feature.properties.title || feature.properties.description || 
+                               (this.options.langue && (feature.properties['title_'+this.options.langue] || feature.properties['description_'+this.options.langue])))) {
+                       var popupContent = '',
+                               popupOptions = '',
+                               description_ok = false;
+                       if (this.options.langue) {
+                               langue = this.options.langue;
+                               if (feature.properties['title_'+langue]) {
+                                       popupContent = '<strong class="title">' + feature.properties['title_'+langue] + '</strong>';
+                               } else if (feature.properties.title)
+                                       popupContent = '<strong class="title">' + feature.properties.title + '</strong>';
+                               if (feature.properties['description_'+langue]) {
+                                       popupContent = popupContent + feature.properties['description_'+langue];
+                                       description_ok = true;
+                               }
+                       } else if(feature.properties.title)
                                popupContent = '<strong class="title">' + feature.properties.title + '</strong>';
-                       if (feature.properties.description)
+                       if (!description_ok && feature.properties.description)
                                popupContent = popupContent + feature.properties.description;
                        if (feature.properties.popup_options)
                                popupOptions = feature.properties.popup_options;
@@ -170,22 +191,29 @@ L.Map.Gis = L.Map.extend({
        },
        
        // API parseGeoJson
-       parseGeoJson: function(data) {
+       parseGeoJson: function (data) {
                var map = this;
                // Analyse des points et déclaration (sans regroupement des points en cluster)
-               if (!map.options.cluster){
-                       if (data.features.length > 0){
+               if (!map.options.cluster) {
+                       if (data.features && data.features.length > 0) {
                                var geojson = L.geoJson('', {
-                                       style: this.options.pathStyles,
+                                       style: this.options.pathStyles ? this.options.pathStyles : function (feature) {
+                                               if (feature.properties && feature.properties.styles)
+                                                       return feature.properties.styles;
+                                               else
+                                                       return '';
+                                       },
                                        onEachFeature: function (feature, layer) {
                                                // Déclarer l'icone du point
-                                               map.setGeoJsonFeatureIcon(feature, layer);
+                                               if (feature.geometry.type == 'Point') {
+                                                       map.setGeoJsonFeatureIcon(feature, layer);
+                                               }
                                                // Déclarer le contenu de la popup s'il y en a
                                                map.setGeoJsonFeaturePopup(feature, layer);
                                        }
                                }).addData(data).addTo(map);
-                               
-                               if (map.options.autocenterandzoom){
+
+                               if (map.options.autocenterandzoom) {
                                        if (data.features.length == 1 && data.features[0].geometry.type == 'Point')
                                                map.setView(geojson.getBounds().getCenter(), map.options.zoom);
                                        else
@@ -194,15 +222,15 @@ L.Map.Gis = L.Map.extend({
                                if (map.options.openId)
                                        gis_focus_marker(map.options.openId,map.options.mapId);
 
-                               if (typeof map.geojsons=="undefined") map.geojsons = [];
+                               if (typeof map.geojsons=='undefined') map.geojsons = [];
                                map.geojsons.push(geojson);
                        }
                } else {
-                       map.markers = L.markerClusterGroup(map.options.clusterOptions);
-
+                       map.markerCluster = L.markerClusterGroup(map.options.clusterOptions).addTo(map);
+                       var markers = [];
                        /* Pour chaque points présents, on crée un marqueur */
-                       jQuery.each(data.features, function(i, feature){
-                               if (feature.geometry.coordinates[0]){
+                       jQuery.each(data.features, function (i, feature) {
+                               if (feature.geometry.coordinates[0]) {
                                        var marker = L.marker([feature.geometry.coordinates[1], feature.geometry.coordinates[0]]);
 
                                        // Déclarer l'icone du point
@@ -211,30 +239,35 @@ L.Map.Gis = L.Map.extend({
                                        map.setGeoJsonFeaturePopup(feature, marker);
 
                                        marker.id = feature.id;
-                                       map.markers.addLayer(marker);
+                                       markers.push(marker);
                                }
                        });
 
-                       map.addLayer(map.markers);
+                       map.markerCluster.addLayers(markers);
 
-                       if (map.options.autocenterandzoom){
+                       if (map.options.autocenterandzoom) {
                                if (data.features.length > 1)
-                                       map.fitBounds(map.markers.getBounds());
+                                       map.fitBounds(map.markerCluster.getBounds());
                                else
-                                       map.setView(map.markers.getBounds().getCenter(), map.options.zoom);
+                                       map.setView(map.markerCluster.getBounds().getCenter(), map.options.zoom);
                        }
                }
        },
        
        // API Compat GIS3
-       addJSON: function(data) {
+       addJSON: function (data) {
                return this.parseGeoJson(data);
        },
        
        // API Compat GIS3
-       removeAllMarkers: function(){
-               if (typeof this.geojsons=="undefined") this.geojsons = [];
-               for(var i in this.geojsons){
+       removeAllMarkers: function () {
+               // virer les éléments du cluster s'il est utilisé
+               if (this.options.cluster) {
+                       this.markerCluster.clearLayers();
+               }
+               // virer les points de la carte
+               if (typeof this.geojsons=='undefined') this.geojsons = [];
+               for (var i in this.geojsons) {
                        this.geojsons[i].clearLayers();
                        this.removeLayer(this.geojsons[i]);
                }
@@ -244,32 +277,32 @@ L.Map.Gis = L.Map.extend({
        loadData: function () {
                var map = this;
                if (map.options.affiche_points
-                       && typeof(map.options.json_points) !== "undefined"
-                       && map.options.json_points.url.length){
+                       && typeof(map.options.json_points) !== 'undefined'
+                       && map.options.json_points.url.length) {
                        // Récupération des points à mettre sur la carte, via json externe
                        var args = {};
                        jQuery.extend(true, args, map.options.json_points.env);
-                       if (typeof map.options.json_points.objets !== "undefined"){
+                       if (typeof map.options.json_points.objets !== 'undefined') {
                                args.objets = map.options.json_points.objets;
-                               if (args.objets == "point_libre"){
+                               if (args.objets == 'point_libre') {
                                        args.lat = map.options.center[0];
                                        args.lon = map.options.center[1];
-                                       if (typeof map.options.json_points.titre !== "undefined")
+                                       if (typeof map.options.json_points.titre !== 'undefined')
                                                args.titre = map.options.json_points.titre;
-                                       if (typeof map.options.json_points.description !== "undefined")
+                                       if (typeof map.options.json_points.description !== 'undefined')
                                                args.description = map.options.json_points.description;
-                                       if (typeof map.options.json_points.icone !== "undefined")
+                                       if (typeof map.options.json_points.icone !== 'undefined')
                                                args.icone = map.options.json_points.icone;
                                }
                        }
-                       if (typeof map.options.json_points.limit !== "undefined")
+                       if (typeof map.options.json_points.limit !== 'undefined')
                                args.limit = map.options.json_points.limit;
                        jQuery.getJSON(map.options.json_points.url,args,
-                               function(data) {
-                                       if (data){
+                               function (data) {
+                                       if (data) {
                                                // Charger le json (data) et déclarer les points
                                                map.parseGeoJson(data);
-                                               jQuery("#"+map._container.id).trigger('ready',map);
+                                               jQuery('#'+map._container.id).trigger('ready',map);
                                        }
                                }
                        );
@@ -278,32 +311,41 @@ L.Map.Gis = L.Map.extend({
        
        addOverlays: function () {
                var map = this;
-               if (map.options.kml && map.options.kml.length){
+               if (map.options.kml && map.options.kml.length) {
                        map.kml = {};
-                       for(var i in map.options.kml){
+                       for (var i in map.options.kml) {
                                map.kml[i] = new L.KML(map.options.kml[i], {async: true});
                                if (map.options.centrer_fichier)
-                                       map.kml[i].on("loaded", function(e) { map.fitBounds(e.target.getBounds()); });
+                                       map.kml[i].on('loaded', function (e) { map.fitBounds(e.target.getBounds()); });
                                map.addLayer(map.kml[i]);
                        }
                }
-               if (map.options.gpx && map.options.gpx.length){
+               if (map.options.gpx && map.options.gpx.length) {
                        map.gpx = {};
-                       for(var i in map.options.gpx){
+                       for (var i in map.options.gpx) {
                                map.gpx[i] = new L.GPX(map.options.gpx[i], {async: true});
                                if (map.options.centrer_fichier)
-                                       map.gpx[i].on("loaded", function(e) { map.fitBounds(e.target.getBounds()); });
+                                       map.gpx[i].on('loaded', function (e) { map.fitBounds(e.target.getBounds()); });
                                map.addLayer(map.gpx[i]);
                        }
                }
-               if (map.options.geojson && map.options.geojson.length){
-                       for(var i in map.options.geojson){
-                               jQuery.getJSON(map.options.geojson[i], function(data){
+               if (map.options.geojson && map.options.geojson.length) {
+                       for (var i in map.options.geojson) {
+                               jQuery.getJSON(map.options.geojson[i], function (data) {
                                        if (data)
                                                map.parseGeoJson(data);
                                });
                        }
                }
+               if (map.options.topojson && map.options.topojson.length) {
+                       for (var i in map.options.topojson) {
+                               map.topojson[i] = new L.TOPOJSON(map.options.topojson[i], {async: true});
+                               if (map.options.centrer_fichier) {
+                                       map.topojson[i].on('loaded', function (e) { map.fitBounds(e.target.getBounds()); });
+                               }
+                               map.addLayer(map.topojson[i]);
+                       }
+               }
        }
 });