MC/website/web/item-catalogue.js
2026-11-27 23:43:05 +00:00

7 lines
2.5 KiB
JavaScript

(()=>{const main=document.querySelector('main');if(!main||document.getElementById('registered-item-catalogue'))return;
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
const section=el('section');section.id='registered-item-catalogue';section.className='armoury-chapter item-catalogue';section.append(el('p','THE IMPERIAL ITEM REGISTER'),el('h2','Water, spice & stillsuits'),el('p','Registered equipment, quality and lore. Clan stillsuits are designed by clan leaders; political office equipment follows the current term of office.'));
const status=el('p','Loading the item register…'),menu=el('nav'),grid=el('div');menu.setAttribute('aria-label','Filter item quality');grid.className='equipment-grid';status.role='status';section.append(menu,status,grid);const first=main.querySelector('.armoury-chapter');if(first)first.before(section);else main.append(section);
fetch('/assets/items/catalogue-v1.json',{cache:'no-cache'}).then(r=>{if(!r.ok)throw Error();return r.json();}).then(data=>{if(data.schemaVersion!==1||!Array.isArray(data.items)||!Array.isArray(data.qualityTiers))throw Error();const tiers=new Map(data.qualityTiers.map(t=>[t.key,t])),buttons=[];let current='all';
function render(){grid.replaceChildren();for(const[k,b]of buttons)b.setAttribute('aria-pressed',String(k===current));const rows=data.items.filter(item=>current==='all'||item.quality===current);status.textContent=rows.length+' registered '+(rows.length===1?'item':'items');if(!rows.length)grid.append(el('p','No items are registered at this quality yet.'));for(const item of rows){const tier=tiers.get(item.quality);if(!tier)continue;const card=el('article'),image=el('img');if(typeof item.image==='string'&&/^\/assets\/items\/[a-z0-9_-]+\.(png|webp)$/.test(item.image))image.src=item.image;image.alt=item.name;image.loading='lazy';const badge=el('span',tier.label);badge.className='item-quality';badge.dataset.quality=item.quality;card.dataset.quality=item.quality;card.append(image,badge,el('h3',item.name));for(const line of Array.isArray(item.lore)?item.lore:[])card.append(el('p',line));if(item.appearance)card.append(el('p',item.appearance));if(item.maxStackSize)card.append(el('small','Stack size · '+item.maxStackSize));grid.append(card);}}
for(const[key,label]of [['all','All items'],...data.qualityTiers.map(t=>[t.key,t.label])]){const b=el('button',label);b.type='button';b.onclick=()=>{current=key;render();};buttons.push([key,b]);menu.append(b);}render();}).catch(()=>{status.textContent='The item register is temporarily unavailable.';});})();