Skip to content

Commit 8b4fac1

Browse files
committed
1 parent 7a4fec6 commit 8b4fac1

File tree

13 files changed

+913
-0
lines changed

13 files changed

+913
-0
lines changed

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export(addAwesomeMarkers)
1414
export(addCircleMarkers)
1515
export(addCircles)
1616
export(addControl)
17+
export(addEasyButton)
18+
export(addEasyButtonBar)
1719
export(addGeoJSON)
1820
export(addGraticule)
1921
export(addLabelOnlyMarkers)
@@ -53,6 +55,10 @@ export(colorNumeric)
5355
export(colorQuantile)
5456
export(createLeafletMap)
5557
export(dispatch)
58+
export(easyButton)
59+
export(easyButtonList)
60+
export(easyButtonState)
61+
export(easyButtonStateList)
5662
export(fitBounds)
5763
export(hideGroup)
5864
export(iconList)

R/plugin-easybutton.R

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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','&starf;'),
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','&starf;'),
165+
#' onClick = JS("function(btn, map){ alert("Button 1");}")),
166+
#' easyButton(
167+
#' icon = htmltools::span(class='star','&target;'),
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+

inst/examples/easyButton.R

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
library(leaflet)
2+
3+
#' Add two easy buttons
4+
#' first to set zoom level to 1
5+
#' second to find your self
6+
leaflet() %>% addTiles() %>%
7+
addEasyButton(easyButton(
8+
icon='fa-globe', title='Zoom to Level 1',
9+
onClick=JS("function(btn, map){ map.setZoom(1);}"))) %>%
10+
addEasyButton(easyButton(
11+
icon='fa-crosshairs', title='Locate Me',
12+
onClick=JS("function(btn, map){ map.locate({setView: true});}")))
13+
14+
15+
leaflet() %>% addTiles() %>%
16+
addMarkers(data=quakes,
17+
clusterOptions = markerClusterOptions(),
18+
clusterId = 'quakesCluster') %>%
19+
addEasyButton(easyButton(
20+
states = easyButtonStateList(
21+
easyButtonState(
22+
stateName='unfrozen-markers',
23+
icon='ion-toggle',
24+
title='Freeze Clusters',
25+
onClick = JS("
26+
function(btn, map) {
27+
var clusterManager =
28+
map.layerManager.getLayer('cluster',
29+
'quakesCluster');
30+
clusterManager.freezeAtZoom();
31+
btn.state('frozen-markers');
32+
}")
33+
),
34+
easyButtonState(
35+
stateName='frozen-markers',
36+
icon='ion-toggle-filled',
37+
title='UnFreeze Clusters',
38+
onClick = JS("
39+
function(btn, map) {
40+
var clusterManager =
41+
map.layerManager.getLayer('cluster',
42+
'quakesCluster');
43+
clusterManager.unfreeze();
44+
btn.state('unfrozen-markers');
45+
}")
46+
)
47+
)
48+
))
49+
50+
#' Add two easy buttons in a bar
51+
#' first to set zoom level to 1
52+
#' second to find your self
53+
leaflet() %>% addTiles() %>%
54+
addEasyButtonBar(easyButtonList(
55+
easyButton(
56+
icon='fa-globe', title='Zoom to Level 1',
57+
onClick=JS("function(btn, map){ map.setZoom(1);}")),
58+
easyButton(
59+
icon='fa-crosshairs', title='Locate Me',
60+
onClick=JS("function(btn, map){ map.locate({setView: true});}"))
61+
))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
getEasyButton = function(button) {
2+
3+
var options = {};
4+
5+
options.position = button.position;
6+
7+
// only add ID if provided
8+
if(button.id) {
9+
options.id = button.id;
10+
}
11+
12+
// if custom states provided use that
13+
// else use provided icon and onClick
14+
if(button.states) {
15+
options.states = button.states;
16+
return L.easyButton(options);
17+
} else {
18+
return L.easyButton(button.icon, button.onClick,
19+
button.title, options );
20+
}
21+
};
22+
23+
LeafletWidget.methods.addEasyButton = function(button) {
24+
getEasyButton(button).addTo(this);
25+
};
26+
27+
LeafletWidget.methods.addEasyButtonBar = function(buttons, position, id) {
28+
29+
var options = {};
30+
31+
options.position = position;
32+
33+
// only add ID if provided
34+
if(id) {
35+
options.id = id;
36+
}
37+
38+
var easyButtons = [];
39+
for(var i=0; i < buttons.length; i++) {
40+
easyButtons[i] = getEasyButton(buttons[i]);
41+
}
42+
L.easyBar(easyButtons).addTo(this);
43+
44+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (C) 2014 Daniel Montague
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

0 commit comments

Comments
 (0)