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

Show number of active layers on a badge #269

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
12 changes: 12 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

copy: {
dist: {
files: [
// copy badge stylesheet
{expand: true, src: ['src/css/badge.css'], dest: 'dist/css/', flatten: true, filter: 'isFile'}

],
},
},

watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint'] // test file can be added here
Expand Down Expand Up @@ -60,13 +70,15 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
*/

/* Default (development): Watch files and build on change. */
grunt.loadNpmTasks("grunt-contrib-jasmine");
grunt.registerTask("default", ["watch", "jasmine"]);
grunt.registerTask("test", ["jshint", "jasmine"]);
grunt.registerTask('build', [
'copy',
'browserify:dist'
]);

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,14 @@ We're going to try spinning this out into its own library; see: https://github.c
hash: true, // by default this is FALSE
}).addTo(map);

## Add badge to display number of active layers

// Link to stylesheet
<link rel="stylesheet" href="../dist/css/badge.css">

// To add the badge
// map and leafletControl are variables containing the map and layers control instances
var badge = activeLayersBadge(map, leafletControl);

// The badge will be included when line is used
L.LayerGroup.EnvironmentalLayers().addTo(map, leafletControl);
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@


<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="../dist/css/badge.css">

</head>

Expand Down
4 changes: 2 additions & 2 deletions example/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,5 @@
var leafletControl = new L.control.layers(baseMaps,overlayMaps);
leafletControl.addTo(map);



// Display number of active overlay layers
var badge = activeLayersBadge(map, leafletControl);
2 changes: 1 addition & 1 deletion example/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ z-index: 10; }
padding-bottom: 25px;
margin-right: 65px;
display: inline;
}
}
138 changes: 138 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"grunt": "^1.0.4",
"grunt-browserify": "^5.0.0",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-jasmine": "^1.1.0",
"grunt-contrib-jshint": "^1.1.0",
"grunt-contrib-uglify": "^2.2.0",
Expand Down
5 changes: 4 additions & 1 deletion src/AllLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ L.LayerGroup.environmentalLayers = L.LayerGroup.extend(

}

L.control.layers(baseMaps,this.overlayMaps).addTo(map);
var leafletControl = L.control.layers(baseMaps,this.overlayMaps);
leafletControl.addTo(map);

if(this.options.hash)
var hash = new L.Hash(map, this.overlayMaps);

// Display number of active overlay layers
var badge = activeLayersBadge(map, leafletControl);
},

onRemove: function (map) {},
Expand Down
40 changes: 40 additions & 0 deletions src/activeLayersBadge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
activeLayersBadge = function(map, leafletControl) {
var touched = false;
var activeOverlays = leafletControl.getActiveOverlayNames();
var badge = L.control.badge(activeOverlays.length, { position: 'topright' }).addTo(map);

map.on('layeradd', function(ev) {
// Displays badge on page load
$(badge.getContainer()).css("display", "block");
if(touched) {
$(badge.getContainer()).css("display", "none");
}
});

map.on('overlayadd', function(ev) {
this.removeControl(badge);
$(badge.getContainer()).css("display", "none");
activeOverlays = leafletControl.getActiveOverlayNames();
badge = L.control.badge(activeOverlays.length, { position: 'topright' }).addTo(map);
$(badge.getContainer()).css("display", "none");
});

map.on('overlayremove', function(ev) {
this.removeControl(badge);
activeOverlays = leafletControl.getActiveOverlayNames();
badge = L.control.badge(activeOverlays.length, { position: 'topright' }).addTo(map);
$(badge.getContainer()).css("display", "none");
});

L.DomEvent.on(leafletControl.getContainer(), 'mouseover', function (ev) {
touched = true;
$(badge.getContainer()).css("display", "none");
});

L.DomEvent.on(leafletControl.getContainer(), 'mouseout', function (ev) {
touched = false;
$(badge.getContainer()).css("display", "block");
});

return badge;
}
16 changes: 16 additions & 0 deletions src/css/badge.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*--- Badge styles ---*/
.badge {
width: 30px;
height: 30px;
padding: 4px 2px 0;
border-radius: 50px;
border: 2px solid #908f8f;
background: white;
color: black;
font-weight: bold;
font-size: 1.4em;
margin-left: 80%;
position: absolute;
text-align: center;
top: 9px;
}
1 change: 1 addition & 0 deletions src/leafletEnvironmentalLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ require('./pfasLayer.js');
require('./indigenousLayers.js');
//require('./PLpeopleLayer.js');
require('./layercode.js')
require('./activeLayersBadge');
require('./eonetFiresLayer')
25 changes: 25 additions & 0 deletions src/util/badgeControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
L.Control.Badge = L.Control.extend({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add this badge functionality to Readme as well. Telling that this is automatically there if you use
L.LayerGroup.EnvironmentalLayers().addTo(map);

otherwise using the above function (which calls listeners) you can add!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay 👍


initialize: function(content, options) {
this.content = content;
L.setOptions(this, options);
},

onAdd: function(map) {
var badgeElement = L.DomUtil.create('div');

badgeElement.classList.add('badge');
badgeElement.innerHTML = this.content;

return badgeElement;
},

onRemove: function(map) {
// Nothing to do here
}

});

L.control.badge = function(content, options) {
return new L.Control.Badge(content, options);
}