Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
trafficonese committed Aug 21, 2024
2 parents 5feab38 + a815327 commit bd170cc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 41 deletions.
90 changes: 52 additions & 38 deletions R/groupedlayercontrol.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
groupedlayersControlDependencies <- function() {
list(
html_dep_prod("lfx-groupedlayercontrol", "0.6.1", has_style = TRUE, has_binding = TRUE)
)
list(
html_dep_prod("lfx-groupedlayercontrol", "0.6.1", has_style = TRUE, has_binding = TRUE)
)
}

#' @title Leaflet layer control with support for grouping overlays together.
Expand All @@ -23,43 +23,52 @@ groupedlayersControlDependencies <- function() {
#' addTiles(group = "OpenStreetMap") %>%
#' addProviderTiles("CartoDB", group = "CartoDB") %>%
#' addCircleMarkers(runif(20, -75, -74), runif(20, 41, 42),
#' color = "red", group = "Markers2") %>%
#' color = "red", group = "Markers2"
#' ) %>%
#' addCircleMarkers(runif(20, -75, -74), runif(20, 41, 42),
#' color="green", group = "Markers1") %>%
#' color = "green", group = "Markers1"
#' ) %>%
#' addCircleMarkers(runif(20, -75, -74), runif(20, 41, 42),
#' color="yellow", group = "Markers3") %>%
#' color = "yellow", group = "Markers3"
#' ) %>%
#' addCircleMarkers(runif(20, -75, -74), runif(20, 41, 42),
#' color = "lightblue", group = "Markers4") %>%
#' color = "lightblue", group = "Markers4"
#' ) %>%
#' addCircleMarkers(runif(20, -75, -74), runif(20, 41, 42),
#' color = "purple", group = "Markers5") %>%
#' color = "purple", group = "Markers5"
#' ) %>%
#' addGroupedLayersControl(
#' baseGroups = c("OpenStreetMap","CartoDB"),
#' baseGroups = c("OpenStreetMap", "CartoDB"),
#' overlayGroups = list(
#' "Layergroup_2" = c("Markers5","Markers4"),
#' "Layergroup_1" = c("Markers2","Markers1","Markers3")),
#' "Layergroup_2" = c("Markers5", "Markers4"),
#' "Layergroup_1" = c("Markers2", "Markers1", "Markers3")
#' ),
#' position = "topright",
#' options = groupedLayersControlOptions(groupCheckboxes = TRUE,
#' collapsed = FALSE,
#' groupsCollapsable = TRUE,
#' sortLayers = FALSE,
#' sortGroups = FALSE,
#' sortBaseLayers = FALSE,
#' exclusiveGroups = "Layergroup_1")
#' options = groupedLayersControlOptions(
#' groupCheckboxes = TRUE,
#' collapsed = FALSE,
#' groupsCollapsable = TRUE,
#' sortLayers = FALSE,
#' sortGroups = FALSE,
#' sortBaseLayers = FALSE,
#' exclusiveGroups = "Layergroup_1"
#' )
#' )
addGroupedLayersControl <- function(
map,
baseGroups = character(0),
overlayGroups = character(0),
position = c("topright", "bottomright", "bottomleft", "topleft"),
options = groupedLayersControlOptions()) {

map$dependencies <- c(map$dependencies, groupedlayersControlDependencies())
options <- c(options, list(position = match.arg(position)))

groupedOverlayGroups <- transform_groupedoverlays(overlayGroups)
leaflet::invokeMethod(map, NULL, "addGroupedLayersControl",
baseGroups, groupedOverlayGroups,
options)
leaflet::invokeMethod(
map, NULL, "addGroupedLayersControl",
baseGroups, groupedOverlayGroups,
options
)
}

#' Options for the GroupedLayersControl
Expand All @@ -85,18 +94,19 @@ groupedLayersControlOptions <- function(exclusiveGroups = NULL,
collapsed = TRUE,
autoZIndex = TRUE,
...) {

filterNULL(list(exclusiveGroups = exclusiveGroups,
groupCheckboxes = groupCheckboxes,
groupsCollapsable = groupsCollapsable,
groupsExpandedClass = groupsExpandedClass,
groupsCollapsedClass = groupsCollapsedClass,
sortLayers = sortLayers,
sortGroups = sortGroups,
sortBaseLayers = sortBaseLayers,
collapsed = collapsed,
autoZIndex = autoZIndex,
...))
filterNULL(list(
exclusiveGroups = exclusiveGroups,
groupCheckboxes = groupCheckboxes,
groupsCollapsable = groupsCollapsable,
groupsExpandedClass = groupsExpandedClass,
groupsCollapsedClass = groupsCollapsedClass,
sortLayers = sortLayers,
sortGroups = sortGroups,
sortBaseLayers = sortBaseLayers,
collapsed = collapsed,
autoZIndex = autoZIndex,
...
))
}


Expand All @@ -110,16 +120,20 @@ groupedLayersControlOptions <- function(exclusiveGroups = NULL,
#' @rdname GroupedLayersControl
#' @export
addGroupedOverlay <- function(map, group, name, groupname) {
invokeMethod(map, NULL, "addGroupedOverlay",
group, name, groupname)
invokeMethod(
map, NULL, "addGroupedOverlay",
group, name, groupname
)
}

#' @description Add a baselayer to the GroupedLayersControl
#' @rdname GroupedLayersControl
#' @export
addGroupedBaseLayer <- function(map, group, name) {
invokeMethod(map, NULL, "addGroupedBaseLayer",
group, name)
invokeMethod(
map, NULL, "addGroupedBaseLayer",
group, name
)
}

#' @description Remove an overlay layer from the GroupedLayersControl
Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test-groupedlayerscontrol.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ test_that("Test addGroupedLayersControl", {
baseGroups = c("OpenStreetMap", "CartoDB"),
overlayGroups = list(
"Layergroup_2" = c("Markers5", "Markers4"),
"Layergroup_1" = c("Markers2", "Markers1", "Markers3")),
"Layergroup_1" = c("Markers2", "Markers1", "Markers3")
),
position = "topright",
options = groupedLayersControlOptions(groupCheckboxes = TRUE, collapsed = FALSE)
)
Expand All @@ -32,8 +33,11 @@ test_that("Test addGroupedLayersControl", {
baseGroups = c("OpenStreetMap", "CartoDB"),
overlayGroups = list(
"Layergroup_2" = c("Markername5" = "Markers5", "Markername4" = "Markers4"),
"Layergroup_1" = c("Markername2" = "Markers2", "Markername1" = "Markers1",
"Markername3" = "Markers3")),
"Layergroup_1" = c(
"Markername2" = "Markers2", "Markername1" = "Markers1",
"Markername3" = "Markers3"
)
),
position = "topright",
options = groupedLayersControlOptions(groupCheckboxes = TRUE, collapsed = FALSE)
)
Expand Down

0 comments on commit bd170cc

Please sign in to comment.