server_photo_s85js/public/js/map/photos/photoSource.js.ok
2026-08-11 08:45:04 +02:00

214 lines
3.9 KiB
Text

// public/js/map/photos/photoSource.js
// ==================================
// MAPBOX PHOTO SOURCE
// ==================================
(() => {
"use strict";
let mapboxMap = null;
const SOURCE_ID = "photos";
const SOURCE_OPTIONS = {
cluster: true,
clusterRadius: 80,
};
// ==================================
// SET MAP
// ==================================
function setMap(map) {
mapboxMap = map;
}
// ==================================
// SOURCE ESISTENTE
// ==================================
function getSource() {
if (!mapboxMap) {
return null;
}
return mapboxMap.getSource(SOURCE_ID);
}
// ==================================
// CREA SOURCE
// ==================================
function createSource(features = []) {
if (!mapboxMap) {
return null;
}
if (!mapboxMap.isStyleLoaded()) {
return null;
}
const existingSource = getSource();
if (existingSource) {
return existingSource;
}
const data = {
type: "FeatureCollection",
features,
};
console.log("[PhotoSource] creo source", {
features: features.length,
});
mapboxMap.addSource(SOURCE_ID, {
type: "geojson",
data,
...SOURCE_OPTIONS,
});
return getSource();
}
// ==================================
// UPDATE DATA
// ==================================
function setData(features = []) {
if (!mapboxMap) {
return;
}
if (!mapboxMap.isStyleLoaded()) {
return;
}
const data = {
type: "FeatureCollection",
features,
};
const source = getSource();
if (source) {
console.log("[PhotoSource] aggiorno source", {
features: features.length,
});
source.setData(data);
return;
}
createSource(features);
}
// ==================================
// GET CLUSTER LEAVES
// ==================================
function getClusterLeaves(
clusterId,
limit = 100,
offset = 0,
callback,
) {
const source = getSource();
if (!source) {
console.warn("[PhotoSource] source non presente");
return;
}
source.getClusterLeaves(
Number(clusterId),
limit,
offset,
callback,
);
}
// ==================================
// GET EXPANSION ZOOM
// ==================================
function getClusterExpansionZoom(clusterId, callback) {
const source = getSource();
if (!source) {
console.warn("[PhotoSource] source non presente");
return;
}
source.getClusterExpansionZoom(
Number(clusterId),
callback,
);
}
// ==================================
// QUERY FOTO
// ==================================
function getFeatures() {
if (!mapboxMap) {
return [];
}
return mapboxMap.querySourceFeatures(SOURCE_ID, {
filter: ["!", ["has", "point_count"]],
});
}
// ==================================
// QUERY CLUSTER
// ==================================
function getClusters() {
if (!mapboxMap) {
return [];
}
return mapboxMap.querySourceFeatures(SOURCE_ID, {
filter: ["has", "point_count"],
});
}
// ==================================
// DEBUG
// ==================================
function debug() {
const source = getSource();
if (!source) {
console.log("[PhotoSource] source photos non presente");
return;
}
console.log("[PhotoSource] debug", {
source,
features: source._data?.features?.length,
photoFeatures: getFeatures().length,
clusters: getClusters().length,
});
}
// ==================================
// API PUBBLICA
// ==================================
window.MapboxPhotoSource = {
setMap,
getSource,
createSource,
setData,
getClusterLeaves,
getClusterExpansionZoom,
getFeatures,
getClusters,
debug,
};
})();