1/** 2 * Override the adding features functionality of the Leaflet module, 3 * with Marker Clustering logics. 4 */ 5 6(function($, Drupal) { 7 8 /** 9 * Add Leaflet Features with Marker Clustering to the Leaflet Map. 10 * 11 * @param features 12 * Features List definition. 13 * @param initial 14 * Boolean to identify initial status. 15 */ 16 Drupal.Leaflet.prototype.add_features = function (features, initial) { 17 const leaflet_markercluster_options = this.map_settings.leaflet_markercluster.options && this.map_settings.leaflet_markercluster.options.length > 0 ? JSON.parse(this.map_settings.leaflet_markercluster.options) : {}; 18 const leaflet_markercluster_include_path = this.map_settings.leaflet_markercluster.include_path; 19 20 // Define Map Layers holder (both unclustered and clustered). 21 let layers = { 22 unclustered: { 23 // Define a base Layer Group, to hold all (ungrouped) Features Layers. 24 _base: this.create_feature_group() 25 }, 26 clusters: { 27 // Define a base Layer Cluster, to hold all (ungrouped) Clustered Layers. 28 _base: new L.MarkerClusterGroup(leaflet_markercluster_options) 29 } 30 }; 31 32 for (let i = 0; i < features.length; i++) { 33 let feature = features[i]; 34 let lFeature; 35 // In case of a Features Group. 36 if (feature.group) { 37 // Define a named Layer Group, to hold all unClustered Features Layers. 38 layers.unclustered[feature['group_label']] = this.create_feature_group(); 39 // Define a new Layer Group Cluster, to hold specific Group Layers. 40 layers.clusters[feature['group_label']] = new L.MarkerClusterGroup(leaflet_markercluster_options); 41 // Define every single Leaflet Feature of the Group. 42 for (let groupKey in feature.features) { 43 let groupFeature = feature.features[groupKey]; 44 lFeature = this.create_feature(groupFeature); 45 if (lFeature !== undefined) { 46 // If the Leaflet feature is extending the Path class (Polygon, 47 // Polyline, Circle) don't add it to Markercluster if not requested, 48 // and don't add it if specifically requested not to. 49 if ((lFeature.setStyle && !leaflet_markercluster_include_path) || groupFeature['markercluster_excluded']) { 50 layers.unclustered[feature['group_label']].addLayer(lFeature); 51 } 52 else { 53 // Add the single Leaflet Feature to the Layer Group Cluster. 54 layers.clusters[feature['group_label']].addLayer(lFeature); 55 } 56 57 // Allow others to do something with the feature that was just added to the map 58 $(document).trigger('leaflet.feature', [lFeature, groupFeature, this, layers]); 59 } 60 } 61 62 // Add the Group Label Cluster Layer and/or the Group Label Base Layer as Overlay to the Map. 63 if (layers.unclustered[feature['group_label']].getLayers().length > 0 || layers.clusters[feature['group_label']].getLayers().length > 0) { 64 this.add_overlay(feature['group_label'], L.featureGroup([layers.unclustered[feature['group_label']], layers.clusters[feature['group_label']]]), feature['disabled']); 65 } 66 } 67 else { 68 lFeature = this.create_feature(feature); 69 if (lFeature !== undefined) { 70 // If the Leaflet feature is extending the Path class (Polygon, 71 // Polyline, Circle) don't add it to Markercluster if not requested, 72 // and don't add it if specifically requested not to. 73 if ((lFeature.setStyle && !leaflet_markercluster_include_path) || feature['markercluster_excluded']) { 74 layers.unclustered._base.addLayer(lFeature); 75 } 76 else { 77 layers.clusters._base.addLayer(lFeature); 78 } 79 80 // Allow others to do something with the feature that was just added to the map 81 $(document).trigger('leaflet.feature', [lFeature, feature, this]); 82 } 83 } 84 } 85 86 // Add lBaseCluster to the map 87 this.add_overlay(null, L.featureGroup([layers.unclustered._base, layers.clusters._base]), false); 88 89 // Allow plugins to do things after features have been added.
90 $(document).trigger('leaflet.features', [initial || false, this]) 91 }; 92 93})(jQuery, Drupal);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.