-
Notifications
You must be signed in to change notification settings - Fork 505
Expand file tree
/
Copy pathplugin-easybutton.R
More file actions
185 lines (174 loc) · 5.56 KB
/
plugin-easybutton.R
File metadata and controls
185 lines (174 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
leafletEasyButtonDependencies <- function() {
list(
htmltools::htmlDependency(
"leaflet-easybutton",
"2.4.0",
"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 a unique name for the state
#' @seealso [easyButton()]
#' @describeIn easyButton state of an easyButton.
#' @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")
}
#' Creates an easy button.
#' @seealso <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 = NULL,
title = NULL,
onClick = NULL,
position = "topleft",
id = NULL,
states = NULL
) {
if (!missing(onClick) && !inherits(onClick, "JS_EVAL")) {
stop("onClick needs to be a returned value from a JS() call")
}
if (!is.null(states) && ! (
inherits(states, "list") &&
all(sapply(states, function(x) inherits(x, "leaflet_easybutton_state"))))) {
stop("states needs to be a list() of easyButton instances")
}
structure(filterNULL(list(
icon = as.character(icon),
title = as.character(title),
onClick = onClick,
position = position,
id = id,
states = states
)),
class = "leaflet_easybutton")
}
#' Add a EasyButton on the map
#' see <https://github.com/CliffCloud/Leaflet.EasyButton>
#'
#' @param map a map widget object
#' @param button the button object created with [easyButton()]
#' @examples
#' leaf <- leaflet() %>%
#' addTiles() %>%
#' addEasyButton(easyButton(
#' icon = htmltools::span(class = "star", htmltools::HTML("★")),
#' onClick = JS("function(btn, map){ map.setZoom(1);}")))
#' leaf
#'
#' @describeIn easyButton add an EasyButton to the map
#' @export
addEasyButton <- function(
map,
button
) {
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 <https://github.com/CliffCloud/Leaflet.EasyButton>
#'
#' @param ... a list of buttons created with [easyButton()]
#' @seealso [addEasyButton()]
#' @examples
#' leaf <- leaflet() %>%
#' addTiles() %>%
#' addEasyButtonBar(
#' easyButton(
#' icon = htmltools::span(class = "star", htmltools::HTML("★")),
#' onClick = JS("function(btn, map){ alert(\"Button 1\");}")),
#' easyButton(
#' icon = htmltools::span(class = "star", htmltools::HTML("⌖")),
#' onClick = JS("function(btn, map){ alert(\"Button 2\");}")))
#' leaf
#'
#'
#' @describeIn easyButton add an EasyButton to the map
#' @export
addEasyButtonBar <- function(
map,
...,
position = "topleft",
id = NULL
) {
buttons <- list(...)
if (!length(buttons) >= 1 ||
!all(sapply(buttons, function(x) inherits(x, "leaflet_easybutton")))) {
stop("need buttons created with easyButton()")
}
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
)
}