Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding feature to make individual groups collapsable #46

Open
wants to merge 7 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/leaflet.groupedlayercontrol.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,23 @@
overflow-y: scroll;
padding-right: 10px;
}

.leaflet-control-layers-group.group-collapsable.collapsed .leaflet-control-layers-group-collapse,
.leaflet-control-layers-group.group-collapsable:not(.collapsed) .leaflet-control-layers-group-expand,
.leaflet-control-layers-group.group-collapsable.collapsed label:not(.leaflet-control-layers-group-label){
display: none;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you switch to two-space tabs please? I know the repo doesn't have anything fancy to check for that (and might even have some inconsistencies due to other PRs 🙈 ), so try to match the existing indentation whenever possible.

Copy link
Author

@ForgottenLords ForgottenLords Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I designed the improvements to be disabled by default so that it doesn't cause problems with older configurations, so upgrading should have no problems.

  2. I will try finding time in the next few days to revisit the code and make the requested changes.

}

.leaflet-control-layers-group-expand-default:before{
content: "+";
width: 12px;
display: inline-block;
text-align: center;
}

.leaflet-control-layers-group-collapse-default:before{
content: "-";
width: 12px;
display: inline-block;
text-align: center;
}
35 changes: 32 additions & 3 deletions src/leaflet.groupedlayercontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ L.Control.GroupedLayers = L.Control.extend({
position: 'topright',
autoZIndex: true,
exclusiveGroups: [],
groupCheckboxes: false
groupCheckboxes: false,
groupsCollapsable: false,
groupsCollapseClass: "leaflet-control-layers-group-collapse-default",
groupsExpandClass: "leaflet-control-layers-group-expand-default",
},

initialize: function (baseLayers, groupedOverlays, options) {
Expand Down Expand Up @@ -66,7 +69,7 @@ L.Control.GroupedLayers = L.Control.extend({
var id = L.Util.stamp(layer);
var _layer = this._getLayer(id);
if (_layer) {
delete this.layers[this.layers.indexOf(_layer)];
this._layers.splice(this._layers.indexOf(_layer), 1);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug fix independent from my collapsable groups feature.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Someone else opened a PR for this which I just merged (#50). Can you take this commit out of your branch?

}
this._update();
return this;
Expand Down Expand Up @@ -276,6 +279,21 @@ L.Control.GroupedLayers = L.Control.extend({
}
}

if (this.options.groupsCollapsable){
groupContainer.classList.add("group-collapsable");
groupContainer.classList.add("collapsed");

var groupMin = document.createElement('span');
groupMin.className = 'leaflet-control-layers-group-collapse '+this.options.groupsCollapseClass;
groupLabel.appendChild(groupMin);

var groupMax = document.createElement('span');
groupMax.className = 'leaflet-control-layers-group-expand '+this.options.groupsExpandClass;
groupLabel.appendChild(groupMax);

L.DomEvent.on(groupLabel, 'click', this._onGroupCollapseToggle, groupContainer);
}

var groupName = document.createElement('span');
groupName.className = 'leaflet-control-layers-group-name';
groupName.innerHTML = obj.group.name;
Expand All @@ -296,8 +314,19 @@ L.Control.GroupedLayers = L.Control.extend({

return label;
},

_onGroupCollapseToggle: function (event) {
L.DomEvent.stopPropagation(event);
L.DomEvent.preventDefault(event);
if (this.classList.contains("group-collapsable") && this.classList.contains("collapsed")){
this.classList.remove("collapsed");
}else if (this.classList.contains("group-collapsable") && !this.classList.contains("collapsed")){
this.classList.add("collapsed");
}
},

_onGroupInputClick: function () {
_onGroupInputClick: function (event) {
L.DomEvent.stopPropagation(event);
var i, input, obj;

var this_legend = this.legend;
Expand Down