web/website/scripts/main-menu.mjs

15 lines
7.8 KiB
JavaScript

export const MAIN_MENU=[['Home','https://www.dunerealms.com/','nav-home-v2.webp'],['Shop','https://shop.dunerealms.com/','nav-shop-v1.webp'],['Community','https://www.dunerealms.com/staff.html','nav-boards-v2.webp'],['Starport','https://www.dunerealms.com/exchange.html','nav-exchange-v2.webp'],['Realm Hub','https://www.dunerealms.com/realm-hub.html','nav-clan-v2.webp'],['My Account','https://cp.dunerealms.com/','nav-account-v2.webp'],['Support','https://support.dunerealms.com/','nav-support-v2.webp'],['Wiki','https://wiki.dunerealms.com/','nav-manual-v2.webp']];
export const MAIN_MENU_GROUPS={Community:[['Forums','https://forums.dunerealms.com/','nav-boards-v2.webp'],['Discord','https://www.dunerealms.com/discord.html','nav-discord-v2.webp'],['Staff & Teams','https://www.dunerealms.com/staff.html','nav-staff-v2.webp'],['Events','https://cp.dunerealms.com/#events/events-calendar','nav-home-v2.webp']],Starport:[['Browse Exchange','https://www.dunerealms.com/exchange.html','nav-exchange-v2.webp'],['Items','https://www.dunerealms.com/exchange.html?type=items','minecraft-items/diamond.png'],['Kits','https://www.dunerealms.com/exchange.html?type=kits','imperial-atelier-v1.webp'],['Recruitment','https://www.dunerealms.com/exchange.html?type=recruitment','exchange-clan-recruitment-v1.png'],['Political Positions','https://www.dunerealms.com/exchange.html?type=politics','exchange-political-positions-v1.png'],['Auctions','https://www.dunerealms.com/exchange.html?type=auctions','nav-exchange-v2.webp']],'Realm Hub':[['Clans & Houses','https://clans.dunerealms.com/','nav-clan-v2.webp'],['Companies','https://corp.dunerealms.com/','exchange-company-jobs-v1.png'],['Politics & Offices','https://www.dunerealms.com/government.html','seals/office-landsraad.png']],'My Account':[['User CP','https://cp.dunerealms.com/','nav-account-v2.webp'],['Mail','https://cp.dunerealms.com/#post/mail-inbox','imperial-postbox-v1.png'],['Imperial Postbank','https://cp.dunerealms.com/#bank/bank-overview','imperial-postbank-v2.png'],['Rihla Transfers','https://cp.dunerealms.com/#realm/rihla','rihla-brand-v1.png'],['Quest Dashboard','https://cp.dunerealms.com/#quests/quest-dashboard','exchange-quests-v1.png'],['Events','https://cp.dunerealms.com/#events/events-calendar','nav-home-v2.webp'],['Imperial Bureau','https://cp.dunerealms.com/#bureau/user-management','nav-staff-v2.webp']]};
export const ALL_MENU_LINKS=[...MAIN_MENU,...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 box=group.lastElementChild,previous=[...box.children],children=[];for(const child of items){let a=previous.find(n=>!children.includes(n)&&n.getAttribute('href')===child[1]);if(!a){a=doc.createElement('a');changes++;}fill(a,...child);children.push(a);}for(const a of previous)if(!children.includes(a)){a.remove();changes++;}let cursor=box.firstElementChild;for(const a of children){if(a!==cursor){box.insertBefore(a,cursor);changes++;}cursor=a.nextElementSibling;}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();new MutationObserver(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;for(const group of document.querySelectorAll(".dune-menu-group[open]")){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;
}