forked from unimatrix/web
37 lines
7.2 KiB
JavaScript
37 lines
7.2 KiB
JavaScript
(()=>{
|
|
'use strict';
|
|
const $=s=>document.querySelector(s),all=s=>[...document.querySelectorAll(s)];
|
|
const nav=$('#portal-navigation')||$('nav[aria-label="Main navigation"]'),toggle=$('.menu-toggle'),menus=[...nav.querySelectorAll('details')];
|
|
function closeMenus(){for(const menu of menus)menu.open=false;nav.classList.remove('open');toggle.setAttribute('aria-expanded','false');}
|
|
toggle.addEventListener('click',()=>{const open=toggle.getAttribute('aria-expanded')!=='true';nav.classList.toggle('open',open);toggle.setAttribute('aria-expanded',String(open));});
|
|
for(const menu of menus)menu.addEventListener('toggle',()=>{if(menu.open)for(const other of menus)if(other!==menu)other.open=false;});
|
|
document.addEventListener('click',e=>{if(!e.target.closest('.nav-inner,.dr-header-bar'))closeMenus();});
|
|
document.addEventListener('keydown',e=>{if(e.key==='Escape'){const active=menus.find(m=>m.open);closeMenus();active?.querySelector('summary').focus();}});
|
|
for(const link of [...nav.querySelectorAll('a')])link.addEventListener('click',closeMenus);
|
|
const play=$('#play-dialog'),world=$('#world-dialog');let toastTimer;
|
|
function notify(message){const toast=$('.toast');clearTimeout(toastTimer);toast.textContent=message;toast.hidden=false;toastTimer=setTimeout(()=>toast.hidden=true,3500);}
|
|
for(const button of all('[data-copy]'))button.addEventListener('click',async()=>{try{await navigator.clipboard.writeText(button.dataset.copy);notify('Server address copied. See you in the realm.');}catch{notify('Server address: '+button.dataset.copy);}});
|
|
for(const button of all('[data-play]'))button.addEventListener('click',event=>{event.preventDefault();closeMenus();play.showModal();});
|
|
for(const dialog of [play,world]){dialog.querySelector('[data-close]').addEventListener('click',()=>dialog.close());dialog.addEventListener('click',e=>{if(e.target!==dialog)return;const r=dialog.getBoundingClientRect();if(e.clientX<r.left||e.clientX>r.right||e.clientY<r.top||e.clientY>r.bottom)dialog.close();});}
|
|
const tabs=all('[role=tab]');function edition(tab){for(const button of tabs){const active=button===tab;button.setAttribute('aria-selected',String(active));button.tabIndex=active?0:-1;$('#'+button.getAttribute('aria-controls')).hidden=!active;}}
|
|
for(const tab of tabs){tab.addEventListener('click',()=>edition(tab));tab.addEventListener('keydown',e=>{if(!['ArrowLeft','ArrowRight','Home','End'].includes(e.key))return;e.preventDefault();const next=e.key==='Home'?tabs[0]:e.key==='End'?tabs.at(-1):tabs[(tabs.indexOf(tab)+(e.key==='ArrowRight'?1:-1)+tabs.length)%tabs.length];edition(next);next.focus();});}
|
|
const worlds={arrakis:['Arrakduin','A desert that asks everything of you. Walk the dunes, seek the spice and find your place among the Fremen. Survival, opportunity and danger share the same sands.'],caladan:['Kaladam','Green cliffs rise from warm oceans. Travel between jungle islands, discover hidden shores and build a home where the sea meets the sky.'],giedi:['Giedermark','A world of dark stone, industry and ambition. Carve your future from its harsh landscape and discover what lies beyond the surface.'],kaitain:['Kaiterhof','The heart of Imperial life. Fields, forests and the affairs of great Houses meet here, where influence can matter as much as a blade.'],sigma:['Sigmadraken IV','An icy frontier of snowfields, frozen lakes and mountain forests. Prepare for the cold and write your story at the edge of the Imperium.']};
|
|
for(const button of all('[data-world]'))button.addEventListener('click',()=>{const [name,copy]=worlds[button.dataset.world];$('#world-title').textContent=name;$('#world-description').textContent=copy;const image=$('#world-dialog-art');image.src='/assets/'+button.dataset.world+'.webp';image.alt=button.querySelector('.world-art').alt;world.showModal();});
|
|
function node(tag,text,className){const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(className)n.className=className;return n;}
|
|
const date=n=>new Intl.DateTimeFormat('en-GB',{day:'numeric',month:'short',year:'numeric'}).format(new Date(Number(n)));
|
|
function excerpt(text){return String(text||'').replace(/!\[[^\]]*\]\([^)]*\)/g,'').replace(/\[([^\]]+)\]\([^)]*\)/g,'$1').replace(/[#*_`>]/g,'').replace(/\s+/g,' ').trim().slice(0,135);}
|
|
async function news(){const host=$('#news-grid');try{
|
|
const response=await fetch('/api/news?page=1&surface=home',{cache:'no-store',signal:AbortSignal.timeout(8000)});if(!response.ok)throw Error();const data=await response.json();if(!Array.isArray(data.posts))throw Error();host.replaceChildren();
|
|
for(const [i,post]of data.posts.slice(0,3).entries()){
|
|
const card=node('article',undefined,'news-card'+(!i?' featured':'')),href='https://www.dunerealms.com/news-archive.html?post='+encodeURIComponent(post.id),art=node('a',undefined,'news-art'),image=node('img');
|
|
// Reuse the editorial artwork register; never load arbitrary author URLs.
|
|
const approved=['world','news','maintenance','changelogs','events','community'];image.src=post.artwork==='assets/happy-beta-launch.png'?'/assets/happy-beta-launch.png':'/assets/news-'+(approved.includes(post.category)?post.category:'news')+'.webp';image.alt='';image.loading='lazy';art.href=href;art.setAttribute('aria-label',post.title);art.append(image);
|
|
const copy=node('div',undefined,'news-copy'),meta=node('div',undefined,'news-meta');meta.append(node('span',post.label||'News'));if(Number.isFinite(Number(post.published_at)))meta.append(node('time',date(post.published_at)));const heading=node('h3'),title=node('a',post.title);title.href=href;heading.append(title);const read=node('a','Read dispatch →','text-link');read.href=href;copy.append(meta,heading,node('p',excerpt(post.body)+'…'),read);card.append(art,copy);host.append(card);
|
|
}
|
|
if(!host.children.length)host.append(node('p','The next chapter is on its way. Published news will appear here.','news-loading'));
|
|
}catch{host.replaceChildren(node('div',undefined,'news-loading'));host.firstChild.append(node('span','THE IMPERIAL DISPATCH','eyebrow'),node('p','News is temporarily unavailable.'),Object.assign(node('a','Open the news archive →','text-link'),{href:'https://www.dunerealms.com/news-archive.html'}));}}
|
|
async function network(){try{const response=await fetch('https://cp.dunerealms.com/api/worlds',{credentials:'omit',cache:'no-store',signal:AbortSignal.timeout(7000)});if(!response.ok)throw Error();const data=await response.json(),proxy=data.servers?.find(s=>s.id==='velocity'),state=$('#network-state');state.classList.toggle('online',proxy?.state==='online');state.replaceChildren(node('i'),document.createTextNode(proxy?.state==='online'?'Network available':proxy?.state==='offline'?'Network offline':'Status unavailable'));}catch{const state=$('#network-state');state.replaceChildren(node('i'),document.createTextNode('Status unavailable'));}}
|
|
if($('#news-grid'))news();if($('#network-state')){network();setInterval(()=>{if(!document.hidden)network();},30000);}
|
|
async function staffContext(){try{const r=await fetch('https://cp.dunerealms.com/api/welcome',{credentials:'include',cache:'no-store',signal:AbortSignal.timeout(6000)});if(!r.ok)return;const context=await r.json();for(const link of all('[data-staff-access]'))link.hidden=context.staffAccess!==true;}catch{}}
|
|
staffContext();
|
|
})();
|

Staff