PageSourceSearch

https://www.gwa-nds.de/sites/all/modules/leaflet_markercluster/leaflet_markercluster.drupal.js?tltl63

js gwa-nds.de collected 2026-09-28 11:29:12 UTC 10,481 bytes, 272 lines download raw bytes

1/*
2 * We are overriding a large part of the JS defined in leaflet (leaflet.drupal.js).
3 * Not nice, but we can't do otherwise without refactoring code in Leaflet.
4 */
5
6(function ($) {
7
8  var LEAFLET_MARKERCLUSTER_EXCLUDE_FROM_CLUSTER = 0x01;
9
10  Drupal.behaviors.leaflet = { // overrides same behavior in leaflet/leaflet.drupal.js
11    attach: function(context, settings) {
12      var start = (new Date()).getTime();
13
14      $(settings.leaflet).each(function () {
15        // skip to the next iteration if the map already exists
16        var container = L.DomUtil.get(this.mapId);
17        if (!container || container._leaflet_id) {
18          return;
19        }
20
21        // load a settings object with all of our map settings
22        var settings = {};
23        for (var setting in this.map.settings) {
24          settings[setting] = this.map.settings[setting];
25        }
26
27        // instantiate our new map
28        var lMap = new L.Map(this.mapId, settings);
29        lMap.bounds = [];
30
31        // add map layers
32        var layers = {}, overlays = {};
33        var i = 0;
34        for (var key in this.map.layers) {
35          var layer = this.map.layers[key];
36          var map_layer = Drupal.leaflet.create_layer(layer, key);
37          //layers[key] = map_layer;
38
39          // Distinguish between "base layers" and "overlays".
40          // Fall back to "base" in case "layer_type" has not been defined in
41          // hook_leaflet_map_info()
42          layer.layer_type = (typeof layer.layer_type === 'undefined') ? 'base' : layer.layer_type;
43          // As stated in http://leafletjs.com/examples/layers-control,
44          // when using multiple base layers, only one of them should be added
45          // to the map at instantiation, but all of them should be present in
46          // the base layers object when creating the layers control.
47          // See statement L.control.layers(layers, overlays) much further below.
48          switch (layer.layer_type) {
49            case 'overlay':
50              //lMap.addLayer(map_layer);
51              overlays[key] = map_layer;
52              break;
53            default:
54              if (i === 0 /*|| !this.map.settings.layerControl*/) {
55                lMap.addLayer(map_layer);
56                i++;
57              }
58              layers[key] = map_layer;
59              break;
60          }
61          i++;
62        }
63
64        var switchEnable = false;
65        for (var key in layers) {
66          if (layers[key].options.switchLayer) {
67            layers[key].setSwitchLayer(layers[layers[key].options.switchLayer]);
68            switchEnable = true;
69          }
70        }
71        if (switchEnable) {
72          switchManager = new SwitchLayerManager(lMap, {baseLayers: layers});
73        }
74
75        // keep an instance of leaflet layers
76        this.map.lLayers = layers;
77
78        // keep an instance of map_id
79        this.map.map_id = this.mapId;
80
81        // @RdB create marker cluster layers if leaflet.markercluster.js is included
82        // There will be one cluster layer for each "clusterGroup".
83        var clusterLayers = {};
84        if (typeof L.MarkerClusterGroup !== 'undefined') {
85
86          // If we specified a custom cluster icon, use that.
87          if (this.map.markercluster_icon) {
88            var icon_settings = this.map.markercluster_icon;
89
90            settings['iconCreateFunction'] = function(cluster) {
91              var icon = new L.Icon({iconUrl: icon_settings.iconUrl});
92
93              // override applicable marker defaults
94              if (icon_settings.iconSize) {
95                icon.options.iconSize = new L.Point(parseInt(icon_settings.iconSize.x), parseInt(icon_settings.iconSize.y));
96              }
97              if (icon_settings.iconAnchor) {
98                icon.options.iconAnchor = new L.Point(parseFloat(icon_settings.iconAnchor.x), parseFloat(icon_settings.iconAnchor.y));
99              }
100              if (icon_settings.popupAnchor) {
101                icon.options.popupAnchor = new L.Point(parseFloat(icon_settings.popupAnchor.x), parseFloat(icon_settings.popupAnchor.y));
102              }
103              if (icon_settings.shadowUrl !== undefined) {
104                icon.options.shadowUrl = icon_settings.shadowUrl;
105              }
106              if (icon_settings.shadowSize) {
107                icon.options.shadowSize = new L.Point(parseInt(icon_settings.shadowSize.x), parseInt(icon_settings.shadowSize.y));
108              }
109              if (icon_settings.shadowAnchor) {
110                icon.options.shadowAnchor = new L.Point(parseInt(icon_settings.shadowAnchor.x), parseInt(icon_settings.shadowAnchor.y));
111              }
112
113              return icon;
114            }
115          }
116        }
117
118        // add features
119        for (i = 0; i < this.features.length; i++) {
120          var feature = this.features[i];
121         
122          var cluster = (feature.type === 'point' || feature.type === 'json') &&
123            (!feature.flags || !(feature.flags & LEAFLET_MARKERCLUSTER_EXCLUDE_FROM_CLUSTER));
124
125          if (cluster) {
126            var clusterGroup = feature.clusterGroup ? feature.clusterGroup : 'global';
127            if (!clusterLayers[clusterGroup]) {
128              // Note: only applicable settings will be used, remainder are ignored
129              clusterLayers[clusterGroup] = new L.MarkerClusterGroup(settings);
130              lMap.addLayer(clusterLayers[clusterGroup]);
131            }
132          }
133          var lFeature;
134
135          // dealing with a layer group
136          if (feature.group) {
137            var lGroup = new L.LayerGroup();
138            for (var groupKey in feature.features) {
139              var groupFeature = feature.features[groupKey];
140              lFeature = leaflet_create_feature(groupFeature, lMap);
141              lFeature.options.regions = feature.regions;
142              if (groupFeature.popup) {
143                lFeature.bindPopup(groupFeature.popup);
144              }
145              lGroup.addLayer(lFeature);
146            }
147
148            // add the group to the layer switcher
149            overlays[feature.label] = lGroup;
150
151            if (cluster && clusterLayers[clusterGroup])  {
152              clusterLayers[clusterGroup].addLayer(lGroup);
153            } else {
154              lMap.addLayer(lGroup);
155            }
156          }
157          else {
158            lFeature = leaflet_create_feature(feature, lMap);
159            // @RdB add to cluster layer if one is defined, else to map
160            if (cluster && clusterLayers[clusterGroup]) {
161              lFeature.options.regions = feature.regions;
162              clusterLayers[clusterGroup].addLayer(lFeature);
163            }
164            else {
165              lMap.addLayer(lFeature);
166            }
167            if (feature.popup) {
168              lFeature.bindPopup(feature.popup/*, {autoPanPadding: L.point(25,25)}*/);
169            }
170          }
171
172          // Allow others to do something with the feature that was just added to the map
173          $(document).trigger('leaflet.feature', [lFeature, feature]);
174        }
175
176        // add layer switcher
177        if (this.map.settings.layerControl) {
178          L.control.layers(layers, overlays).addTo(lMap);
179        }
180
181        // center the map
182        var zoom = this.map.settings.zoom ? this.map.settings.zoom : this.map.settings.zoomDefault;
183        if (this.map.center && (this.map.center.force || this.features.length === 0)) {
184          lMap.setView(new L.LatLng(this.map.center.lat, this.map.center.lon), zoom);
185        }
186        else if (this.features.length > 0) {
187          Drupal.leaflet.fitbounds(lMap);
188          if (this.map.settings.zoom) { // or: if (zoom) ?
189            lMap.setZoom(zoom);
190          }
191        }
192
193        // associate the center and zoom level proprerties to the built lMap.
194        // useful for post-interaction with it
195        lMap.center = lMap.getCenter();
196        lMap.zoom = lMap.getZoom();
197
198        // add attribution
199        if (this.map.settings.attributionControl && this.map.attribution) {
200          lMap.attributionControl.setPrefix(this.map.attribution.prefix);
201          lMap.attributionControl.addAttribution(this.map.attribution.text);
202        }
203
204        // add the leaflet map to our settings object to make it accessible
205        this.lMap = lMap;
206
207        // allow other modules to get access to the map object using jQuery's trigger method
208        $(document).trigger('leaflet.map', [this.map, lMap]);
209
210        // Destroy features so that an AJAX reload does not get parts of the old set.
211        // Required when the View has "Use AJAX" set to Yes.
212        this.features = null;
213      });
214
215      function leaflet_create_feature(feature, lMap) {
216        var lFeature;
217        switch (feature.type) {
218          case 'point':
219            lFeature = Drupal.leaflet.create_point(feature, lMap);
220            break;
221          case 'linestring':
222            lFeature = Drupal.leaflet.create_linestring(feature, lMap);
223            break;
224          case 'polygon':
225            lFeature = Drupal.leaflet.create_polygon(feature, lMap);
226            break;
227          case 'multipolyline':
228            feature.multipolyline = true;
229            // no break;
230          case 'multipolygon':
231            lFeature = Drupal.leaflet.create_multipoly(feature, lMap);
232            break;
233          case 'json':
234            lFeature = Drupal.leaflet.create_json(feature.json, lMap);
235            break;
236          case 'popup':
237            lFeature = Drupal.leaflet.create_popup(feature, lMap);
238            break;
239          case 'circle':
240            lFeature = Drupal.leaflet.create_circle(feature, lMap);
241            break;
242          case 'circlemarker':
243            lFeature = Drupal.leaflet.create_circlemarker(feature, lMap);
244            break;
245          case 'rectangle':
246            lFeature = Drupal.leaflet.create_rectangle(feature, lMap);
247            break;
248        }
249
250        // assign our given unique ID, useful for associating nodes
251        if (feature.leaflet_id) {
252          lFeature._leaflet_id = feature.leaflet_id;
253        }
254
255        var options = {};
256        if (feature.options) {
257          for (var option in feature.options) {
258            options[option] = feature.options[option];
259          }
260          lFeature.setStyle(options);
261        }
262        return lFeature;
263      }
264
265      if (window.console && window.console.log) { // Does not work on IE8
266        var renderTime = (new Date()).getTime() - start;
267        window.console.log('leaflet_markercluster.drupal.js render time: ' + renderTime/1000 + ' s');
268      }
269    }
270  };
271
272})(jQuery);

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.