|
| 1 | +leafletEasyButtonDependencies <- function() { |
| 2 | + list( |
| 3 | + htmltools::htmlDependency( |
| 4 | + "leaflet-easybutton", |
| 5 | + "1.3.1", |
| 6 | + system.file("htmlwidgets/plugins/Leaflet.EasyButton", package = "leaflet"), |
| 7 | + script = c("easy-button.js", "EasyButton-binding.js"), |
| 8 | + stylesheet = c('easy-button.css') |
| 9 | + ) |
| 10 | + ) |
| 11 | +} |
| 12 | + |
| 13 | +#' Create an easyButton statestate |
| 14 | +#' @param stateName the name of the state |
| 15 | +#' @param icon the button icon |
| 16 | +#' @param title text to show on hover |
| 17 | +#' @param onClick the action to take |
| 18 | +#' @export |
| 19 | +easyButtonState <- function( |
| 20 | + stateName, |
| 21 | + icon, |
| 22 | + title, |
| 23 | + onClick |
| 24 | +) { |
| 25 | + if(!inherits(onClick,'JS_EVAL')) { |
| 26 | + stop("onClick needs to be a returned value from a JS() call") |
| 27 | + } |
| 28 | + structure(list( |
| 29 | + stateName = as.character(stateName), |
| 30 | + icon = as.character(icon), |
| 31 | + title = as.character(title), |
| 32 | + onClick = onClick |
| 33 | + ), |
| 34 | + class='leaflet_easybutton_state') |
| 35 | +} |
| 36 | + |
| 37 | +#' Create a list of easyButton states. |
| 38 | +#' @param ... states created from \code{\link{easyButtonState}()} |
| 39 | +#' @export |
| 40 | +easyButtonStateList <- function(...) { |
| 41 | + res = structure( |
| 42 | + list(...), |
| 43 | + class = "leaflet_easybutton_state_list" |
| 44 | + ) |
| 45 | + cls = unlist(lapply(res, inherits, 'leaflet_easybutton_state')) |
| 46 | + if (any(!cls)) |
| 47 | + stop('Arguments passed to easyButtonStateList() must be icon objects returned from easyButtonState()') |
| 48 | + res |
| 49 | +} |
| 50 | + |
| 51 | +#' Creates an easy button. |
| 52 | +#' see \url{https://github.com/CliffCloud/Leaflet.EasyButton} |
| 53 | +#' @param icon the button icon |
| 54 | +#' @param title text to show on hover |
| 55 | +#' @param onClick the action to take |
| 56 | +#' @param position topleft|topright|bottomleft|bottomright |
| 57 | +#' @param id id for the button |
| 58 | +#' @param states the states |
| 59 | +#' @export |
| 60 | +easyButton <- function( |
| 61 | + icon = htmltools::span(class='easy-button','•'), |
| 62 | + title = "Easy Button", |
| 63 | + onClick = JS("function(btn, map){alert('That was easy!');}"), |
| 64 | + position = "topleft", |
| 65 | + id = NULL, |
| 66 | + states = NULL |
| 67 | +) { |
| 68 | + if(!inherits(onClick,'JS_EVAL')) { |
| 69 | + stop("onClick needs to be a returned value from a JS() call") |
| 70 | + } |
| 71 | + if(!is.null(states) && !inherits(states,'leaflet_easybutton_state_list')) { |
| 72 | + stop("states needs to be a returned value from a easyButtonStateList() call") |
| 73 | + } |
| 74 | + structure(list( |
| 75 | + icon = as.character(icon), |
| 76 | + title = as.character(title), |
| 77 | + onClick = onClick, |
| 78 | + position = position, |
| 79 | + id = id, |
| 80 | + states = states |
| 81 | + ), |
| 82 | + class='leaflet_easybutton') |
| 83 | +} |
| 84 | + |
| 85 | +#' Creates a list of easy buttons. |
| 86 | +#' @param ... icons created from \code{\link{easyButton}()} |
| 87 | +#' @export |
| 88 | +easyButtonList = function(...) { |
| 89 | + res = structure( |
| 90 | + list(...), |
| 91 | + class = "leaflet_easybutton_list" |
| 92 | + ) |
| 93 | + cls = unlist(lapply(res, inherits, 'leaflet_easybutton')) |
| 94 | + if (any(!cls)) |
| 95 | + stop('Arguments passed to easyButtonList() must be icon objects returned from easyButton()') |
| 96 | + res |
| 97 | +} |
| 98 | + |
| 99 | +#' Add a EasyButton on the map |
| 100 | +#' see \url{https://github.com/CliffCloud/Leaflet.EasyButton} |
| 101 | +#' |
| 102 | +#' @param map a map widget object |
| 103 | +#' @param button the button object created with \code{\link{easyButton}} |
| 104 | +#' @examples |
| 105 | +#' library(leaflet) |
| 106 | +#' |
| 107 | +#' leaf <- leaflet() %>% |
| 108 | +#' addTiles() %>% |
| 109 | +#' addEasyButton(easyButton( |
| 110 | +#' icon = htmltools::span(class='star','★'), |
| 111 | +#' onClick = JS("function(btn, map){ map.setZoom(1);}"))) |
| 112 | +#' |
| 113 | +#' @export |
| 114 | +addEasyButton <- function( |
| 115 | + map, |
| 116 | + button = easyButton() |
| 117 | +) { |
| 118 | + |
| 119 | + if(!inherits(button,'leaflet_easybutton')) { |
| 120 | + stop('button should be created with easyButton()') |
| 121 | + } |
| 122 | + |
| 123 | + map$dependencies <- c(map$dependencies, leafletEasyButtonDependencies()) |
| 124 | + |
| 125 | + # Add dependencies for various icon libs if required. |
| 126 | + if(is.null(button$states)) { |
| 127 | + if(grepl('fa-',button$icon)) |
| 128 | + map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies()) |
| 129 | + if(grepl('glyphicon-',button$icon)) |
| 130 | + map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies()) |
| 131 | + if(grepl('ion-',button$icon)) |
| 132 | + map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies()) |
| 133 | + } else { |
| 134 | + if(any(sapply(button$states,function(x) grepl('fa-',x$icon)))) |
| 135 | + map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies()) |
| 136 | + if(any(sapply(button$states,function(x) grepl('glyphicon-',x$icon)))) |
| 137 | + map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies()) |
| 138 | + if(any(sapply(button$states,function(x) grepl('ion-',x$icon)))) |
| 139 | + map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies()) |
| 140 | + } |
| 141 | + |
| 142 | + invokeMethod( |
| 143 | + map, |
| 144 | + getMapData(map), |
| 145 | + 'addEasyButton', |
| 146 | + button |
| 147 | + ) |
| 148 | +} |
| 149 | + |
| 150 | +#' Add a easyButton bar on the map |
| 151 | +#' see \url{https://github.com/CliffCloud/Leaflet.EasyButton} |
| 152 | +#' |
| 153 | +#' @param map a map widget object |
| 154 | +#' @param buttons the buttons object created with \code{\link{easyButtonList}} |
| 155 | +#' @param position topleft|topright|bottomleft|bottomright |
| 156 | +#' @param id id for the button bar |
| 157 | +#' @examples |
| 158 | +#' library(leaflet) |
| 159 | +#' |
| 160 | +#' leaf <- leaflet() %>% |
| 161 | +#' addTiles() %>% |
| 162 | +#' addEasyButtonBar(easyButtonList( |
| 163 | +#' easyButton( |
| 164 | +#' icon = htmltools::span(class='star','★'), |
| 165 | +#' onClick = JS("function(btn, map){ alert("Button 1");}")), |
| 166 | +#' easyButton( |
| 167 | +#' icon = htmltools::span(class='star','⌖'), |
| 168 | +#' onClick = JS("function(btn, map){ alert("Button 2");}")))) |
| 169 | +#' |
| 170 | +#' |
| 171 | +#' @export |
| 172 | +addEasyButtonBar <- function( |
| 173 | + map, |
| 174 | + buttons, |
| 175 | + position = 'topleft', |
| 176 | + id = NULL |
| 177 | +) { |
| 178 | + if(!inherits(buttons,'leaflet_easybutton_list')) { |
| 179 | + stop('button should be created with easyButtonList()') |
| 180 | + } |
| 181 | + |
| 182 | + map$dependencies <- c(map$dependencies, leafletEasyButtonDependencies()) |
| 183 | + |
| 184 | + # Add dependencies for various icon libs if required. |
| 185 | + for(button in buttons) { |
| 186 | + if(is.null(button$states)) { |
| 187 | + if(grepl('fa-',button$icon)) |
| 188 | + map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies()) |
| 189 | + if(grepl('glyphicon-',button$icon)) |
| 190 | + map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies()) |
| 191 | + if(grepl('ion-',button$icon)) |
| 192 | + map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies()) |
| 193 | + } else { |
| 194 | + if(any(sapply(button$states,function(x) grepl('fa-',x$icon)))) |
| 195 | + map$dependencies <- c(map$dependencies, leafletAmFontAwesomeDependencies()) |
| 196 | + if(any(sapply(button$states,function(x) grepl('glyphicon-',x$icon)))) |
| 197 | + map$dependencies <- c(map$dependencies, leafletAmBootstrapDependencies()) |
| 198 | + if(any(sapply(button$states,function(x) grepl('ion-',x$icon)))) |
| 199 | + map$dependencies <- c(map$dependencies, leafletAmIonIconDependencies()) |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + invokeMethod( |
| 204 | + map, |
| 205 | + getMapData(map), |
| 206 | + 'addEasyButtonBar', |
| 207 | + buttons, |
| 208 | + position, |
| 209 | + id |
| 210 | + ) |
| 211 | +} |
| 212 | + |
0 commit comments