Skip to content

Fix popups/Shiny-events for *Src-functions #46

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

Closed
wants to merge 4 commits 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
30 changes: 15 additions & 15 deletions R/glify-lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ addGlPolylinesSrc = function(map,
data = sf::st_sf(id = 1:length(geom), geometry = geom)

ell_args <- list(...)
fl_data = paste0(dir_data, "/", layerId, "_data.js")
pre = paste0('var data = data || {}; data["', layerId, '"] = ')
fl_data = paste0(dir_data, "/", group, "_data.js")
pre = paste0('var data = data || {}; data["', group, '"] = ')
writeLines(pre, fl_data)
jsonify_args = try(
match.arg(
Expand All @@ -199,7 +199,7 @@ addGlPolylinesSrc = function(map,
map$dependencies = c(
map$dependencies,
glifyDependenciesSrc(),
glifyDataAttachmentSrc(fl_data, layerId)
glifyDataAttachmentSrc(fl_data, group)
)

# color
Expand All @@ -211,24 +211,23 @@ addGlPolylinesSrc = function(map,
if (ncol(color) != 3) stop("only 3 column color matrix supported so far")
color = as.data.frame(color, stringsAsFactors = FALSE)
colnames(color) = c("r", "g", "b")

if (nrow(color) > 1) {
fl_color = paste0(dir_color, "/", layerId, "_color.js")
pre = paste0('var col = col || {}; col["', layerId, '"] = ')
fl_color = paste0(dir_color, "/", group, "_color.js")
pre = paste0('var col = col || {}; col["', group, '"] = ')
writeLines(pre, fl_color)
cat('[', jsonify::to_json(color), '];',
file = fl_color, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyColorAttachmentSrc(fl_color, layerId)
glifyColorAttachmentSrc(fl_color, group)
)

color = NULL
}

# popup
if (!is.null(popup)) {
if (!is.null(popup) && !isFALSE(popup)) {
htmldeps <- htmltools::htmlDependencies(popup)
if (length(htmldeps) != 0) {
map$dependencies = c(
Expand All @@ -237,30 +236,30 @@ addGlPolylinesSrc = function(map,
)
}
popup = makePopup(popup, data_orig)
fl_popup = paste0(dir_popup, "/", layerId, "_popup.js")
pre = paste0('var popup = popup || {}; popup["', layerId, '"] = ')
fl_popup = paste0(dir_popup, "/", group, "_popup.js")
pre = paste0('var popups = popups || {}; popups["', group, '"] = ')
writeLines(pre, fl_popup)
cat('[', jsonify::to_json(popup), '];',
file = fl_popup, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyPopupAttachmentSrc(fl_popup, layerId)
glifyPopupAttachmentSrc(fl_popup, group)
)

popup <- TRUE
}

# weight
if (length(unique(weight)) > 1) {
fl_weight = paste0(dir_weight, "/", layerId, "_weight.js")
pre = paste0('var wgt = wgt || {}; wgt["', layerId, '"] = ')
fl_weight = paste0(dir_weight, "/", group, "_weight.js")
pre = paste0('var wgt = wgt || {}; wgt["', group, '"] = ')
writeLines(pre, fl_weight)
cat('[', jsonify::to_json(weight), '];',
file = fl_weight, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyRadiusAttachmentSrc(fl_weight, layerId)
glifyRadiusAttachmentSrc(fl_weight, group)
)

weight = NULL
Expand All @@ -275,6 +274,7 @@ addGlPolylinesSrc = function(map,
, opacity
, group
, layerId
, popup
)

leaflet::expandLimits(
Expand Down
29 changes: 15 additions & 14 deletions R/glify-points.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ addGlPointsSrc = function(map,
crds = sf::st_coordinates(data)[, c(2, 1)]

ell_args <- list(...)
fl_data = paste0(dir_data, "/", layerId, "_data.js")
pre = paste0('var data = data || {}; data["', layerId, '"] = ')
fl_data = paste0(dir_data, "/", group, "_data.js")
pre = paste0('var data = data || {}; data["', group, '"] = ')
writeLines(pre, fl_data)
jsonify_args = try(
match.arg(
Expand All @@ -215,7 +215,7 @@ addGlPointsSrc = function(map,
map$dependencies = c(
map$dependencies,
glifyDependenciesSrc(),
glifyDataAttachmentSrc(fl_data, layerId)
glifyDataAttachmentSrc(fl_data, group)
)

# color
Expand All @@ -229,22 +229,22 @@ addGlPointsSrc = function(map,
colnames(fillColor) = c("r", "g", "b")

if (nrow(fillColor) > 1) {
fl_color = paste0(dir_color, "/", layerId, "_color.js")
pre = paste0('var col = col || {}; col["', layerId, '"] = ')
fl_color = paste0(dir_color, "/", group, "_color.js")
pre = paste0('var col = col || {}; col["', group, '"] = ')
writeLines(pre, fl_color)
cat('[', jsonify::to_json(fillColor), '];',
file = fl_color, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyColorAttachmentSrc(fl_color, layerId)
glifyColorAttachmentSrc(fl_color, group)
)

fillColor = NULL
}

# popup
if (!is.null(popup)) {
if (!is.null(popup) && !isFALSE(popup)) {
htmldeps <- htmltools::htmlDependencies(popup)
if (length(htmldeps) != 0) {
map$dependencies = c(
Expand All @@ -253,30 +253,30 @@ addGlPointsSrc = function(map,
)
}
popup = makePopup(popup, data)
fl_popup = paste0(dir_popup, "/", layerId, "_popup.js")
pre = paste0('var popup = popup || {}; popup["', layerId, '"] = ')
fl_popup = paste0(dir_popup, "/", group, "_popup.js")
pre = paste0('var popups = popups || {}; popups["', group, '"] = ')
writeLines(pre, fl_popup)
cat('[', jsonify::to_json(popup), '];',
file = fl_popup, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyPopupAttachmentSrc(fl_popup, layerId)
glifyPopupAttachmentSrc(fl_popup, group)
)

popup <- TRUE
}

# radius
if (length(unique(radius)) > 1) {
fl_radius = paste0(dir_radius, "/", layerId, "_radius.js")
pre = paste0('var rad = rad || {}; rad["', layerId, '"] = ')
fl_radius = paste0(dir_radius, "/", group, "_radius.js")
pre = paste0('var rad = rad || {}; rad["', group, '"] = ')
writeLines(pre, fl_radius)
cat('[', jsonify::to_json(radius), '];',
file = fl_radius, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyRadiusAttachmentSrc(fl_radius, layerId)
glifyRadiusAttachmentSrc(fl_radius, group)
)

radius = NULL
Expand All @@ -301,6 +301,7 @@ addGlPointsSrc = function(map,
, fillOpacity
, group
, layerId
, popup
)

leaflet::expandLimits(
Expand Down
24 changes: 12 additions & 12 deletions R/glify-polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ addGlPolygonsSrc = function(map,
data = sf::st_sf(id = 1:length(geom), geometry = geom)

ell_args <- list(...)
fl_data = paste0(dir_data, "/", layerId, "_data.js")
pre = paste0('var data = data || {}; data["', layerId, '"] = ')
fl_data = paste0(dir_data, "/", group, "_data.js")
pre = paste0('var data = data || {}; data["', group, '"] = ')
writeLines(pre, fl_data)
jsonify_args = try(
match.arg(
Expand All @@ -196,7 +196,7 @@ addGlPolygonsSrc = function(map,
map$dependencies = c(
map$dependencies,
glifyDependenciesSrc(),
glifyDataAttachmentSrc(fl_data, layerId)
glifyDataAttachmentSrc(fl_data, group)
)

# color
Expand All @@ -208,24 +208,23 @@ addGlPolygonsSrc = function(map,
if (ncol(fillColor) != 3) stop("only 3 column fillColor matrix supported so far")
fillColor = as.data.frame(fillColor, stringsAsFactors = FALSE)
colnames(fillColor) = c("r", "g", "b")

if (nrow(fillColor) > 1) {
fl_color = paste0(dir_color, "/", layerId, "_color.js")
pre = paste0('var col = col || {}; col["', layerId, '"] = ')
fl_color = paste0(dir_color, "/", group, "_color.js")
pre = paste0('var col = col || {}; col["', group, '"] = ')
writeLines(pre, fl_color)
cat('[', jsonify::to_json(fillColor), '];',
file = fl_color, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyColorAttachmentSrc(fl_color, layerId)
glifyColorAttachmentSrc(fl_color, group)
)

fillColor = NULL
}

# popup
if (!is.null(popup)) {
if (!is.null(popup) && !isFALSE(popup)) {
htmldeps <- htmltools::htmlDependencies(popup)
if (length(htmldeps) != 0) {
map$dependencies = c(
Expand All @@ -234,17 +233,17 @@ addGlPolygonsSrc = function(map,
)
}
popup = makePopup(popup, data_orig)
fl_popup = paste0(dir_popup, "/", layerId, "_popup.js")
pre = paste0('var popup = popup || {}; popup["', layerId, '"] = ')
fl_popup = paste0(dir_popup, "/", group, "_popup.js")
pre = paste0('var popups = popups || {}; popups["', group, '"] = ')
writeLines(pre, fl_popup)
cat('[', jsonify::to_json(popup), '];',
file = fl_popup, append = TRUE)

map$dependencies = c(
map$dependencies,
glifyPopupAttachmentSrc(fl_popup, layerId)
glifyPopupAttachmentSrc(fl_popup, group)
)

popup <- TRUE
}

map = leaflet::invokeMethod(
Expand All @@ -255,6 +254,7 @@ addGlPolygonsSrc = function(map,
, fillOpacity
, group
, layerId
, popup
)

leaflet::expandLimits(
Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/Leaflet.glify/addGlifyPoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ LeafletWidget.methods.addGlifyPoints = function(data, cols, popup, opacity, radi
var content = popup ? popup[idx].toString() : null;
if (HTMLWidgets.shinyMode) {
Shiny.setInputValue(map.id + "_glify_click", {
id: layerId ? layerId[idx] : idx+1,
id: layerId ? (Array.isArray(layerId) ? layerId[idx] : layerId) : idx+1,
group: pointslayer.settings.className,
lat: point[0],
lng: point[1],
Expand Down
39 changes: 24 additions & 15 deletions inst/htmlwidgets/Leaflet.glify/addGlifyPointsSrc.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
LeafletWidget.methods.addGlifyPointsSrc = function(fillColor, radius, fillOpacity, group, layerId) {
LeafletWidget.methods.addGlifyPointsSrc = function(fillColor, radius, fillOpacity, group, layerId, popup) {

var map = this;

// color
var clrs;
if (fillColor === null) {
clrs = function(index, feature) { return col[layerId][0][index]; };
clrs = function(index, feature) { return col[group][0][index]; };
} else {
clrs = fillColor;
}

// radius
var size;
if (radius === null) {
size = function(index, point) { return rad[layerId][0][index]; };
size = function(index, point) { return rad[group][0][index]; };
} else {
size = radius;
}

// popup
var pop;
if (popup && popups[group]) {
pop = function(index, feature) { return popups[group][0][index]; };
}

var pointslayer = L.glify.points({
map: map,
click: function (e, point, xy) {
if (typeof(popup) === "undefined") {
return;
} else if (typeof(popup[layerId]) === "undefined") {
return;
} else {
//var idx = data[layerId][0].indexOf(point);
var idx = data[layerId][0].findIndex(k => k==point);
//set up a standalone popup (use a popup as a layer)
if (map.hasLayer(pointslayer.glLayer)) {
if (map.hasLayer(pointslayer.glLayer)) {
var idx = data[group][0].findIndex(k => k==point);
if (HTMLWidgets.shinyMode) {
Shiny.setInputValue(map.id + "_glify_click", {
id: layerId ? (Array.isArray(layerId) ? layerId[idx] : layerId) : idx+1,
group: Object.values(pointslayer.glLayer._eventParents)[0].groupname,
lat: e.latlng.lat,
lng: e.latlng.lng,
data: point
});
}
if (pop !== undefined) {
L.popup()
.setLatLng(point)
.setContent(popup[layerId][0][idx].toString())
.setContent(popups[group][0][idx].toString())
.openOn(map);
}
}
},
data: data[layerId][0],
data: data[group][0],
color: clrs,
opacity: fillOpacity,
size: size,
className: group
});

map.layerManager.addLayer(pointslayer.glLayer, "glify", layerId, group);
map.layerManager.addLayer(pointslayer.glLayer, "glify", null, group);

};

Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/Leaflet.glify/addGlifyPolygons.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LeafletWidget.methods.addGlifyPolygons = function(data, cols, popup, opacity, gr
var idx = data.features.findIndex(k => k==feature);
if (HTMLWidgets.shinyMode) {
Shiny.setInputValue(map.id + "_glify_click", {
id: layerId ? layerId[idx] : idx+1,
id: layerId ? (Array.isArray(layerId) ? layerId[idx] : layerId) : idx+1,
group: Object.values(shapeslayer.glLayer._eventParents)[0].groupname,
lat: e.latlng.lat,
lng: e.latlng.lng,
Expand Down
Loading