Skip to content

Commit

Permalink
contextmenu init
Browse files Browse the repository at this point in the history
  • Loading branch information
trafficonese committed Aug 25, 2020
1 parent 0a067f8 commit cd28209
Show file tree
Hide file tree
Showing 23 changed files with 1,723 additions and 1 deletion.
12 changes: 12 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

S3method("[",leaflet_mapkey_icon_set)
export(addAntpath)
export(addContextmenu)
export(addEasyprint)
export(addGIBS)
export(addHeightgraph)
export(addHexbin)
export(addHistory)
export(addItemContextmenu)
export(addMapkeyMarkers)
export(addOpenweatherCurrent)
export(addOpenweatherTiles)
Expand All @@ -30,27 +32,36 @@ export(goBackHistory)
export(goForwardHistory)
export(heightgraphOptions)
export(hexbinOptions)
export(hideContextmenu)
export(hideHexbin)
export(historyOptions)
export(insertItemContextmenu)
export(makeMapkeyIcon)
export(mapkeyIconList)
export(mapkeyIcons)
export(mapmenuItems)
export(markermenuItems)
export(menuItem)
export(openSidebar)
export(openweatherCurrentOptions)
export(openweatherOptions)
export(playbackOptions)
export(reachabilityOptions)
export(removeAntpath)
export(removeEasyprint)
export(removeItemContextmenu)
export(removePlayback)
export(removeReachability)
export(removeSidebar)
export(removeSidebyside)
export(removeTimeslider)
export(removeVelocity)
export(removeallItemsContextmenu)
export(setDate)
export(setDisabledContextmenu)
export(setOptionsVelocity)
export(setTransparent)
export(showContextmenu)
export(showHexbin)
export(sidebar_pane)
export(sidebar_tabs)
Expand All @@ -65,3 +76,4 @@ importFrom(htmltools,tags)
importFrom(magrittr,"%>%")
importFrom(utils,adist)
importFrom(utils,globalVariables)
importFrom(utils,packageVersion)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# leaflet.extras2 (development version)

