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

add new draw options, fix for removeDrawToolbar #184

Closed
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export(editToolbarOptions)
export(enableMeasurePath)
export(enableTileCaching)
export(gpsOptions)
export(handlersOptions)
export(leafletExtrasDependencies)
export(legendOptions)
export(makePulseIcon)
Expand All @@ -80,6 +81,7 @@ export(searchOptions)
export(selectedPathOptions)
export(setMapWidgetStyle)
export(suspendScroll)
export(toolbarOptions)
export(weatherIconList)
export(weatherIcons)
import(leaflet)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# leaflet.extras 1.1.0

## New Features
- The `addDrawToolbar` offers two new options: `handlersOptions` and `toolbarOptions`, with which you can customize the drawing toolbar and the tooltips.

# leaflet.extras 1.0.0

## leaflet.js
Expand Down
13 changes: 11 additions & 2 deletions R/draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ drawDependencies <- function() {
#' @param circleMarkerOptions See \code{\link{drawCircleMarkerOptions}}(). Set to FALSE to disable circle marker drawing.
#' @param editOptions By default editing is disable. To enable editing pass \code{\link{editToolbarOptions}}().
#' @param singleFeature When set to TRUE, only one feature can be drawn at a time, the previous ones being removed.
#' @param toolbar See \code{\link{toolbarOptions}}. Set to \code{NULL} to take Leaflets default values.
#' @param handlers See \code{\link{handlersOptions}}. Set to \code{NULL} to take Leaflets default values.
#' @export
#' @rdname draw
#' @examples
Expand Down Expand Up @@ -54,13 +56,18 @@ addDrawToolbar <- function(
markerOptions = drawMarkerOptions(),
circleMarkerOptions = drawCircleMarkerOptions(),
editOptions = FALSE,
singleFeature = FALSE
singleFeature = FALSE,
toolbar = NULL,
handlers = NULL
) {

if (!is.null(targetGroup) && !is.null(targetLayerId)) {
stop("To edit existing features either specify a targetGroup or a targetLayerId, but not both")
}

if (!inherits(toolbar, "list")) toolbar <- NULL
if (!inherits(handlers, "list")) handlers <- NULL

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

markerIconFunction <- NULL
Expand Down Expand Up @@ -89,7 +96,9 @@ addDrawToolbar <- function(
marker = markerOptions,
circlemarker = circleMarkerOptions,
singleFeature = singleFeature)),
edit = editOptions )
edit = editOptions,
toolbar = toolbar,
handlers = handlers)

leaflet::invokeMethod(map, leaflet::getMapData(map), "addDrawToolbar",
targetLayerId, targetGroup, options)
Expand Down
140 changes: 140 additions & 0 deletions R/drawOptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,143 @@ editToolbarOptions <- function(
allowIntersection = allowIntersection
))
}


#' Options for editing handlers
#' @description Customize tooltips for \code{\link{addDrawToolbar}}
#' @param polyline List of options for polyline tooltips.
#' @param polygon List of options for polygon tooltips.
#' @param rectangle List of options for rectangle tooltips.
#' @param circle List of options for circle tooltips.
#' @param marker List of options for marker tooltips.
#' @param circlemarker List of options for circlemarker tooltips.
#' @export
#' @examples \dontrun{
#' library(leaflet)
#' library(leaflet.extras)
#' leaflet() %>%
#' addTiles() %>%
#' addDrawToolbar(
#' handlers = handlersOptions(
#' polyline = list(tooltipStart = "Click It",
#' tooltipCont = "Keep going",
#' tooltipEnd = "Make it stop"),
#' ),
#' polylineOptions = T, rectangleOptions = F, circleOptions = F,
#' polygonOptions = F, markerOptions = F, circleMarkerOptions = F
#' )
#' }
handlersOptions <- function(
polyline = list(
error = "<strong>Error:</strong> shape edges cannot cross!",
tooltipStart = "Click to start drawing line.",
tooltipCont = "Click to start drawing line.",
tooltipEnd = "Click to start drawing line."),
polygon = list(
tooltipStart = "Click to start drawing shape.",
tooltipCont = "Click to start drawing shape.",
tooltipEnd = "Click to start drawing shape."),
rectangle = list(
tooltipStart = "Click and drag to draw rectangle."),
circle = list(
tooltipStart = "Click map to place circle marker.",
radius = "Radius"),
marker = list(
tooltipStart = "Click map to place marker."),
circlemarker = list(
tooltipStart = "Click and drag to draw circle.")
) {
leaflet::filterNULL(list(
polyline = list(
error = polyline$error,
tooltip = list(
start = polyline$tooltipStart,
cont = polyline$tooltipCont,
end = polyline$tooltipEnd
)),
polygon = list(
tooltip = list(
start = polygon$tooltipStart,
cont = polygon$tooltipCont,
end = polygon$tooltipEnd
)),
rectangle = list(tooltip = list(start = rectangle$tooltipStart)),
circle = list(radius = circle$radius,
tooltip = list(start = circle$tooltipStart)),
marker = list(tooltip = list(start = marker$tooltipStart)),
circlemarker = list(tooltip = list(start = circlemarker$tooltipStart))
))
}


