Skip to content

Commit

Permalink
Merge commit 'refs/pull/43/head' of https://github.com/ismyrnow/leafl…
Browse files Browse the repository at this point in the history
  • Loading branch information
NHellFire committed Feb 14, 2018
2 parents 4662bc9 + f819574 commit 992abb8
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/leaflet.groupedlayercontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ L.Control.GroupedLayers = L.Control.extend({
position: 'topright',
autoZIndex: true,
exclusiveGroups: [],
groupCheckboxes: false
groupCheckboxes: false,
// Whether to sort the layers. When `false`, layers will keep the order
// in which they were added to the control.
sortLayers: false,
// A [compare function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
// that will be used for sorting the layers, when `sortLayers` is `true`.
// The function receives both the `L.Layer` instances and their names, as in
// `sortFunction(layerA, layerB, nameA, nameB)`.
// By default, it sorts layers alphabetically by their name.
sortFunction: function (layerA, layerB, nameA, nameB) {
if (nameA < nameB) {
return -1;
} else if (nameB < nameA) {
return 1;
} else {
return 0;
}
}
},

initialize: function (baseLayers, groupedOverlays, options) {
Expand Down Expand Up @@ -153,6 +170,12 @@ L.Control.GroupedLayers = L.Control.extend({
this._lastZIndex++;
layer.setZIndex(this._lastZIndex);
}

if (this.options.sortLayers) {
this._layers.sort(L.bind(function (a, b) {
return this.options.sortFunction(a.layer, b.layer, a.name, b.name);
}, this));
}
},

_update: function () {
Expand Down

0 comments on commit 992abb8

Please sign in to comment.