forked from r-spatial/mapview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.R
More file actions
189 lines (167 loc) · 5.83 KB
/
Copy pathsync.R
File metadata and controls
189 lines (167 loc) · 5.83 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
186
187
188
189
#' View two or more (possibly synchronised) mapview or leaflet maps
#'
#' @description
#' These functions are deprecated.
#' Please use leafsync::\code{\link[leafsync]{sync}} and
#' leafsync::\code{\link[leafsync]{latticeView}} instead.
#'
#' @param ... any number of mapview or leaflet objects or a list thereof
#' @param ncol how many columns should be plotted
#' @param sync whether to synchronise zoom and pan for certain elements.
#' Possible values are "all" (default) to sync all maps, "none" to disable
#' synchronisation or a list of panel numbers, e.g. \code{list(c(1, 3), c(2, 4))}
#' will synchronise panels 1 & 3 and panels 2 & 4. Panels are drawn from top right
#' to bottom left.
#' @param sync.cursor whether to show cursor position in synced panels (default TRUE).
#' @param no.initial.sync whether to sync the initial view (default TRUE).
#'
#' @export latticeView
#' @name latticeView
#' @aliases latticeView
#'
latticeView <- function(...,
ncol = 2,
sync = "none",
sync.cursor = FALSE,
no.initial.sync = TRUE) {
.Deprecated(new = "leafsync::latticeView", package = "mapview",
old = "mapview::latticeView")
## convert all ... objects to list or extract list if list was passed
ls <- list(...)
if (length(ls) == 1) ls <- ls[[1]]
for (i in seq(ls)) {
if (inherits(ls[[i]], "mapview")) ls[[i]] <- mapview2leaflet(ls[[i]])
if(length(ls[[i]]$dependencies) == 0){
ls[[i]]$dependencies = list()
}
#ls[[i]]$dependencies[[length(ls[[i]]$dependencies) + 1]] <- sync_dep
# give a "unique" id to each leaflet map for lookup
if(is.null(ls[[i]]$elementId)){
# use unique same id generator as htmlwidgets
# https://github.com/ramnathv/htmlwidgets/blob/master/R/htmlwidgets.R#L165
ls[[i]]$elementId <- paste("htmlwidget", as.integer(stats::runif(1, 1, 10000)), sep="-")
}
}
## calculate div width depending on ncol and set div style
wdth <- paste0("width:", round(1 / ncol * 100, 0) - 1, "%;")
styl <- paste0("display:inline;",
wdth,
"float:left;border-style:solid;border-color:#BEBEBE;border-width:1px 1px 1px 1px;")
## htmltools stuff ... ?
tg <- lapply(seq(ls), function(i) {
htmltools::tags$div(style = styl, ls[[i]])
})
## string operations for syncing, depending on sync argument
## initialize sync_string as empty
sync_strng <- ""
if(!is.list(sync) && sync=="all"){
sync = list(seq(ls))
}
if (is.list(sync)) {
for (i in seq(sync)) {
synci <- sync[[i]]
sync_grid <- expand.grid(synci,synci,KEEP.OUT.ATTRS=FALSE)
sync_strng <- c(sync_strng,apply(
sync_grid,
MARGIN=1,
function(combo){
# don't sync to self
if(combo[1] != combo[2]){
return(sprintf(
"leaf_widgets['%s'].sync(leaf_widgets['%s'],{syncCursor: %s, noInitialSync: %s});",
ls[[combo[1]]]$elementId,
ls[[combo[2]]]$elementId,
tolower(as.logical(sync.cursor)),
tolower(as.logical(no.initial.sync))
))
}
return("")
}
))
}
}
sync_strng <- paste0(sync_strng,collapse="\n")
tl <- htmltools::attachDependencies(
htmltools::tagList(
tg,
htmlwidgets::onStaticRenderComplete(
paste0('var leaf_widgets = {};
Array.prototype.map.call(
document.querySelectorAll(".leaflet"),
function(ldiv){
if (HTMLWidgets.find("#" + ldiv.id) && HTMLWidgets.find("#" + ldiv.id).getMap()) {
leaf_widgets[ldiv.id] = HTMLWidgets.find("#" + ldiv.id).getMap();
}
}
);
',
sync_strng
)
)
),
dependencyLeafletsync()
)
return(htmltools::browsable(tl))
}
#' @describeIn latticeView alias for ease of typing
#' @aliases latticeview
#' @export latticeview
latticeview <- function(...) {
.Deprecated(new = "leafsync::latticeview", package = "mapview",
old = "mapview::latticeview")
latticeView(...)
}
#' @describeIn latticeView convenience function for syncing maps
#' @aliases sync
#' @export sync
sync <- function(...,
ncol = 2,
sync = "all",
sync.cursor = TRUE,
no.initial.sync = TRUE) {
.Deprecated(new = "leafsync::sync", package = "leafsync",
old = "mapview::sync")
latticeView(...,
ncol = ncol,
sync = sync,
sync.cursor = sync.cursor,
no.initial.sync = no.initial.sync)
}
# Reduce(paste0, sapply(seq(ls), function(i) {
# first <- do.call(c, lapply(seq(ls), function(j) {
# paste0("leaf_widgets[", j - 1, "]")
# }))
# paste0(first, ".sync(leaf_widgets[", i - 1, "]);")
# }))
# ## more htmltools stuff... ??
# tl <- htmltools::tagList(
# # htmltools::tags$head(htmltools::tags$script(
# # type="text/javascript",
# # src="https://cdn.rawgit.com/turban/Leaflet.Sync/master/L.Map.Sync.js"
# # )),
# tg,
# htmlwidgets::onStaticRenderComplete(
# paste0('var leaf_widgets = Array.prototype.map.call(
# document.querySelectorAll(".leaflet"),
# function(ldiv){
# return HTMLWidgets.find("#" + ldiv.id).getMap();
# }
# );',
# sync_strng
# )
# )
# )
# htmltools::tags$head(htmltools::tags$script(
# type="text/javascript",
# src="https://cdn.rawgit.com/turban/Leaflet.Sync/master/L.Map.Sync.js"
# )),
# Provide Leaflet.sync Dependency
dependencyLeafletsync <- function(){
htmltools::htmlDependency(
name = "Leaflet.Sync"
,version = "0.0.5"
,src = c(file = system.file("htmlwidgets/lib/Leaflet.Sync",
package = "mapview"))
,script = "L.Map.Sync.js"
)
}