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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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','★'), | ||
#' 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','★'), | ||
#' onClick = JS("function(btn, map){ alert('Button 1');}")), | ||
#' easyButton( | ||
#' icon = htmltools::span(class='star','⌖'), | ||
#' onClick = JS("function(btn, map){ alert('Button 2');}")))) | ||
#' | ||
#' | ||
#' @export | ||
addEasyButtonBar <- function( | ||
map, | ||
buttons, | ||
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 | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
44
inst/htmlwidgets/plugins/Leaflet.EasyButton/EasyButton-binding.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofeasyButtonList
altogether?