in-c4/static/index.js

153 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2022-08-04 05:41:51 +02:00
let items = {};
document.onreadystatechange = function() {
if (document.readyState === 'interactive') {
document.getElementById('search').oninput = search;
const checkbox = document.getElementById('showhidden')
checkbox.oninput = showhidden;
fetch('/api/items').then((response) => response.json()).then((data) => {
items = data;
renderItems();
showhidden({target: checkbox});
});
}
}
function renderItems() {
const container = document.getElementById('results');
const template = document.getElementById('item');
const loading = document.getElementById('loading');
2024-05-16 22:07:04 +02:00
let count = 0;
2022-08-04 05:41:51 +02:00
for (const [id, item] of Object.entries(items)) {
const clone = template.content.cloneNode(true);
const [loc, longloc] = getLocString(items, item);
clone.querySelector(".loc").textContent = loc;
clone.querySelector(".loc").title = longloc;
clone.querySelector(".type").textContent = item.type;
clone.querySelector(".id").textContent = item.id;
clone.querySelector(".name").textContent = item.name;
clone.querySelector(".content").textContent = item.content;
clone.querySelector(".note").textContent = item.note;
clone.querySelector(".result").id = `item-${id}`;
2022-08-06 01:12:47 +02:00
clone.querySelector("a").href = `form.html?id=${encodeURIComponent(id)}`;
2022-08-04 05:41:51 +02:00
if (item.hidden) {
2024-05-16 22:07:04 +02:00
clone.querySelector(".result").classList.add('hidden');
} else {
count++;
}
2022-08-04 05:41:51 +02:00
container.appendChild(clone);
2024-05-16 22:07:04 +02:00
}
2022-08-04 05:41:51 +02:00
loading.remove();
2024-05-16 22:07:04 +02:00
updateCounter(count);
2022-08-04 05:41:51 +02:00
}
function getLocString(items, item) {
item.ancestors = [];
2022-08-04 05:41:51 +02:00
let next = item;
2022-08-05 02:08:51 +02:00
let loop = false;
2022-08-04 05:41:51 +02:00
while (next) {
if (item.ancestors.includes(next)) {
2022-08-05 02:08:51 +02:00
loop = true;
if (item.ancestors.length == 1) {
item.ancestors.push(null);
2022-08-05 02:08:51 +02:00
}
break;
}
item.ancestors.unshift(next);
2022-08-04 05:41:51 +02:00
next = items[next.is_in];
}
item.ancestors.pop();
let loc = item.ancestors.map(i => i.id).join(' ➜ ') || '⬚';
let longloc = item.ancestors.map(i => `${i.type || ''} ${i.id} ${i.name && `(${i.name})` || ''}`).join(' ➜ ') || 'universe';
2022-08-05 02:08:51 +02:00
if (loop) {
loc += ' ↻';
longloc += ' ↻';
}
2022-08-04 05:41:51 +02:00
return [loc, `${item.id} is in ${longloc}`];
}
function search(e) {
const searchAttrs = ['id', 'name', 'type', 'note', 'content'];
const query = e.target.value;
const regex = new RegExp(query, 'i')
2024-05-16 22:07:04 +02:00
let count = 0;
2022-08-04 05:41:51 +02:00
for (const elem of document.getElementsByClassName('result')) {
const item = items[elem.id.slice(5)];
2022-08-04 05:41:51 +02:00
let found = false;
if (query) {
for (const a in searchAttrs) {
const attr = item[searchAttrs[a]];
if (attr && attr.match(regex)) {
found = true;
break;
}
}
} else {
found = true;
}
if (found) {
elem.classList.remove('filtered');
2024-05-16 22:07:04 +02:00
count++;
2022-08-04 05:41:51 +02:00
} else {
elem.classList.add('filtered');
}
}
2024-05-16 22:07:04 +02:00
// Indicate failed search.
if (count) {
e.target.classList.remove('failed');
} else {
e.target.classList.add('failed');
}
updateCounter(count);
}
function updateCounter(count) {
const itemcount = document.getElementById('itemcount');
switch(count) {
case 0:
itemcount.textContent = 'No items found.';
break;
case 1:
itemcount.textContent = '1 item found.';
break;
default:
itemcount.textContent = `${count} items found.`;
break;
}
2022-08-04 05:41:51 +02:00
}
function showhidden(e){
const container = document.getElementById('results');
if (e.target.checked) {
results.classList.add('showhidden');
} else {
results.classList.remove('showhidden');
}
}
function showItem(e) {
const item = items[e.target.id.slice(5)];
const ancestors = item.ancestors.concat(item);
for (let i = ancestors.length-1; i >= 0; i--) {
const ancestor = ancestors[i];
if (ancestor.coords_bl && ancestor.coords_tr) {
if (ancestor != item) {
const mapnote = `Showing ${ancestor.type || ''} ${ancestor.id} ${ancestor.name && `(${ancestor.name})` || ''}`;
document.getElementById('mapnote').textContent = mapnote;
}
coordsToMap(ancestor.coords_bl, ancestor.coords_tr);
break;
}
}
}
function hideItem(e) {
clearMap();
2024-05-16 22:07:04 +02:00
}