#' Options for editing the toolbar
#' @description Customize the toolbar for \code{\link{addDrawToolbar}}
#' @param actions List of options for actions toolbar button.
#' @param finish List of options for finish toolbar button.
#' @param undo List of options for undo toolbar button.
#' @param buttons List of options for buttons toolbar button.
#' @export
#' @examples \dontrun{
#' library(leaflet)
#' library(leaflet.extras)
#' leaflet() %>%
#' addTiles() %>%
#' addDrawToolbar(
#' toolbar = toolbarOptions(
#' actions = list(text = "STOP"),
#' finish = list(text = "DONE"),
#' buttons = list(polyline = "Draw a sexy polyline",
#' rectangle = "Draw a gigantic rectangle",
#' circlemarker = "Make a nice circle"),
#' ),
#' polylineOptions = T, rectangleOptions = T,circleOptions = T,
#' polygonOptions = F, markerOptions = F, circleMarkerOptions = F
#' )
#' }
toolbarOptions <- function(
actions = list(
title = "Cancel drawing",
text = "Cancel"
),
finish = list(
title = "Finish drawing",
text = "Finish"
),
undo = list(
title = "Delete last point drawn",
text = "Delete last point"
),
buttons = list(
polyline = "Draw a polyline",
polygon = "Draw a polygon",
rectangle = "Draw a rectangle",
circle = "Draw a circle",
marker = "Draw a marker",
circlemarker = "Draw a circlemarker"
)
) {
leaflet::filterNULL(list(
actions = list(
title = actions$title,
text = actions$text
),
finish = list(
title = finish$title,
text = finish$text
),
undo = list(
title = undo$title,
text = undo$text
),
buttons = list(
polyline = buttons$polyline,
polygon = buttons$polygon,
rectangle = buttons$rectangle,
circle = buttons$circle,
marker = buttons$marker,
circlemarker = buttons$circlemarker
)
))
}


49 changes: 46 additions & 3 deletions inst/htmlwidgets/bindings/lfx-draw-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti
var map = this;

if(map.drawToolbar) {
map.drawToolbar.removeFrom(map);
delete map.drawToobar;
map.drawToolbar.remove(map);
delete map.drawToolbar;
}

// FeatureGroup that will hold our drawn shapes/markers
Expand Down Expand Up @@ -68,6 +68,49 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti
options.edit = editOptions;
}

