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

Support for https://github.com/CliffCloud/Leaflet.EasyButton/ #295

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions NAMESPACE
Expand Up @@ -14,6 +14,8 @@ export(addAwesomeMarkers)
export(addCircleMarkers)
export(addCircles)
export(addControl)
export(addEasyButton)
export(addEasyButtonBar)
export(addGeoJSON)
export(addGraticule)
export(addLabelOnlyMarkers)
Expand Down Expand Up @@ -53,6 +55,10 @@ export(colorNumeric)
export(colorQuantile)
export(createLeafletMap)
export(dispatch)
export(easyButton)
export(easyButtonList)
export(easyButtonState)
export(easyButtonStateList)
export(fitBounds)
export(hideGroup)
export(iconList)
Expand Down
212 changes: 212 additions & 0 deletions R/plugin-easybutton.R
@@ -0,0 +1,212 @@
leafletEasyButtonDependencies <- function() {
list(
htmltools::htmlDependency(
"leaflet-easybutton",
"1.3.1",
system.file("htmlwidgets/plugins/Leaflet.EasyButton", package = "leaflet"),
script = c("easy-button.js", "EasyButton-binding.js"),
stylesheet = c('easy-button.css')
)
)
}

#' Create an easyButton statestate
#' @param stateName the name of the state
#' @param icon the button icon
#' @param title text to show on hover
#' @param onClick the action to take
#' @export
easyButtonState <- function(
stateName,
icon,
title,
onClick
) {
if(!inherits(onClick,'JS_EVAL')) {
stop("onClick needs to be a returned value from a JS() call")
}
structure(list(
stateName = as.character(stateName),
icon = as.character(icon),
title = as.character(title),
onClick = onClick
),
class='leaflet_easybutton_state')
}

#' Create a list of easyButton states.
#' @param ... states created from \code{\link{easyButtonState}()}
#' @export
easyButtonStateList <- function(...) {
res = structure(
list(...),
class = "leaflet_easybutton_state_list"
)
cls = unlist(lapply(res, inherits, 'leaflet_easybutton_state'))
if (any(!cls))
stop('Arguments passed to easyButtonStateList() must be icon objects returned from easyButtonState()')
res
}

#' Creates an easy button.
#' see \url{https://github.com/CliffCloud/Leaflet.EasyButton}
#' @param icon the button icon
#' @param title text to show on hover
#' @param onClick the action to take
#' @param position topleft|topright|bottomleft|bottomright
#' @param id id for the button
#' @param states the states
#' @export
easyButton <- function(
icon = htmltools::span(class='easy-button','!'),
title = "Easy Button",
onClick = JS("function(btn, map){alert('That was easy!');}"),
position = "topleft",
id = NULL,
states = NULL
) {
if(!inherits(onClick,'JS_EVAL')) {
stop("onClick needs to be a returned value from a JS() call")
}
if(!is.null(states) && !inherits(states,'leaflet_easybutton_state_list')) {
stop("states needs to be a returned value from a easyButtonStateList() call")
}
structure(list(
icon = as.character(icon),
title = as.character(title),
onClick = onClick,
position = position,
id = id,
states = states
),
class='leaflet_easybutton')
}

#' Creates a list of easy buttons.
#' @param ... icons created from \code{\link{easyButton}()}
#' @export
easyButtonList = function(...) {
res = structure(
list(...),
class = "leaflet_easybutton_list"
)
cls = unlist(lapply(res, inherits, 'leaflet_easybutton'))
if (any(!cls))
stop('Arguments passed to easyButtonList() must be icon objects returned from easyButton()')
res
}

#' Add a EasyButton on the map
#' see \url{https://github.com/CliffCloud/Leaflet.EasyButton}
#'
#' @param map a map widget object
#' @param button the button object created with \code{\link{easyButton}}
#' @examples
#' library(leaflet)
#'
#' leaf <- leaflet() %>%
#' addTiles() %>%
#' addEasyButton(easyButton(
#' icon = htmltools::span(class='star','&starf;'),
#' onClick = JS("function(btn, map){ map.setZoom(1);}")))
#'
#' @export
addEasyButton <- function(
map,
button = easyButton()
) {

if(!inherits(button,'leaflet_easybutton')) {
stop('button should be created with easyButton()')
}

map$dependencies <- c(map$dependencies, leafletEasyButtonDependencies())

# Add dependencies for various icon libs if required.
if(is.null(button$states)) {
if(grepl('fa-',button$icon))
map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies())
if(grepl('glyphicon-',button$icon))
map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies())
if(grepl('ion-',button$icon))
map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies())
} else {
if(any(sapply(button$states,function(x) grepl('fa-',x$icon))))
map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies())
if(any(sapply(button$states,function(x) grepl('glyphicon-',x$icon))))
map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies())
if(any(sapply(button$states,function(x) grepl('ion-',x$icon))))
map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies())
}

invokeMethod(
map,
getMapData(map),
'addEasyButton',
button
)
}

