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 1 commit
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;
}
36 changes: 33 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 @@ -257,7 +260,11 @@ L.Control.GroupedLayers = L.Control.extend({
// Create the group container if it doesn't exist
if (!groupContainer) {
groupContainer = document.createElement('div');
groupContainer.className = 'leaflet-control-layers-group';
if (this.options.groupsCollapsable){
groupContainer.className = 'leaflet-control-layers-group group-collapsable collapsed';
}else{
groupContainer.className = 'leaflet-control-layers-group';
}
groupContainer.id = 'leaflet-control-layers-group-' + obj.group.id;

var groupLabel = document.createElement('label');
Expand All @@ -276,6 +283,18 @@ L.Control.GroupedLayers = L.Control.extend({
}
}

if (this.options.groupsCollapsable){
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, groupLabel);
}

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

return label;
},

_onGroupCollapseToggle: function (event) {
event.stopPropagation();
var groupElement=this.closest(".leaflet-control-layers-group");
if (groupElement.classList.contains("group-collapsable") && groupElement.classList.contains("collapsed")){
groupElement.classList.remove("collapsed");
}else if (groupElement.classList.contains("group-collapsable") && groupElement.classList.contains("collapsed")==false){
groupElement.classList.add("collapsed");
}
},

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

var this_legend = this.legend;
Expand Down