From ce715bbb0abb37dfee4551e225432152f4d6e4e3 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Mon, 12 Jan 2015 09:37:43 +0100 Subject: [PATCH] Apply geojson default listeners to the GeoJSON layer as well. --- assets/maps/contao-leaflet.js | 58 +++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/assets/maps/contao-leaflet.js b/assets/maps/contao-leaflet.js index 43b3f49..05c457a 100644 --- a/assets/maps/contao-leaflet.js +++ b/assets/maps/contao-leaflet.js @@ -24,26 +24,9 @@ L.Contao = L.Class.extend({ initialize: function() { L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images'; - // Bind triggered data:loading and data:loaded events to the map so that the loading indicator - // is aware of that. Dataloading and dataloaded are the default events which leaflet uses but leaflet.ajax not. - L.Map.addInitHook(function () { - var map = this; - - this.on('layeradd', function(e) { - if (e.layer.on) { - e.layer.on('data:loading', function() { map.fire('dataloading'); }); - e.layer.on('data:loaded', function() { map.fire('dataload'); }); - } - }); - }); - - if (L.GeoJSON.AJAX) { - // Set default pointToLayer and onEachFeature handler. - L.GeoJSON.AJAX.prototype.options = { - pointToLayer: this.pointToLayer.bind(this), - onEachFeature: this.onEachFeature.bind(this) - }; - } + this.bindDataLoadingEvents(); + this.setGeoJsonListeners(L.GeoJSON); + this.setGeoJsonListeners(L.GeoJSON.AJAX); }, /** @@ -198,7 +181,42 @@ L.Contao = L.Class.extend({ obj.bindPopup(feature.properties.popupContent); } } + }, + + /** + * Bind triggered data:loading and data:loaded events to the map. + * + * These events are fired by leaflet.ajax. The loading indicator listens to the map dataloading and dataloaded + * events which is also used by the tile layers. + */ + bindDataLoadingEvents: function() { + // + L.Map.addInitHook(function () { + var map = this; + + this.on('layeradd', function(e) { + if (e.layer.on) { + e.layer.on('data:loading', function() { map.fire('dataloading'); }); + e.layer.on('data:loaded', function() { map.fire('dataload'); }); + } + }); + }); + }, + + /** + * Set the default geojson listeners to the prototype. + * + * @param obj + */ + setGeoJsonListeners: function(obj) { + if (obj && obj.prototype) { + obj.prototype.options = { + pointToLayer: this.pointToLayer.bind(this), + onEachFeature: this.onEachFeature.bind(this) + }; + } } + }); window.ContaoLeaflet = new L.Contao();