import { createApp, reactive, ref, computed, watch, onMounted, toRaw } from '../lib/vue.esm-browser.js';
import * as ntools from './node-utils.js';
const apiUrl = 'https://map.meshcore.dev/api/v1/nodes';
const keyOrder = ['adv_name', 'type', 'link', 'inserted_date', 'updated_date', 'public_key', 'coords', 'params' ]
const humanLabel = {
coords: 'Coordinates',
adv_name: 'Name',
inserted_date: 'Inserted',
updated_date: 'Last updated',
public_key: 'Public key',
type: 'Node type',
params: 'Radio params',
link: 'Meshcore link',
};
const types = {
'1': 'Client',
'2': 'Repeater',
'3': 'Room Server',
'4': 'Sensor'
};
const humanValue = {
inserted_date(val) {
return new Date(val).toLocaleString();
},
updated_date(val) {
return new Date(val).toLocaleString();
},
coords(val) {
return `${val}`;
},
type(val) {
return types[val];
},
link(val) {
return `Copy to clipboard`
},
params(val) {
return Object.entries(val).map(([key, val]) => `${key}=${val}`).join(', ')
}
}
function getSvgIconUrl(text, color) {
const svg = `
`;
return L.icon({
iconUrl: URL.createObjectURL(new Blob([svg], { type: 'image/svg+xml' })),
iconSize: [32, 32],
iconAnchor: [17, 17],
popupAnchor: [0, -16],
});
}
function clearLocationHash () {
history.pushState('', document.title, location.pathname + location.search);
}
function getTable(node) {
return '
'+
'' + keyOrder.flatMap(key => node[key] ? [`| ${humanLabel[key]} | ${ humanValue[key] ? humanValue[key](node[key]) : node[key] } | `] : [] ).join('
') + '
'+
'
';
}
window.isNewerThan = (date, days) => {
const daysMs = 1000 * 3600 * 24 * days;
const dateMs = new Date(date).getTime();
return dateMs > Date.now() - daysMs;
}
const deletionMailUrl = new URL('mailto:recrof@gmail.com');
deletionMailUrl.searchParams.append('subject', 'MeshCore Map node deletion request');
deletionMailUrl.searchParams.append('body',
'Please delete my node from MeshCore Map database\n'+
'MeshCore link: \n'
);
const appAttribution = `
App: recrof,
support my work |
Node deletion request
`;
const baseMapSelected = localStorage.getItem('baseMapSelected') || 'OpenStreetMap';
const baseMaps = {
'OpenStreetMap': L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: `Tiles: © OpenStreetMap | ${appAttribution}`
}),
'Esri Satellite': L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
maxZoom: 18,
attribution: `Tiles: © Esri | Sources: Esri, DigitalGlobe, GeoEye, i-cubed, USDA FSA, USGS, AEX, Getmapping, Aerogrid, IGN, IGP, swisstopo, GIS Users | ${appAttribution}`,
}),
};
let initCoords = { lat: 7, lon: 25, zoom: 3 };
const urlParams = Object.fromEntries(new URLSearchParams(location.search));
if(!(isNaN(urlParams.lat) || isNaN(urlParams.lon) || isNaN(urlParams.zoom))) {
initCoords = urlParams
}
const map = window.leafletMap = leaflet.map('map', {
minZoom: 2,
maxBounds: [
[-90, -180], // top left
[90, 200], // bottom right
],
layers: baseMaps[baseMapSelected],
zoomControl: false
}).setView([initCoords.lat, initCoords.lon], initCoords.zoom);
map.on('baselayerchange', function(ev) {
localStorage.setItem('baseMapSelected', ev.name);
});
L.control.layers(baseMaps, null, { position: 'bottomleft' }).addTo(map);
// map.zoomControl.setPosition('bottomleft');
const icons = Object.fromEntries([1, 2, 3, 4].map(id => [id, L.icon({
iconUrl: `img/node_types/${id}.svg`,
iconSize: [32, 32],
iconAnchor: [17, 17],
popupAnchor: [0, -16],
})]));
createApp({
setup() {
const dialogAddNode = ref();
const app = window.app = reactive({
nodes: [],
nodesByType: {},
filteredNodes: [],
search: '',
link: '',
nodeFilter: [],
fromDate: '2025-03-01',
clusteringZoom: 12,
});
const filtersActive = computed(() => app.filteredNodes.length && app.nodes.length !== app.filteredNodes.length);
const stats = computed(() => {
const nodes = app.nodes;
if(!nodes) return [];
const result = [];
result.push(`
total: ${nodes.length} |
person${nodes.filter(n => n.type === 1).length} |
cell_tower${nodes.filter(n => n.type === 2).length} |
forum${nodes.filter(n => n.type === 3).length}
`);
result.push(`24h: ${app.nodes.filter(n => isNewerThan(n.inserted_date, 1)).length}`);
result.push(`7d: ${app.nodes.filter(n => isNewerThan(n.inserted_date, 7)).length}`);
result.push(`30d: ${app.nodes.filter(n => isNewerThan(n.inserted_date, 30)).length}`);
return result;
});
const searchResults = computed(() => {
if(!app.search) { return [] }
return app.filteredNodes.filter(
node => node.adv_name.toLowerCase().includes(app.search.toLowerCase()) || node.public_key.startsWith(app.search)
).toSorted(
(a, b) => a.adv_name.localeCompare(b.adv_name)
).slice(0, 20);
});
watch([
() => app.nodeFilter,
() => app.fromDate,
],
() => {
const fromDate = new Date(app.fromDate);
app.filteredNodes = app.nodeFilter
.flatMap(type => app.nodesByType[type])
.filter(node => node && (node.updatedDate ? node.updatedDate > fromDate : node.insertDate > fromDate));
console.log('refresh', app.nodeFilter, app.filteredNodes.length);
refreshMap({ download: false });
}
);
watch(() => app.clusteringZoom, () => {
refreshMap({ download: false, clusteringZoom: app.clusteringZoom });
});
let markerClusterGroup = L.markerClusterGroup({
disableClusteringAtZoom: app.clusteringZoom
});
async function refreshMap({ download = true, clusteringZoom = 0 } = {}) {
if(download) {
const nodesReq = await fetch(apiUrl);
app.nodes = await nodesReq.json();
for(const node of app.nodes) {
let icon = icons[node.type.toString()];
(app.nodesByType[node.type] ??= []).push(node);
if(node.type === 1) {
const label = ntools.getNameIconLabel(node.adv_name);
const color = ntools.getColourForName(node.adv_name);
icon = getSvgIconUrl(label, color);
}
const marker = node.marker = L.marker(
[node.adv_lat, node.adv_lon], { icon, title: node.adv_name }
);
node.coords = `${node.adv_lat.toFixed(4)}, ${node.adv_lon.toFixed(4)}`;
node.lastAdvertDate = new Date(node.last_advert);
node.insertDate = new Date(node.inserted_date);
node.updatedDate = node.updated_date && new Date(node.updated_date);
const popup = L.popup({ minWidth: 350, maxWidth: 350, content: getTable(node) });
marker.bindPopup(popup);
}
}
markerClusterGroup.clearLayers();
const nodes = app.filteredNodes.length > 0 ? app.filteredNodes : app.nodes;
map.removeLayer(markerClusterGroup);
if(clusteringZoom) {
markerClusterGroup = L.markerClusterGroup({
disableClusteringAtZoom: clusteringZoom
});
}
for(const node of nodes) {
markerClusterGroup.addLayer(toRaw(node.marker));
}
map.addLayer(markerClusterGroup);
}
async function addNode() {
if(!(app.link && app.link.startsWith('meshcore://'))) {
alert('Please paste valid meshcore link.');
return;
};
const res = await fetch(apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
links: [ app.link ],
radio: {}
})
});
const reply = await res.json();
alert(reply.message || reply.error);
clearLocationHash();
location.reload();
}
function showNode(node) {
node.marker.openPopup();
map.flyTo(node.marker.getLatLng(), 19);
app.search = '';
}
function highlightString(source, toHighlight) {
const escapedSource = source.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>');
const matchIndex = source.toLowerCase().indexOf(toHighlight.toLowerCase());
const highlightString = matchIndex >= 0 ? source.substring(matchIndex, matchIndex + toHighlight.length) : toHighlight;
return escapedSource.replace(highlightString, `${highlightString}`);
}
map.on('moveend', function(e) {
const pos = map.getCenter();
const zoom = map.getZoom();
history.replaceState({}, '', `/?lat=${pos.lat.toFixed(4)}&lon=${pos.lng.toFixed(4)}&zoom=${zoom}`);
});
refreshMap();
onMounted(() => {
app.nodeFilter = ['1', '2', '3', '4'];
if(location.hash === '#add-new-node') {
dialogAddNode.value.showModal();
dialogAddNode.value.addEventListener('close', () => clearLocationHash());
}
})
window.refreshMap = refreshMap;
return {
app, refreshMap, addNode,
stats, searchResults, filtersActive,
showNode, dialogAddNode, highlightString
}
},
}).mount('#app')