* Included [Leaflet Contextmenu](https://github.com/aratcliffe/Leaflet.contextmenu) plugin
* Included [Leaflet TimeSlider](https://github.com/dwilhelm89/LeafletSlider) plugin
* `addWMS` gained the `layerId` argument and works like `leaflet::addWMSTiles` except for the `popupOptions`
* `Side-by-Side` doesn't propagate click events when dragging. Thanks to `f905a47` of [#23](https://github.com/digidem/leaflet-side-by-side/pull/23)
Expand Down
208 changes: 208 additions & 0 deletions R/contextmenu.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
contextmenuDependency <- function() {
list(
htmltools::htmlDependency(
"lfx-contextmenu", version = "1.0.0",
src = system.file("htmlwidgets/lfx-contextmenu", package = "leaflet.extras2"),
script = c("leaflet.contextmenu.js",
"leaflet.contextmenu-bindings.js"),
stylesheet = "leaflet.contextmenu.css"
)
)
}

#' Add contextmenu Plugin
#'
#' Add a contextmenu to the map or markers/vector layers.
#' @param map a map widget object created from \code{\link[leaflet]{leaflet}}
#' @family Contextmenu Functions
#' @references \url{https://github.com/aratcliffe/Leaflet.contextmenu}
#' @export
#' @return A leaflet map object
#'
#' @details
#' This function is only used to include the required JavaScript and CSS
#' bindings and to set up some Shiny event handlers.
#'
#' \subsection{Contextmenu initialization}{
#' The contextmenu for
#' \itemize{
#' \item {the \strong{map} must be defined in \code{\link[leaflet]{leafletOptions}}.}
#' \item {the \strong{markers/vector layers} must be defined in \code{\link[leaflet]{markerOptions}}
#' or \code{\link[leaflet]{pathOptions}}.}
#' }
#' }
#'
#' \subsection{Contextmenu selection}{
#' When a contextmenu is selected, a Shiny input with the ID \code{"MAPID_contextmenu_select"}
#' is set (`MAPID` refers to the map's id).
#'
#' If the selected contextmenu item is triggered from:
#' \itemize{
#' \item {the \strong{map}, the returned list containts the \code{text} of the item.}
#' \item {the \strong{markers}, the returned list also contains the
#' \code{layerId}, \code{group}, \code{lat}, \code{lng} and \code{label}.}
#' \item {the \strong{vector layers}, the returned list also contains the
#' \code{layerId}, \code{group} and \code{label}.}
#' }
#' }
#'
#' @examples
#' library(leaflet)
#' leaflet(options = leafletOptions(
#' contextmenu = TRUE,
#' contextmenuWidth = 200,
#' contextmenuItems =
#' mapmenuItems(
#' menuItem("Zoom Out", "function(e) {this.zoomOut()}", disabled=FALSE),
#' "-",
#' menuItem("Zoom In", "function(e) {this.zoomIn()}")))) %>%
#' addTiles(group = "base") %>%
#' addContextmenu() %>%
#' addMarkers(data = breweries91, label = ~brewery,
#' layerId = ~founded, group = "marker",
#' options = markerOptions(
#' contextmenu = TRUE,
#' contextmenuWidth = 200,
#' contextmenuItems =
#' markermenuItems(
#' menuItem(text = "Show Marker Coords",
#' callback = "function(e) {alert(e.latlng);}",
#' index = 1)
#' )
#' ))
#'
addContextmenu <- function(map) {
map$dependencies <- c(map$dependencies, contextmenuDependency())
leaflet::invokeMethod(map, NULL, "addContextmenu")
}

#' showContextmenu
#'
#' Open the contextmenu at certain lat/lng-coordinates
#' @family Contextmenu Functions
#' @inheritParams leaflet::addMarkers
#' @return A leaflet map object
#' @export
showContextmenu <- function(map, lat=NULL, lng=NULL, data=leaflet::getMapData(map)) {
pts <- leaflet::derivePoints(data, lng, lat, missing(lng), missing(lat), "showContextmenu")
leaflet::invokeMethod(map, NULL, "showContextmenu", pts)
}

#' hideContextmenu
#'
#' Hide the contextmenu
#' @family Contextmenu Functions
#' @inheritParams addContextmenu
#' @return A leaflet map object
#' @export
hideContextmenu <- function(map) {
leaflet::invokeMethod(map, NULL, "hideContextmenu")
}

#' addItemContextmenu
#'
#' Add a new contextmenu menu item
#' @family Contextmenu Functions
#' @inheritParams addContextmenu
#' @param option new menu item to add
#' @return A leaflet map object
#' @export
addItemContextmenu <- function(map, option) {
if (utils::packageVersion("leaflet") < "2.0.4") {
warning("The `addItemContextmenu` function requires leaflet `2.0.4` to correctly register callbacks.")
}
leaflet::invokeMethod(map, NULL, "addItemContextmenu", option)
}
# remotes::install_github("rstudio/leaflet#696")

#' insertItemContextmenu
#'
#' Insert a new contextmenu menu item at a specific index
#' @family Contextmenu Functions
#' @inheritParams addItemContextmenu
#' @inheritParams removeItemContextmenu
#' @return A leaflet map object
#' @export
insertItemContextmenu <- function(map, option, index) {
if (utils::packageVersion("leaflet") < "2.0.4") {
warning("The `insertItemContextmenu` function requires leaflet `2.0.4` to correctly register callbacks.")
}
leaflet::invokeMethod(map, NULL, "insertItemContextmenu", option, index)
}
# remotes::install_github("rstudio/leaflet#696")

#' removeItemContextmenu
#'
#' Remove a contextmenu item by index.
#' @family Contextmenu Functions
#' @inheritParams addContextmenu
#' @param index Index of the contextmenu. (NOTE: Since the index is passed to JavaScript,
#' it is zero-based)
#' @return A leaflet map object
#' @export
removeItemContextmenu <- function(map, index) {
leaflet::invokeMethod(map, NULL, "removeItemContextmenu", index)
}

#' setDisabledContextmenu
#'
#' Enable/Disable a contextmenu item by index.
#' @family Contextmenu Functions
#' @inheritParams removeItemContextmenu
#' @param disabled Set to \code{TRUE} to disable the element and \code{FALSE}
#' to enable it. Default is \code{TRUE}
#' @return A leaflet map object
#' @export
setDisabledContextmenu <- function(map, index, disabled=TRUE) {
leaflet::invokeMethod(map, NULL, "setDisabledContextmenu", index, disabled)
}

#' removeallItemsContextmenu
#'
#' Remove all contextmenu items from the map.
#' @family Contextmenu Functions
#' @inheritParams removeItemContextmenu
#' @return A leaflet map object
#' @export
removeallItemsContextmenu <- function(map) {
leaflet::invokeMethod(map, NULL, "removeallItemsContextmenu")
}



#' menuItem
#' @param text The label to use for the menu item
#' @param callback A callback function to be invoked when the menu item is
#' clicked. The callback is passed an object with properties identifying the
#' location the menu was opened at: \code{latlng}, \code{layerPoint} and \code{containerPoint}.
#' The callback-function must be valid JavaScript and will be wrapped in
#' \code{\link[leaflet]{JS}}.
#' @param ... For further options please visit \url{https://github.com/aratcliffe/Leaflet.contextmenu}
#' @family Contextmenu Functions
#' @return A contextmenu item list
#' @export
menuItem <- function(text, callback=NULL, ...) {
list(text=text,
callback=leaflet::JS(callback),
...)
}

#' mapmenuItems
#' @param ... contextmenu item/s
#' @family Contextmenu Functions
#' @return A list of \code{menuItem} for the map
#' @export
mapmenuItems <- function(...) {
list(...)
}

#' markermenuItems
#' @param ... contextmenu item/s
#' @family Contextmenu Functions
#' @return A list of \code{menuItem} for markers
#' @export
markermenuItems <- function(...) {
list(list(...))
}


2 changes: 1 addition & 1 deletion R/leaflet.extras2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @importFrom magrittr %>%
#' @import leaflet
#' @importFrom htmltools htmlDependency tagGetAttribute tags tagList
#' @importFrom utils globalVariables adist
#' @importFrom utils globalVariables adist packageVersion
#'
#' @name leaflet.extras2
#' @docType package
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ remotes::install_github('trafficonese/leaflet.extras2')
Plugins integrated so far ...

- [Ant Path](https://github.com/rubenspgcavalcante/leaflet-ant-path)
- [Contextmenu](https://github.com/aratcliffe/Leaflet.contextmenu)
- [Easy Print](https://github.com/rowanwins/leaflet-easyPrint)
- [GIBS](https://github.com/aparshin/leaflet-GIBS)
- [Heightgraph](https://github.com/GIScience/Leaflet.Heightgraph)
Expand Down
Loading

0 comments on commit cd28209

Please sign in to comment.