I'm trying to make pat-leaflet dynamic, that is it shows a list of items as markers in a map but this list can dinamically change so I need to update the map. My first attempt was to remove/add marker but I don't have any way to do it because the map is not accessible from outside javascript (I think). My second attempt is to just update the data-geojson attribute and "re run" the pattern. I've found someone already did it on other patterns (see Mockup sortable pattern: how to refresh list after appending new item? - #2 by Netroxen) but the registry.scan($('body')); does nothing (I'm trying in the console and F12 browser tools).
The number of items is around 30, so there should be no much penalty on doing it.
ususally you don't need to take care of updating the map yourself as it's done by collectionfilter already ... I've several productive websites, which work well with collectionfilter and pat-leaflet eg Referenze — Italiano ...
Reading the code, the pat-leaflet read the geo-jsondata from an url and update it when an event is rised? Also I should add <div class="plone-loader"><div class="loader" /></div> to my template to get the update.
Really a great product! But I would like to try to do the update by myself in my product/template without adding collective.collectionfilter, just to filter the map using a custom list and updating the data-geojson attribute. What I miss is the way to tell pat-leaflet that data has changed and recreate the widget. A registry.scan() should do it but maybe I'm missing something.
The behavior of registry.scan() has changed recently so that an already scanned pattern doesn't get initialized anymore (which makes sense) ... you can help yourself with something like this:
let node = document.querySelector(".pat-leaflet");
node["pattern-leaflet"].init();
or jQuery style (I think in 6.0.0a2 you have to use this one):
var $el = $(".pat-leaflet");
$el.data("pattern-leaflet").init();
This a reference to manage the markers on a leaflet map, if someone need it:
var $el = $(".pat-leaflet");
var $mymap = $el.data("pattern-leaflet").map
clusters = []
$mymap.eachLayer((l) => {
if( l instanceof L.MarkerCluster)
clusters.push(l)
})
clusters[0].getAllChildMarkers() <- get all markers for each group
(we can use LayerGroup instead of MarkerCluster to get all the markers with clusters[0].getLayers())
$mymap.eachLayer((l) => {
if( l instanceof L.MarkerClusterGroup) <- use MarkerClusterGroup to remove markers
clusters.push(l)
})
clusters[0].getLayers()
clusters[0].removeLayer(clusters[0].getLayers()[10]) <- remove a marker and the cluster counter get updated correctly