Update leaflet loading.

This commit is contained in:
David Molineus
2016-10-04 13:47:23 +02:00
parent 9c3618a521
commit 8a15b79623
3 changed files with 72 additions and 12 deletions

View File

@@ -13,17 +13,18 @@
function defineLeafletLoading(L) { function defineLeafletLoading(L) {
L.Control.Loading = L.Control.extend({ L.Control.Loading = L.Control.extend({
options: { options: {
delayIndicator: null,
position: 'topleft', position: 'topleft',
separate: false, separate: false,
zoomControl: null, zoomControl: null,
spinjs: false, spinjs: false,
spin: { spin: {
lines: 7, lines: 7,
length: 3, length: 3,
width: 3, width: 3,
radius: 5, radius: 5,
rotate: 13, rotate: 13,
top: "83%" top: "83%"
} }
}, },
@@ -74,8 +75,8 @@
} }
this._indicator = L.DomUtil.create('a', classes, container); this._indicator = L.DomUtil.create('a', classes, container);
if (this.options.spinjs) { if (this.options.spinjs) {
this._spinner = new Spinner(this.options.spin).spin(); this._spinner = new Spinner(this.options.spin).spin();
this._indicator.appendChild(this._spinner.el); this._indicator.appendChild(this._spinner.el);
} }
return container; return container;
}, },
@@ -103,12 +104,32 @@
addLoader: function(id) { addLoader: function(id) {
this._dataLoaders[id] = true; this._dataLoaders[id] = true;
this.updateIndicator(); if (this.options.delayIndicator && !this.delayIndicatorTimeout) {
// If we are delaying showing the indicator and we're not
// already waiting for that delay, set up a timeout.
var that = this;
this.delayIndicatorTimeout = setTimeout(function () {
that.updateIndicator();
that.delayIndicatorTimeout = null;
}, this.options.delayIndicator);
}
else {
// Otherwise show the indicator immediately
this.updateIndicator();
}
}, },
removeLoader: function(id) { removeLoader: function(id) {
delete this._dataLoaders[id]; delete this._dataLoaders[id];
this.updateIndicator(); this.updateIndicator();
// If removing this loader means we're in no danger of loading,
// clear the timeout. This prevents old delays from instantly
// triggering the indicator.
if (this.options.delayIndicator && this.delayIndicatorTimeout && !this.isLoading()) {
clearTimeout(this.delayIndicatorTimeout);
this.delayIndicatorTimeout = null;
}
}, },
updateIndicator: function() { updateIndicator: function() {
@@ -183,6 +204,26 @@
this.addLoader(this.getEventId(e)); this.addLoader(this.getEventId(e));
}, },
_handleBaseLayerChange: function (e) {
var that = this;
// Check for a target 'layer' that contains multiple layers, such as
// L.LayerGroup. This will happen if you have an L.LayerGroup in an
// L.Control.Layers.
if (e.layer && e.layer.eachLayer && typeof e.layer.eachLayer === 'function') {
e.layer.eachLayer(function (layer) {
that._handleBaseLayerChange({ layer: layer });
});
}
else {
// If we're changing to a canvas layer, don't handle loading
// as canvas layers will not fire load events.
if (!(L.TileLayer.Canvas && e.layer instanceof L.TileLayer.Canvas)) {
that._handleLoading(e);
}
}
},
_handleLoad: function(e) { _handleLoad: function(e) {
this.removeLoader(this.getEventId(e)); this.removeLoader(this.getEventId(e));
}, },
@@ -212,6 +253,21 @@
} }
}, },
_layerRemove: function(e) {
if (!e.layer || !e.layer.off) return;
try {
e.layer.off({
loading: this._handleLoading,
load: this._handleLoad
}, this);
}
catch (exception) {
console.warn('L.Control.Loading: Tried and failed to remove ' +
'event handlers from layer', e.layer);
console.warn('L.Control.Loading: Full details', exception);
}
},
_addLayerListeners: function(map) { _addLayerListeners: function(map) {
// Add listeners for begin and end of load to any layers already on the // Add listeners for begin and end of load to any layers already on the
// map // map
@@ -226,6 +282,7 @@
// When a layer is added to the map, add listeners for begin and end // When a layer is added to the map, add listeners for begin and end
// of load // of load
map.on('layeradd', this._layerAdd, this); map.on('layeradd', this._layerAdd, this);
map.on('layerremove', this._layerRemove, this);
}, },
_removeLayerListeners: function(map) { _removeLayerListeners: function(map) {
@@ -238,8 +295,9 @@
}, this); }, this);
}, this); }, this);
// Remove layeradd listener from map // Remove layeradd/layerremove listener from map
map.off('layeradd', this._layerAdd, this); map.off('layeradd', this._layerAdd, this);
map.off('layerremove', this._layerRemove, this);
}, },
_addMapListeners: function(map) { _addMapListeners: function(map) {
@@ -247,6 +305,7 @@
// events, eg, for AJAX calls that affect the map but will not be // events, eg, for AJAX calls that affect the map but will not be
// reflected in the above layer events. // reflected in the above layer events.
map.on({ map.on({
baselayerchange: this._handleBaseLayerChange,
dataloading: this._handleLoading, dataloading: this._handleLoading,
dataload: this._handleLoad, dataload: this._handleLoad,
layerremove: this._handleLoad layerremove: this._handleLoad
@@ -255,6 +314,7 @@
_removeMapListeners: function(map) { _removeMapListeners: function(map) {
map.off({ map.off({
baselayerchange: this._handleBaseLayerChange,
dataloading: this._handleLoading, dataloading: this._handleLoading,
dataload: this._handleLoad, dataload: this._handleLoad,
layerremove: this._handleLoad layerremove: this._handleLoad

File diff suppressed because one or more lines are too long

View File

@@ -55,7 +55,7 @@ $GLOBALS['LEAFLET_LIBRARIES']['leaflet-markercluster'] = array
$GLOBALS['LEAFLET_LIBRARIES']['leaflet-loading'] = array $GLOBALS['LEAFLET_LIBRARIES']['leaflet-loading'] = array
( (
'name' => 'Leaflet.loading', 'name' => 'Leaflet.loading',
'version' => '0.1.16', 'version' => '0.1.23',
'license' => '<a href="https://github.com/ebrelsford/Leaflet.loading/blob/master/LICENSE" target="_blank">MIT</a>', 'license' => '<a href="https://github.com/ebrelsford/Leaflet.loading/blob/master/LICENSE" target="_blank">MIT</a>',
'homepage' => 'https://github.com/ebrelsford/Leaflet.loading', 'homepage' => 'https://github.com/ebrelsford/Leaflet.loading',
'css' => 'assets/leaflet/libs/leaflet-loading/Control.Loading.min.css', 'css' => 'assets/leaflet/libs/leaflet-loading/Control.Loading.min.css',