ensure marker id is unique

This commit is contained in:
Azgaar 2021-12-29 22:11:45 +03:00
parent 6ad2f34639
commit c20bcc1502
2 changed files with 26 additions and 1 deletions

View file

@ -1013,6 +1013,31 @@ function parseLoadedData(data) {
ERROR && console.error("Data Integrity Check. Province", p.i, "is linked to removed state", p.state); ERROR && console.error("Data Integrity Check. Province", p.i, "is linked to removed state", p.state);
p.removed = true; // remove incorrect province p.removed = true; // remove incorrect province
}); });
{
const markerIds = [];
let nextId = last(pack.markers)?.i + 1 || 0;
pack.markers.forEach(marker => {
if (markerIds[marker.i]) {
ERROR && console.error("Data Integrity Check. Marker", marker.i, "has non-unique id. Changing to", nextId);
const domElements = document.querySelectorAll("#marker" + marker.i);
if (domElements[1]) domElements[1].id = "marker" + nextId; // rename 2nd dom element
const noteElements = notes.filter(note => note.id === "marker" + marker.i);
if (noteElements[1]) noteElements[1].id = "marker" + nextId; // rename 2nd note
marker.i = nextId;
nextId += 1;
} else {
markerIds[marker.i] = true;
}
});
// sort markers by index
pack.markers.sort((a, b) => a.i - b.i);
}
})(); })();
changeMapSize(); changeMapSize();

View file

@ -97,7 +97,7 @@ window.Markers = (function () {
} }
function addMarker({cell, type, icon, dx, dy, px}) { function addMarker({cell, type, icon, dx, dy, px}) {
const i = pack.markers.length; const i = last(pack.markers)?.i + 1 || 0;
const [x, y] = getMarkerCoordinates(cell); const [x, y] = getMarkerCoordinates(cell);
const marker = {i, icon, type, x, y, cell}; const marker = {i, icon, type, x, y, cell};
if (dx) marker.dx = dx; if (dx) marker.dx = dx;