MC/website/web/exchange-cards.js

25 lines
6 KiB
JavaScript

import {safeItemArt,itemMaterialIcon} from './item-lore-model.mjs';
import {attachItemLore} from './item-lore-view.mjs';
const asset='https://www.dunerealms.com/assets/';
const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(cls)n.className=cls;return n;};
export const CARD_TYPES=Object.freeze({war:{label:'War proposal',art:'seals/office-landsraad.png',action:'View proposal'},auction:{label:'Auction',art:'seals/emperor-v1.png',action:'View auction'},item:{label:'Minecraft item',art:'minecraft-items/diamond_pickaxe.png',action:'View item'},kit:{label:'Equipment kit',art:'minecraft-items/leather_chestplate.png',action:'View kit'},job:{label:'Recruitment',art:'exchange-clan-recruitment-v1.png',action:'View vacancy'},ad:{label:'Advertisement',art:'nav-exchange-v2.webp',action:'View advertisement'},campaign:{label:'Election campaign',art:'seals/emperor-v1.png',action:'View candidature'},quest:{label:'Quest',art:'exchange-quests-v1.png',action:'View quest'},event:{label:'Event',art:'nav-home-v2.webp',action:'View event'},deed:{label:'Registered deed',art:'organisation-deeds-company-tiers-v1.png',action:'View deed'},key:{label:'Charter key',art:'organisation-deed-keys-v1.png',action:'View entitlement'}});
export function createListingCard(record,{onOpen=null,preview=false,actionLabel=null}={}){
if(!document.querySelector('link[data-exchange-cards]')){const css=el('link');css.rel='stylesheet';css.href=new URL('./exchange-cards.css',import.meta.url).href;css.dataset.exchangeCards='';document.head.append(css);}
const type=Object.hasOwn(CARD_TYPES,record.type)?record.type:'ad',definition=CARD_TYPES[type],card=el('article',undefined,'exchange-card exchange-card-'+type+(record.featured?' exchange-card-featured':''));card.dataset.cardType=type;
const heading=el('header'),label=el('small',preview?'DESIGN EXAMPLE · '+definition.label:definition.label);heading.append(label);if(record.featured)heading.append(el('span','Featured','exchange-card-badge'));card.append(heading);
const body=el('div',undefined,'exchange-card-body'),image=el('img');image.src=['item','kit'].includes(type)?safeItemArt(record.image,type==='item'?itemMaterialIcon(record.item?.material||record.material)||asset+definition.art:asset+definition.art):record.image||asset+definition.art;if(['item','kit'].includes(type))image.onerror=()=>{const fallback=type==='item'?itemMaterialIcon(record.item?.material||record.material):null;if(fallback&&image.src!==fallback){image.src=fallback;return;}image.onerror=null;image.hidden=true;};image.alt='';image.loading='lazy';image.width=image.height=96;if(['item','kit'].includes(type))image.className='minecraft-texture';const copy=el('div',undefined,'exchange-card-copy');copy.append(el('small',record.subtitle||record.owner||'Registered listing'),el('h3',record.title),el('p',record.message||''));if(['deed','key'].includes(type)){const artwork=el('div',undefined,'exchange-card-art-sprite exchange-card-art-'+type);artwork.setAttribute('role','img');artwork.setAttribute('aria-label',definition.label+' artwork');body.append(artwork,copy);}else body.append(['item','kit'].includes(type)&&!preview?attachItemLore(image,record.item||record):image,copy);card.append(body);
const facts=el('div',undefined,'exchange-card-facts');for(const text of record.facts||[])facts.append(el('span',text));if(facts.children.length)card.append(facts);
const footer=el('footer');if(record.price)footer.append(el('strong',record.price));else footer.append(el('small',record.duration||'Details on selection'));const open=el('button',actionLabel||definition.action);open.type='button';open.disabled=!onOpen;open.onclick=()=>onOpen?.(record);footer.append(open);card.append(footer);return card;
}
export function createCardGallery(){const root=el('section',undefined,'exchange-card-gallery');root.append(el('p','Design examples only · no listings, candidatures, charges or rewards are created.','exchange-card-gallery-note'));const grid=el('div',undefined,'exchange-card-gallery-grid');for(const sample of [
{type:'campaign',title:'A voice for the Landsraad',subtitle:'Example candidate · Clan Delegate',message:'A campaign message belongs here, beside the candidate portrait and organisation seal.',featured:true,duration:'1 hour campaign placement',facts:['Clan Delegate','Campaign closes at voting start']},
{type:'auction',title:'Office appointment auction',subtitle:'Example office · tied election',message:'A tied election opens a three-hour auction. No real bids are accepted in this design example.',duration:'3 hour auction',facts:['Highest bid: no live bid','Closes: shown when opened']},
{type:'item',title:'Diamond Pickaxe',subtitle:'Example seller',message:'Actual Minecraft item artwork.',price:'25.00 DM',facts:['Rare','MK I','Alpha · example edition']},
{type:'kit',title:'House Expedition Kit',subtitle:'Clan kit · example',message:'Armour, tools and equipment contents are shown after selection.',price:'75.00 DM',facts:['Clan kit','MK I','Rare']},
{type:'job',title:'Join our caravan',subtitle:'Example household',message:'Role, homeworld and vacancy details.',facts:['Recruitment','Apply through J.A.R.V.I.S.']},
{type:'ad',title:'Trade across the stars',subtitle:'Example Company',message:'A branded advertisement with a clear call to action.',duration:'1 hour promotion',featured:true},
{type:'quest',title:'Supplies for the expedition',subtitle:'Example issuer',message:'Objective and reward summary.',facts:['Reward shown on details','Example quest']},
{type:'event',title:'The caravan assembles',subtitle:'Example organiser',message:'Event details and registration summary.',facts:['Date to be announced']},
{type:'deed',title:'Company charter',subtitle:'Example registered deed',message:'Ownership and charter details.',facts:['Royal · example']},
{type:'key',title:'Silver charter key',subtitle:'Example entitlement',message:'Public information only; private codes are never shown on cards.',facts:['Keyed upgrade · example']}
])grid.append(createListingCard(sample,{preview:true}));root.append(grid);return root;}