Skip to content

Commit

Permalink
feat: new title args for wmslegend. #fix 219
Browse files Browse the repository at this point in the history
  • Loading branch information
trafficonese committed Aug 3, 2024
1 parent 6845618 commit 3f27bff
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# leaflet.extras (development version)

## New Features
- `addWMSLegend` gained the arguments `title`, `titleStyle` and `titleClass` to . Fix #219
- `addDrawToolbar` gained the arguments `edittoolbar` and `edithandlers`, which let's you customize all the tooltips

# leaflet.extras 2.0.0

## New Features
Expand Down
13 changes: 10 additions & 3 deletions R/wms-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ wms.legendDependency <- function() {
#' will appear or disappear accordingly based on whether the layer is visible or not.
#' If no layerId is given, it will try to get the layer name from the `uri`,
#' otherwise a random ID will be assigned.
#' @param title A title that is prepended before the image.
#' @param titleClass CSS-class for the title div
#' @param titleStyle Style the title with CSS
#' @param group The group argument is not used. Please set the `layerId` correctly.
#' @rdname wms-legend
#' @export
Expand All @@ -26,19 +29,23 @@ wms.legendDependency <- function() {
#' options = WMSTileOptions(format = "image/png", transparent = TRUE)
#' ) %>%
#' addWMSLegend(
#' title = "Personenschaden_5000", titleStyle = "font-size:1em; font-weight:800",
#' uri = paste0(
#' "https://www.wms.nrw.de/wms/unfallatlas?request=",
#' "GetLegendGraphic&version=1.3.0&",
#' "format=image/png&layer=Personenschaden_5000"
#' )
#' )
addWMSLegend <- function(map, uri, position = "topright", layerId = NULL, group = NULL) {
addWMSLegend <- function(map, uri, position = "topright", layerId = NULL, group = NULL,
title = "", titleClass = "wms-legend-title", titleStyle="") {
map$dependencies <- c(map$dependencies, wms.legendDependency())
options <- leaflet::filterNULL(list(
layerId = layerId,
group = group,
options = list(uri = uri, position = position)
options = list(uri = uri, position = position),
title = title,
titleClass = titleClass,
titleStyle = titleStyle
))

invokeMethod(map, getMapData(map), "addWMSLegend", options)
}
7 changes: 7 additions & 0 deletions inst/htmlwidgets/bindings/lfx-wms-legend-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ LeafletWidget.methods.addWMSLegend = function(options) {
var map = this;
var wmsLegendControl = new L.Control.WMSLegend(options.options);

const titleDiv = document.createElement('div');
titleDiv.className = options.titleClass;
titleDiv.textContent = options.title;
titleDiv.style = options.titleStyle;

// Use the layername as ID if possble
if (!options.layerId) {
const match = wmsLegendControl.options.uri.match(/layer=([^&]+)/);
Expand All @@ -19,6 +24,7 @@ LeafletWidget.methods.addWMSLegend = function(options) {
if (wmslayer && wmslayer.options) {
if (e.layer.options.layers == wmslayer.options.layers) {
map.controls.add(wmsLegendControl, options.layerId);
map.controls._controlsById[options.layerId].container.prepend(titleDiv);
}
}
});
Expand All @@ -32,6 +38,7 @@ LeafletWidget.methods.addWMSLegend = function(options) {
});

map.controls.add(wmsLegendControl, options.layerId);
map.controls._controlsById[options.layerId].container.prepend(titleDiv);

}).call(this);
};

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

18 changes: 17 additions & 1 deletion man/wms-legend.Rd

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

0 comments on commit 3f27bff

Please sign in to comment.