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) {
L.Control.Loading = L.Control.extend({
options: {
delayIndicator: null,
position: 'topleft',
separate: false,
zoomControl: null,
spinjs: false,
spin: {
lines: 7,
length: 3,
width: 3,
radius: 5,
rotate: 13,
top: "83%"
lines: 7,
length: 3,
width: 3,
radius: 5,
rotate: 13,
top: "83%"
}
},
@@ -74,8 +75,8 @@
}
this._indicator = L.DomUtil.create('a', classes, container);
if (this.options.spinjs) {
this._spinner = new Spinner(this.options.spin).spin();
this._indicator.appendChild(this._spinner.el);
this._spinner = new Spinner(this.options.spin).spin();
this._indicator.appendChild(this._spinner.el);
}
return container;
},
@@ -103,12 +104,32 @@
addLoader: function(id) {
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) {
delete this._dataLoaders[id];
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() {
@@ -183,6 +204,26 @@
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) {
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) {
// Add listeners for begin and end of load to any layers already on the
// map
@@ -226,6 +282,7 @@
// When a layer is added to the map, add listeners for begin and end
// of load
map.on('layeradd', this._layerAdd, this);
map.on('layerremove', this._layerRemove, this);
},
_removeLayerListeners: function(map) {
@@ -238,8 +295,9 @@
}, this);
}, this);
// Remove layeradd listener from map
// Remove layeradd/layerremove listener from map
map.off('layeradd', this._layerAdd, this);
map.off('layerremove', this._layerRemove, this);
},
_addMapListeners: function(map) {
@@ -247,6 +305,7 @@
// events, eg, for AJAX calls that affect the map but will not be
// reflected in the above layer events.
map.on({
baselayerchange: this._handleBaseLayerChange,
dataloading: this._handleLoading,
dataload: this._handleLoad,
layerremove: this._handleLoad
@@ -255,6 +314,7 @@
_removeMapListeners: function(map) {
map.off({
baselayerchange: this._handleBaseLayerChange,
dataloading: this._handleLoading,
dataload: this._handleLoad,
layerremove: this._handleLoad