// Set Toolbar / Handlers options if provided. Changes the default values.
if (options && options.toolbar) {
var rtool = options.toolbar;
var tooldef = L.drawLocal.draw.toolbar;
L.drawLocal.draw.toolbar.buttons.polygon = rtool.buttons.polygon ? rtool.buttons.polygon : tooldef.buttons.polygon;
L.drawLocal.draw.toolbar.buttons.polyline = rtool.buttons.polyline ? rtool.buttons.polyline : tooldef.buttons.polyline;
L.drawLocal.draw.toolbar.buttons.rectangle = rtool.buttons.rectangle ? rtool.buttons.rectangle : tooldef.buttons.rectangle;
L.drawLocal.draw.toolbar.buttons.circle = rtool.buttons.circle ? rtool.buttons.circle : tooldef.buttons.circle;
L.drawLocal.draw.toolbar.buttons.marker = rtool.buttons.marker ? rtool.buttons.marker : tooldef.buttons.marker;
L.drawLocal.draw.toolbar.buttons.circlemarker = rtool.buttons.circlemarker ? rtool.buttons.circlemarker : tooldef.buttons.circlemarker;

L.drawLocal.draw.toolbar.actions.title = rtool.actions.title ? rtool.actions.rectangle : tooldef.actions.title;
L.drawLocal.draw.toolbar.actions.text = rtool.actions.text ? rtool.actions.text : tooldef.actions.text;

L.drawLocal.draw.toolbar.finish.title = rtool.finish.title ? rtool.finish.rectangle : tooldef.finish.title;
L.drawLocal.draw.toolbar.finish.text = rtool.finish.text ? rtool.finish.text : tooldef.finish.text;

L.drawLocal.draw.toolbar.undo.title = rtool.undo.title ? rtool.undo.rectangle : tooldef.undo.title;
L.drawLocal.draw.toolbar.undo.text = rtool.undo.text ? rtool.undo.text : tooldef.undo.text;
}
if (options && options.handlers) {
var rhand = options.handlers;
var handldef = L.drawLocal.draw.handlers;
L.drawLocal.draw.handlers.circle.radius = rhand.circle.radius ? rhand.circle.radius : handldef.circle.radius;
L.drawLocal.draw.handlers.circle.tooltip.start = rhand.circle.tooltip.start ? rhand.circle.tooltip.start : handldef.circle.tooltip.start;

L.drawLocal.draw.handlers.circlemarker.tooltip.start = rhand.circlemarker.tooltip.start ? rhand.circlemarker.tooltip.start : handldef.circlemarker.tooltip.start;

L.drawLocal.draw.handlers.marker.tooltip.start = rhand.marker.tooltip.start ? rhand.marker.tooltip.start : handldef.marker.tooltip.start;

L.drawLocal.draw.handlers.polygon.tooltip.start = rhand.polygon.tooltip.start ? rhand.polygon.tooltip.start : handldef.polygon.tooltip.start;
L.drawLocal.draw.handlers.polygon.tooltip.cont = rhand.polygon.tooltip.cont ? rhand.polygon.tooltip.cont : handldef.polygon.tooltip.cont;
L.drawLocal.draw.handlers.polygon.tooltip.end = rhand.polygon.tooltip.end ? rhand.polygon.tooltip.end : handldef.polygon.tooltip.end;

L.drawLocal.draw.handlers.polyline.error = rhand.polyline.error ? rhand.polyline.error : handldef.polyline.error;
L.drawLocal.draw.handlers.polyline.tooltip.start = rhand.polyline.tooltip.start ? rhand.polyline.tooltip.start : handldef.polyline.tooltip.start;
L.drawLocal.draw.handlers.polyline.tooltip.cont = rhand.polyline.tooltip.cont ? rhand.polyline.tooltip.cont : handldef.polyline.tooltip.cont;
L.drawLocal.draw.handlers.polyline.tooltip.end = rhand.polyline.tooltip.end ? rhand.polyline.tooltip.end : handldef.polyline.tooltip.end;

L.drawLocal.draw.handlers.rectangle.tooltip.start = rhand.rectangle.tooltip.start ? rhand.rectangle.tooltip.start : handldef.rectangle.tooltip.start;
}

// Create new Drawing Control
map.drawToolbar = new L.Control.Draw(options);
map.drawToolbar.addTo(map);

Expand Down Expand Up @@ -194,7 +237,7 @@ LeafletWidget.methods.removeDrawToolbar = function(clearFeatures) {
var map = this;

if(map.drawToolbar) {
map.drawToolbar.removeFrom(map);
map.drawToolbar.remove(map);
delete map.drawToolbar;
}
if(map._editableFeatureGroupName && clearFeatures) {
Expand Down
Loading