Skip to content

Commit c09299e

Browse files
committed
Merged Joe's patch for backward compatibility
1 parent 633778c commit c09299e

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
node_modules
55
inst/htmlwidgets/sources
66
*.swp
7+
inst/examples/rsconnect
8+
inst/examples/*.html

R/leaflet.R

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,29 @@ leaflet = function(data = NULL, width = NULL, height = NULL,
4545
defaultHeight = 400,
4646
padding = padding,
4747
browser.fill = TRUE
48-
)
48+
),
49+
preRenderHook = function(widget) {
50+
if (!is.null(widget$jsHooks$render)) {
51+
widget$jsHooks$render <- lapply(widget$jsHooks$render, function(hook) {
52+
if (is.list(hook)) {
53+
hook$code <- sprintf(hookWrapperTemplate, paste(hook$code, collapse = "\n"))
54+
} else if (is.character(hook)) {
55+
hook <- sprintf(hookWrapperTemplate, paste(hook, collapse = "\n"))
56+
} else {
57+
stop("Unknown hook class ", class(hook))
58+
}
59+
hook
60+
})
61+
}
62+
widget
63+
}
4964
)
5065
}
5166

67+
hookWrapperTemplate <- "function(el, x, data) {
68+
return (%s).call(this.getMap(), el, x, data);
69+
}"
70+
5271
getMapData = function(map) {
5372
attr(map$x, "leafletData", exact = TRUE)
5473
}

inst/examples/minimap.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ m %>%
3434
toggleDisplay = T) %>%
3535
htmlwidgets::onRender("
3636
function(el, x) {
37-
var myMap = this.getMap();
37+
var myMap = this;
3838
myMap.on('baselayerchange',
3939
function (e) {
4040
myMap.minimap.changeLayer(L.tileLayer.provider(e.name));
@@ -72,7 +72,7 @@ leaflet() %>% addTiles() %>%
7272
addMiniMap() %>%
7373
htmlwidgets::onRender("
7474
function(el, t) {
75-
var myMap = this.getMap();
75+
var myMap = this;
7676
7777
var pubs = myMap.layerManager._byGroup.pubs;
7878
var pubs2 = new L.FeatureGroup();
@@ -106,7 +106,7 @@ m %>%
106106
toggleDisplay = T) %>%
107107
htmlwidgets::onRender("
108108
function(el, t) {
109-
var myMap = this.getMap();
109+
var myMap = this;
110110
111111
var pubs = myMap.layerManager._byGroup.pubs;
112112
var pubs2 = new L.FeatureGroup();

inst/examples/proj4Leaflet.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ leaflet(mapOptions = list(crs=crs(crsClass='L.CRS.Simple'), minZoom= -5)) %>%
5757
setMaxBounds(bounds[1], bounds[2], bounds[3], bounds[4]) %>%
5858
htmlwidgets::onRender("
5959
function(el, t) {
60-
var myMap = this.getMap();
60+
var myMap = this;
6161
var bounds = myMap.getBounds();
6262
var image = new L.ImageOverlay(
6363
'http://leafletjs.com/examples/crs-simple/uqm_map_full.png',
@@ -108,7 +108,7 @@ leaflet(mapOptions =
108108
fillOpacity: 1,
109109
};
110110
111-
var myMap = this.getMap();
111+
var myMap = this;
112112
var layers = myMap._layers;
113113
for(var i in layers) {
114114
var layer = layers[i];

0 commit comments

Comments
 (0)