#' Add a easyButton bar on the map
#' see \url{https://github.com/CliffCloud/Leaflet.EasyButton}
#'
#' @param map a map widget object
#' @param buttons the buttons object created with \code{\link{easyButtonList}}
#' @param position topleft|topright|bottomleft|bottomright
#' @param id id for the button bar
#' @examples
#' library(leaflet)
#'
#' leaf <- leaflet() %>%
#' addTiles() %>%
#' addEasyButtonBar(easyButtonList(
#' easyButton(
#' icon = htmltools::span(class='star','&starf;'),
#' onClick = JS("function(btn, map){ alert('Button 1');}")),
#' easyButton(
#' icon = htmltools::span(class='star','&target;'),
#' onClick = JS("function(btn, map){ alert('Button 2');}"))))
#'
#'
#' @export
addEasyButtonBar <- function(
map,
buttons,
Copy link
Member

Choose a reason for hiding this comment

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

Can we make buttons a ... and get rid of easyButtonList altogether?

position = 'topleft',
id = NULL
) {
if(!inherits(buttons,'leaflet_easybutton_list')) {
stop('button should be created with easyButtonList()')
}

map$dependencies <- c(map$dependencies, leafletEasyButtonDependencies())

# Add dependencies for various icon libs if required.
for(button in buttons) {
if(is.null(button$states)) {
if(grepl('fa-',button$icon))
map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies())
if(grepl('glyphicon-',button$icon))
map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies())
if(grepl('ion-',button$icon))
map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies())
} else {
if(any(sapply(button$states,function(x) grepl('fa-',x$icon))))
map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies())
if(any(sapply(button$states,function(x) grepl('glyphicon-',x$icon))))
map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies())
if(any(sapply(button$states,function(x) grepl('ion-',x$icon))))
map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies())
}
}

invokeMethod(
map,
getMapData(map),
'addEasyButtonBar',
buttons,
position,
id
)
}

61 changes: 61 additions & 0 deletions inst/examples/easyButton.R
@@ -0,0 +1,61 @@
library(leaflet)

#' Add two easy buttons.
#' first to set zoom level to 1,
#' second to find your self
leaflet() %>% addTiles() %>%
addEasyButton(easyButton(
icon='fa-globe', title='Zoom to Level 1',
onClick=JS("function(btn, map){ map.setZoom(1);}"))) %>%
addEasyButton(easyButton(
icon='fa-crosshairs', title='Locate Me',
onClick=JS("function(btn, map){ map.locate({setView: true});}")))

#' <br/><br/>Toggle Button to freeze/unfreeze clustering at a zoom level.
leaflet() %>% addTiles() %>%
addMarkers(data=quakes,
clusterOptions = markerClusterOptions(),
clusterId = 'quakesCluster') %>%
addEasyButton(easyButton(
states = easyButtonStateList(
easyButtonState(
stateName='unfrozen-markers',
icon='ion-toggle',
title='Freeze Clusters',
onClick = JS("
function(btn, map) {
var clusterManager =
map.layerManager.getLayer('cluster',
'quakesCluster');
clusterManager.freezeAtZoom();
btn.state('frozen-markers');
}")
),
easyButtonState(
stateName='frozen-markers',
icon='ion-toggle-filled',
title='UnFreeze Clusters',
onClick = JS("
function(btn, map) {
var clusterManager =
map.layerManager.getLayer('cluster',
'quakesCluster');
clusterManager.unfreeze();
btn.state('unfrozen-markers');
}")
)
)
))

#' <br/><br/>Add two easy buttons in a bar
#' first to set zoom level to 1
#' second to find your self
leaflet() %>% addTiles() %>%
addEasyButtonBar(easyButtonList(
easyButton(
icon='fa-globe', title='Zoom to Level 1',
onClick=JS("function(btn, map){ map.setZoom(1);}")),
easyButton(
icon='fa-crosshairs', title='Locate Me',
onClick=JS("function(btn, map){ map.locate({setView: true});}"))
))
44 changes: 44 additions & 0 deletions inst/htmlwidgets/plugins/Leaflet.EasyButton/EasyButton-binding.js
@@ -0,0 +1,44 @@
getEasyButton = function(button) {

var options = {};

options.position = button.position;

// only add ID if provided
if(button.id) {
options.id = button.id;
}

// if custom states provided use that
// else use provided icon and onClick
if(button.states) {
options.states = button.states;
return L.easyButton(options);
} else {
return L.easyButton(button.icon, button.onClick,
button.title, options );
}
};

LeafletWidget.methods.addEasyButton = function(button) {
getEasyButton(button).addTo(this);
};

LeafletWidget.methods.addEasyButtonBar = function(buttons, position, id) {

var options = {};

options.position = position;

// only add ID if provided
if(id) {
options.id = id;
}

var easyButtons = [];
for(var i=0; i < buttons.length; i++) {
easyButtons[i] = getEasyButton(buttons[i]);
}
L.easyBar(easyButtons).addTo(this);

};
7 changes: 7 additions & 0 deletions inst/htmlwidgets/plugins/Leaflet.EasyButton/LICENSE
@@ -0,0 +1,7 @@
Copyright (C) 2014 Daniel Montague

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.