diff --git a/R/glify-lines.R b/R/glify-lines.R index 73890a6..2372e5a 100644 --- a/R/glify-lines.R +++ b/R/glify-lines.R @@ -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( @@ -199,7 +199,7 @@ addGlPolylinesSrc = function(map, map$dependencies = c( map$dependencies, glifyDependenciesSrc(), - glifyDataAttachmentSrc(fl_data, layerId) + glifyDataAttachmentSrc(fl_data, group) ) # color @@ -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( @@ -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 @@ -275,6 +274,7 @@ addGlPolylinesSrc = function(map, , opacity , group , layerId + , popup ) leaflet::expandLimits( diff --git a/R/glify-points.R b/R/glify-points.R index bb926ce..f1dcb75 100644 --- a/R/glify-points.R +++ b/R/glify-points.R @@ -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( @@ -215,7 +215,7 @@ addGlPointsSrc = function(map, map$dependencies = c( map$dependencies, glifyDependenciesSrc(), - glifyDataAttachmentSrc(fl_data, layerId) + glifyDataAttachmentSrc(fl_data, group) ) # color @@ -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( @@ -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 @@ -301,6 +301,7 @@ addGlPointsSrc = function(map, , fillOpacity , group , layerId + , popup ) leaflet::expandLimits( diff --git a/R/glify-polygons.R b/R/glify-polygons.R index 8d85901..3cbd566 100644 --- a/R/glify-polygons.R +++ b/R/glify-polygons.R @@ -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( @@ -196,7 +196,7 @@ addGlPolygonsSrc = function(map, map$dependencies = c( map$dependencies, glifyDependenciesSrc(), - glifyDataAttachmentSrc(fl_data, layerId) + glifyDataAttachmentSrc(fl_data, group) ) # color @@ -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( @@ -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( @@ -255,6 +254,7 @@ addGlPolygonsSrc = function(map, , fillOpacity , group , layerId + , popup ) leaflet::expandLimits( diff --git a/inst/htmlwidgets/Leaflet.glify/addGlifyPoints.js b/inst/htmlwidgets/Leaflet.glify/addGlifyPoints.js index 8852560..a3287cd 100644 --- a/inst/htmlwidgets/Leaflet.glify/addGlifyPoints.js +++ b/inst/htmlwidgets/Leaflet.glify/addGlifyPoints.js @@ -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], diff --git a/inst/htmlwidgets/Leaflet.glify/addGlifyPointsSrc.js b/inst/htmlwidgets/Leaflet.glify/addGlifyPointsSrc.js index 12729c7..664459a 100644 --- a/inst/htmlwidgets/Leaflet.glify/addGlifyPointsSrc.js +++ b/inst/htmlwidgets/Leaflet.glify/addGlifyPointsSrc.js @@ -1,11 +1,11 @@ -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; } @@ -13,38 +13,47 @@ LeafletWidget.methods.addGlifyPointsSrc = function(fillColor, radius, fillOpacit // 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); }; diff --git a/inst/htmlwidgets/Leaflet.glify/addGlifyPolygons.js b/inst/htmlwidgets/Leaflet.glify/addGlifyPolygons.js index 30f5302..17078fd 100644 --- a/inst/htmlwidgets/Leaflet.glify/addGlifyPolygons.js +++ b/inst/htmlwidgets/Leaflet.glify/addGlifyPolygons.js @@ -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, diff --git a/inst/htmlwidgets/Leaflet.glify/addGlifyPolygonsSrc.js b/inst/htmlwidgets/Leaflet.glify/addGlifyPolygonsSrc.js index e714e8e..3ad2f78 100644 --- a/inst/htmlwidgets/Leaflet.glify/addGlifyPolygonsSrc.js +++ b/inst/htmlwidgets/Leaflet.glify/addGlifyPolygonsSrc.js @@ -1,4 +1,4 @@ -LeafletWidget.methods.addGlifyPolygonsSrc = function(fillColor, fillOpacity, group, layerId) { +LeafletWidget.methods.addGlifyPolygonsSrc = function(fillColor, fillOpacity, group, layerId, popup) { var map = this; @@ -7,34 +7,45 @@ LeafletWidget.methods.addGlifyPolygonsSrc = function(fillColor, fillOpacity, gro // 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; } + // popup + var pop; + if (popup && popups[group]) { + pop = function(index, feature) { return popups[group][0][index]; }; + } + var shapeslayer = L.glify.shapes({ map: map, click: function (e, feature) { - if (typeof(popup) === "undefined") { - return; - } else if (typeof(popup[layerId]) === "undefined") { - return; - } else { if (map.hasLayer(shapeslayer.glLayer)) { - var idx = data[layerId][0].features.findIndex(k => k==feature); - L.popup() - .setLatLng(e.latlng) - .setContent(popup[layerId][0][idx].toString()) - .openOn(map); - } + var idx = data[group][0].features.findIndex(k => k==feature); + if (HTMLWidgets.shinyMode) { + Shiny.setInputValue(map.id + "_glify_click", { + 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, + data: feature.properties + }); + } + if (pop !== undefined) { + L.popup() + .setLatLng(e.latlng) + .setContent(popups[group][0][idx].toString()) + .openOn(map); + } } }, - data: data[layerId][0], + data: data[group][0], color: clrs, opacity: fillOpacity, className: group }); - map.layerManager.addLayer(shapeslayer.glLayer, null, null, group); + map.layerManager.addLayer(shapeslayer.glLayer, "glify", null, group); }; diff --git a/inst/htmlwidgets/Leaflet.glify/addGlifyPolylines.js b/inst/htmlwidgets/Leaflet.glify/addGlifyPolylines.js index 7a5780f..d7a23d3 100644 --- a/inst/htmlwidgets/Leaflet.glify/addGlifyPolylines.js +++ b/inst/htmlwidgets/Leaflet.glify/addGlifyPolylines.js @@ -21,7 +21,7 @@ LeafletWidget.methods.addGlifyPolylines = function(data, cols, popup, opacity, g 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(lineslayer.glLayer._eventParents)[0].groupname, lat: e.latlng.lat, lng: e.latlng.lng, diff --git a/inst/htmlwidgets/Leaflet.glify/addGlifyPolylinesSrc.js b/inst/htmlwidgets/Leaflet.glify/addGlifyPolylinesSrc.js index 7faa132..4a908d2 100644 --- a/inst/htmlwidgets/Leaflet.glify/addGlifyPolylinesSrc.js +++ b/inst/htmlwidgets/Leaflet.glify/addGlifyPolylinesSrc.js @@ -1,49 +1,61 @@ -LeafletWidget.methods.addGlifyPolylinesSrc = function(color, weight, opacity, group, layerId) { +LeafletWidget.methods.addGlifyPolylinesSrc = function(color, weight, opacity, group, layerId, popup) { var map = this; // color var clrs; if (color === null) { - clrs = function(index, feature) { return col[layerId][0][index]; }; + clrs = function(index, feature) { return col[group][0][index]; }; } else { clrs = color; } - // radius + // weight var wght; if (weight === null) { - wght = function(index, feature) { return wgt[layerId][0][index]; }; + wght = function(index, feature) { return wgt[group][0][index]; }; } else { wght = weight; } + // popup + var pop; + if (popup && popups[group]) { + pop = function(index, feature) { return popups[group][0][index]; }; + } + + var lineslayer = L.glify.lines({ map: map, click: function (e, feature) { - if (typeof(popup) === "undefined") { - return; - } else if (typeof(popup[layerId]) === "undefined") { - return; - } else { if (map.hasLayer(lineslayer.glLayer)) { - var idx = data[layerId][0].features.findIndex(k => k==feature); + var idx = data[group][0].features.findIndex(k => k==feature); + if (HTMLWidgets.shinyMode) { + Shiny.setInputValue(map.id + "_glify_click", { + id: layerId ? (Array.isArray(layerId) ? layerId[idx] : layerId) : idx+1, + group: Object.values(lineslayer.glLayer._eventParents)[0].groupname, + lat: e.latlng.lat, + lng: e.latlng.lng, + data: feature.properties + }); + } + if (pop !== undefined) { L.popup() .setLatLng(e.latlng) - .setContent(popup[layerId][0][idx].toString()) + .setContent(popups[group][0][idx].toString()) .openOn(map); } } }, latitudeKey: 1, longitudeKey: 0, - data: data[layerId][0], + data: data[group][0], color: clrs, opacity: opacity, weight: wght, className: group }); - map.layerManager.addLayer(lineslayer.glLayer, null, null, group); + map.layerManager.addLayer(lineslayer.glLayer, "glify", null, group); }; diff --git a/tests/testthat/test-leafgl-addGlPoints.R b/tests/testthat/test-leafgl-addGlPoints.R index 8fdbdc3..3fbb823 100644 --- a/tests/testthat/test-leafgl-addGlPoints.R +++ b/tests/testthat/test-leafgl-addGlPoints.R @@ -19,7 +19,7 @@ test_that("addGlPoints works", { m = leaflet() %>% addGlPoints(data = pts, layerId = "someid", src = TRUE) expect_is(m, "leaflet") - expect_identical(m$dependencies[[length(m$dependencies)]]$name, paste0("someid","dat")) + expect_identical(m$dependencies[[length(m$dependencies)]]$name, "glpointsdat") m = leaflet() %>% addGlPoints(data = pts, group = NULL) @@ -28,12 +28,12 @@ test_that("addGlPoints works", { m = leaflet() %>% addGlPoints(data = pts, layerId = NULL, group = NULL, src = TRUE) expect_is(m, "leaflet") - expect_identical(m$dependencies[[length(m$dependencies)]]$name, "data-ptsdat") + expect_identical(m$dependencies[[length(m$dependencies)]]$name, "datadat") m = leaflet() %>% addGlPoints(data = breweries91, src = TRUE) expect_is(m, "leaflet") - expect_identical(m$dependencies[[length(m$dependencies)]]$name, "glpoints-ptsdat") + expect_identical(m$dependencies[[length(m$dependencies)]]$name, "glpointsdat") m = leaflet() %>% addGlPoints(data = breweries91, src = TRUE, radius = 5)