MC/website/scripts/main-menu.mjs

17 lines
8.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export const MAIN_MENU=[["Home","https://www.dunerealms.com/","nav-home-v2.webp"],["Discover","https://wiki.dunerealms.com/","nav-manual-v2.webp"],["Community","https://forums.dunerealms.com/","nav-boards-v2.webp"],["Trade","https://www.dunerealms.com/exchange.html","nav-exchange-v2.webp"],["The realm","https://www.dunerealms.com/realm-hub.html","nav-clan-v2.webp"],["My Account","https://cp.dunerealms.com/","nav-account-v2.webp"]];
export const MAIN_MENU_GROUPS={"Discover":[["Explore worlds","https://www.dunerealms.com/#worlds","nav-home-v2.webp"],["Let’s Play Manual","https://www.dunerealms.com/lets-play.html","nav-manual-v2.webp"],["Imperial Recipe Book","https://www.dunerealms.com/imperial-recipe-book.html","nav-manual-v2.webp"]],"Community":[["Forums","https://forums.dunerealms.com/","nav-boards-v2.webp"],["Discord","https://www.dunerealms.com/discord.html","nav-discord-v2.webp"],["People of the realm","https://www.dunerealms.com/staff.html","nav-staff-v2.webp"],["News & updates","https://www.dunerealms.com/news-archive.html","nav-boards-v2.webp"],["Events","https://cp.dunerealms.com/#events/events-calendar","nav-home-v2.webp"],["Latest bans & appeals","https://www.dunerealms.com/community-records.html","nav-support-v2.webp"]],"Trade":[["Player Exchange","https://www.dunerealms.com/exchange.html","nav-exchange-v2.webp"],["Equipment & kits","https://www.dunerealms.com/exchange.html?type=kits","imperial-atelier-v1.webp"],["Imperial Postbank","https://cp.dunerealms.com/#bank/bank-overview","imperial-postbank-v2.png"],["Community shop","https://shop.dunerealms.com/","nav-shop-v1.webp"]],"The realm":[["Clans","https://www.dunerealms.com/realm-hub.html#clan","nav-clan-v2.webp"],["Companies","https://www.dunerealms.com/realm-hub.html#company","exchange-company-jobs-v1.png"],["Politics & offices","https://www.dunerealms.com/realm-hub.html#politics","seals/office-landsraad.png"]],"My Account":[["My dashboard","https://cp.dunerealms.com/","nav-account-v2.webp"],["Mail","https://cp.dunerealms.com/#post/mail-inbox","nav-support-v2.webp"],["Quests","https://clans.dunerealms.com/#quests/quests","exchange-quests-v1.png"],["Support","https://support.dunerealms.com/","nav-support-v2.webp"],["Staff sign in","https://cp.dunerealms.com/#bureau/user-management","nav-staff-v2.webp"]]};
MAIN_MENU_GROUPS['My Account'].push(['DuneForge','https://cp.dunerealms.com/#forge/git-workspace','nav-manual-v2.webp']);
export const menuLeafLinks=rows=>rows.flatMap(row=>Array.isArray(row[3])?menuLeafLinks(row[3]):[row]);
export const ALL_MENU_LINKS=[...MAIN_MENU,...menuLeafLinks(Object.values(MAIN_MENU_GROUPS).flat())].filter((row,i,rows)=>rows.findIndex(r=>r[1]===row[1])===i);
export function canonicalMenuHref(value){const u=new URL(value,'https://www.dunerealms.com');if(u.hostname==='dunerealms.com')u.hostname='www.dunerealms.com';if(u.hostname==='www.dunerealms.com'&&/^\/(manual|wiki)\.html$/.test(u.pathname))return'https://wiki.dunerealms.com/';if(u.hostname==='cp.dunerealms.com'&&u.pathname==='/'){if(u.hash==='#shop')return'https://shop.dunerealms.com/';if(u.hash==='#exchange')return'https://www.dunerealms.com/exchange.html';}if(['forums','support','cp','clans','corp','wiki'].some(h=>u.hostname===h+'.dunerealms.com'))return u.origin+'/';const kits=u.pathname==='/exchange.html'&&/^kits(?:-(?:clan|office|player|legendary))?$/.test(u.searchParams.get('type')||'');return u.origin+(u.pathname==='/index.html'?'/':u.pathname)+(kits?'?type=kits':'');}
export function normalizeMainMenus(doc,currentUrl,links=MAIN_MENU,groups=MAIN_MENU_GROUPS,authenticated=true){let changes=0;const set=(node,key,value)=>{if(node.getAttribute(key)!==value){node.setAttribute(key,value);changes++;}};
const fill=(node,name,href,icon,link=true)=>{if(link)set(node,'href',href);let image=node.querySelector('img');if(!image){image=doc.createElement('img');node.insertBefore(image,node.firstChild);changes++;}for(const[k,v]of [['src','https://www.dunerealms.com/assets/'+icon],['alt',''],['width','42'],['height','42']])set(image,k,v);let label=[...node.children].find(n=>n.tagName==='SPAN'&&!n.classList.contains('dune-new-overlay')&&!n.classList.contains('dune-sales-overlay'));if(!label){label=doc.createElement('span');node.append(label);changes++;}set(label,'data-dune-nav-label','1');if(label.textContent!==name){label.textContent=name;changes++;}for(const n of [...node.childNodes])if(n.nodeType===3){n.remove();changes++;}const here=new URL(currentUrl,'https://www.dunerealms.com'),there=new URL(href,'https://www.dunerealms.com');const active=link&&(there.hash||there.search?here.href===there.href:canonicalMenuHref(currentUrl)===canonicalMenuHref(href));if(active)set(node,'aria-current','page');else if(node.hasAttribute('aria-current')){node.removeAttribute('aria-current');changes++;}};
for(const nav of doc.querySelectorAll('header nav[aria-label="Main navigation"]')){const existing=[...nav.children],ordered=[];for(const[name,href,icon]of links){const items=name==='My Account'&&!authenticated?[['Sign in','https://cp.dunerealms.com/','nav-account-v2.webp'],['Register','https://cp.dunerealms.com/#register','nav-account-v2.webp']]:groups[name];if(items){let group=existing.find(n=>n.getAttribute('data-dune-nav-group')===name);if(!group){group=doc.createElement('details');group.className='dune-menu-group';set(group,'data-dune-nav-group',name);group.append(doc.createElement('summary'));const box=doc.createElement('div');box.className='dune-menu-dropdown';group.append(box);changes++;}fill(group.firstElementChild,name,href,icon,false);const syncItems=(box,items)=>{const previous=[...box.children],children=[];for(const child of items){const nested=Array.isArray(child[3]);let node=previous.find(n=>!children.includes(n)&&(nested?n.getAttribute('data-dune-submenu')===child[0]:n.tagName==='A'&&n.getAttribute('href')===child[1]));if(!node){node=doc.createElement(nested?'details':'a');if(nested){node.className='dune-menu-group dune-menu-subgroup';set(node,'data-dune-submenu',child[0]);node.append(doc.createElement('summary'));const inner=doc.createElement('div');inner.className='dune-menu-dropdown';node.append(inner);}changes++;}if(nested){fill(node.firstElementChild,child[0],currentUrl,child[2],false);syncItems(node.lastElementChild,child[3]);}else fill(node,...child);children.push(node);}for(const node of previous)if(!children.includes(node)){node.remove();changes++;}let cursor=box.firstElementChild;for(const node of children){if(node!==cursor){box.insertBefore(node,cursor);changes++;}cursor=node.nextElementSibling;}};syncItems(group.lastElementChild,items);ordered.push(group);}else{let a=existing.find(n=>n.tagName==='A'&&n.getAttribute('href')===href);if(!a){a=doc.createElement('a');changes++;}fill(a,name,href,icon);ordered.push(a);}}for(const node of existing)if(!ordered.includes(node)){node.remove();changes++;}let cursor=nav.firstElementChild;for(const node of ordered){if(node!==cursor){nav.insertBefore(node,cursor);changes++;}cursor=node.nextElementSibling;}}return changes;}
export const CANONICAL_MENU_SCRIPT='\n/* Dune canonical menu begin */\n(()=>{if(window.top!==window.self)return;const MAIN_MENU='+JSON.stringify(MAIN_MENU)+';const MAIN_MENU_GROUPS='+JSON.stringify(MAIN_MENU_GROUPS)+';const canonicalMenuHref='+canonicalMenuHref.toString()+';const normalizeMainMenus='+normalizeMainMenus.toString()+';let authenticated=true;const sync=()=>normalizeMainMenus(document,location.href,MAIN_MENU,MAIN_MENU_GROUPS,authenticated);sync();let pending=false;new MutationObserver(()=>{if(pending)return;pending=true;requestAnimationFrame(()=>{pending=false;sync();});}).observe(document.body,{childList:true,subtree:true});document.addEventListener("click",e=>{for(const group of document.querySelectorAll(".dune-menu-group[open]"))if(!group.contains(e.target))group.open=false;});document.addEventListener("keydown",e=>{if(e.key!=="Escape")return;const group=e.target.closest?.(".dune-menu-group[open]")||document.querySelector(".dune-menu-group[open]");if(group){e.preventDefault();group.open=false;group.firstElementChild.focus();}});})();\n/* Dune canonical menu end */\n';
export function withCanonicalMenu(script){
// Older published assets may contain a generated controller without its marker comments.
// Remove both forms before installing the single current controller.
return script.replace(/\(\(\)=>\{if\(window\.top!==window\.self\)return;const MAIN_MENU=[\s\S]*?\}\)\(\);/g,'').replace(/\n?\/\* Dune canonical menu begin \*\/[^]*?\/\* Dune canonical menu end \*\/\n?/g,'')
.replace(/\(\(\)=>\{if\(window\.top!==window\.self\)return;const MAIN_MENU=[^]*?const sync=\(\)=>normalizeMainMenus\(document,location\.href,MAIN_MENU\);sync\(\);new MutationObserver\(sync\)\.observe\(document\.body,\{childList:true,subtree:true\}\);\}\)\(\);/g,'')+CANONICAL_MENU_SCRIPT;
}