viewer: switch to modern fetch API, do not cache metadata and entity files

This commit is contained in:
Matthias Schiffer 2024-01-07 20:49:36 +01:00
parent 3d024c6cd8
commit 643035eaed
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C

View file

@ -284,76 +284,71 @@ function createSign(sign, back) {
return wrapper; return wrapper;
} }
function loadSigns(signLayer) { async function loadSigns(signLayer) {
const xhr = new XMLHttpRequest(); const response = await fetch('data/entities.json', {cache: 'no-store'});
xhr.onload = function () { const res = await response.json();
const res = JSON.parse(this.responseText);
const groups = {};
// Group signs by x,z coordinates const groups = {};
for (const sign of res.signs) {
const key = coordKey([sign.x, sign.z]);
const group = groups[key] ??= [];
group.push(sign);
}
for (const [key, group] of Object.entries(groups)) { // Group signs by x,z coordinates
const el = document.createElement('div'); for (const sign of res.signs) {
const key = coordKey([sign.x, sign.z]);
let material; const group = groups[key] ??= [];
let kind; group.push(sign);
// Sort from top to bottom
group.sort((a, b) => b.y - a.y);
for (const sign of group) {
el.appendChild(createSign(sign, false));
if (sign.back_text)
el.appendChild(createSign(sign, true));
material ??= sign.material;
kind ??= sign.kind;
}
// Default material
material ??= 'oak';
const [x, z] = key.split(',').map((i) => +i);
const popup = L.popup().setContent(el);
popup.on('add', () => {
params.marker = [x, z];
updateHash();
});
popup.on('remove', () => {
params.marker = null;
updateHash();
});
const marker = L.marker([-z-0.5, x+0.5], {
icon: signIcon(material, kind),
}).addTo(signLayer).bindPopup(popup);
markers[coordKey([x, z])] = marker;
if (params.marker && x === params.marker[0] && z === params.marker[1])
marker.openPopup();
}
} }
xhr.open('GET', 'data/entities.json', true); for (const [key, group] of Object.entries(groups)) {
xhr.send(); const el = document.createElement('div');
let material;
let kind;
// Sort from top to bottom
group.sort((a, b) => b.y - a.y);
for (const sign of group) {
el.appendChild(createSign(sign, false));
if (sign.back_text)
el.appendChild(createSign(sign, true));
material ??= sign.material;
kind ??= sign.kind;
}
// Default material
material ??= 'oak';
const [x, z] = key.split(',').map((i) => +i);
const popup = L.popup().setContent(el);
popup.on('add', () => {
params.marker = [x, z];
updateHash();
});
popup.on('remove', () => {
params.marker = null;
updateHash();
});
const marker = L.marker([-z-0.5, x+0.5], {
icon: signIcon(material, kind),
}).addTo(signLayer).bindPopup(popup);
markers[coordKey([x, z])] = marker;
if (params.marker && x === params.marker[0] && z === params.marker[1])
marker.openPopup();
}
} }
window.createMap = function () { window.createMap = function () {
const xhr = new XMLHttpRequest(); (async function () {
xhr.onload = function () { const response = await fetch('data/info.json', {cache: 'no-store'});
const res = JSON.parse(this.responseText), const res = await response.json();
mipmaps = res.mipmaps, const {mipmaps, spawn} = res;
spawn = res.spawn, const features = res.features || {};
features = res.features || {};
const updateParams = function () { const updateParams = function () {
const args = parseHash(); const args = parseHash();
@ -489,8 +484,5 @@ window.createMap = function () {
updateHash(); updateHash();
}; };
}; })();
xhr.open('GET', 'data/info.json', true);
xhr.send();
} }