Skip to content

Commit

Permalink
Fix Layer reordering bug. closes #955
Browse files Browse the repository at this point in the history
Update LayerControl._layers object which was being neglected and not being updated. Added a line to properly sort the layerControl based on the MapML Layer zIndex.
  • Loading branch information
AliyanH committed Apr 7, 2024
1 parent 1fcd6f2 commit 86e68b6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/mapml/control/LayerControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,51 @@ export var LayerControl = L.Control.Layers.extend({
_withinZoomBounds: function (zoom, range) {
return range.min <= zoom && zoom <= range.max;
},

// imported from leaflet with slight modifications
// for layerControl ordering based on zIndex
_update: function () {
if (!this._container) {
return this;
}

L.DomUtil.empty(this._baseLayersList);
L.DomUtil.empty(this._overlaysList);

this._layerControlInputs = [];
var baseLayersPresent,
overlaysPresent,
i,
obj,
baseLayersCount = 0;

// <----------- MODIFICATION from the default _update method
// sort the layercontrol layers object based on the zIndex
// provided by MapMLLayer
this._layers.sort(

This comment has been minimized.

Copy link
@prushforth

prushforth Apr 7, 2024

Member

In theory the sorting should be handled by a function passed as an option, but perhaps that's not working for some reason. Looks like it all depends on layer.options.zIndex so not sure what's going on.

(a, b) => a.layer.options.zIndex - b.layer.options.zIndex
);
// -------------------------------------------------->
for (i = 0; i < this._layers.length; i++) {
obj = this._layers[i];
this._addItem(obj);
overlaysPresent = overlaysPresent || obj.overlay;
baseLayersPresent = baseLayersPresent || !obj.overlay;
baseLayersCount += !obj.overlay ? 1 : 0;
}

// Hide base layers section if there's only one layer.
if (this.options.hideSingleBase) {
baseLayersPresent = baseLayersPresent && baseLayersCount > 1;
this._baseLayersList.style.display = baseLayersPresent ? '' : 'none';
}

this._separator.style.display =
overlaysPresent && baseLayersPresent ? '' : 'none';

return this;
},

_addItem: function (obj) {
var layercontrols = obj.layer._layerEl._layerControlHTML;
// the input is required by Leaflet...
Expand Down

0 comments on commit 86e68b6

Please sign in to comment.