PlotDirector/PlotLine/wwwroot/js/writing-context-components.js

51 lines
6.4 KiB
JavaScript

(() => {
'use strict';
const root=document.querySelector('[data-writing-context]');if(!root)return;
function explore(host,index){host.querySelectorAll('[data-context-scene-card]').forEach(card=>card.hidden=Number(card.dataset.contextSceneCard)!==index);const range=host.querySelector('[data-context-scrubber]');if(range)range.value=String(index);}
function refresh(host){
if(host.querySelector('[data-context-plot-cards]')){window.PlotDirectorContextPlots?.refresh(host);return;}
window.PlotDirectorContextMap?.refresh(host);
const current=root.querySelector('[data-writing-context-fragment]')?.dataset.sceneId;
const currentColumn=[...host.querySelectorAll('[data-context-chart-scene]')].find(node=>node.dataset.contextChartScene===current);
host.querySelectorAll('[data-context-chart-scene]').forEach(node=>node.setAttribute('aria-current',node===currentColumn?'location':'false'));
const index=Number(currentColumn?.dataset.sceneIndex),chart=host.querySelector('[data-context-chart]');
const spacing=host.querySelector('[data-context-spacing]');if(spacing&&chart)chart.style.setProperty('--scene-width',spacing.value+'px');
const marker=host.querySelector('[data-context-current-marker]');if(marker){marker.hidden=!currentColumn;if(currentColumn)marker.style.left=`calc(160px + (${index} + .5) * var(--scene-width,64px))`;}
host.querySelectorAll('[data-context-line-filter]').forEach(input=>{const lane=host.querySelector(`[data-context-line="${input.dataset.contextLineFilter}"]`);if(lane)lane.hidden=!input.checked;});
const metric=host.querySelector('[data-context-metric-select]');host.querySelectorAll('[data-context-metric]').forEach(panel=>panel.hidden=metric&&panel.dataset.contextMetric!==metric.value);
if(host.dataset.explored&&host.querySelector('[data-context-scrubber]'))explore(host,Number(host.querySelector('[data-context-scrubber]').value));
const currentSignature=current+':'+index;
if(currentColumn&&(!host.dataset.explored||host.dataset.currentSignature!==currentSignature)&&host.dataset.exploring!=='true'){
host.dataset.explored='true';explore(host,index);host.dataset.centerPending='true';
}
host.dataset.currentSignature=currentSignature;
if(host.dataset.centerPending&&!host.closest('[data-writing-context-panel]')?.hidden){center(host);delete host.dataset.centerPending;}
}
function center(host){const marker=host.querySelector('[data-context-current-marker]'),scroll=host.querySelector('.writing-story-scroll');if(marker&&!marker.hidden&&scroll)scroll.scrollLeft=Math.max(0,marker.offsetLeft-scroll.clientWidth/2);}
async function load(host){
if(host.dataset.loaded||host.dataset.loading)return;
host.dataset.loading='true';host.setAttribute('aria-busy','true');const requestedUrl=host.dataset.componentUrl;host.style.minHeight=host.clientHeight+'px';
let loading=host.querySelector('[data-writing-loading]');if(!loading){loading=document.createElement('p');loading.dataset.writingLoading='';loading.className='small text-muted';loading.setAttribute('role','status');host.prepend(loading);}loading.textContent='Loading saved information…';
try{
const response=await fetch(host.dataset.componentUrl,{signal:AbortSignal.timeout(15000),headers:{'X-Requested-With':'XMLHttpRequest'}});if(!response.ok||response.redirected)throw Error();
const fragment=window.PlotDirectorWritingFollow.fragmentFromHtml(await response.text(),'[data-writing-component-fragment]');
if(!host.isConnected||host.dataset.componentUrl!==requestedUrl)return;root.dispatchEvent(new CustomEvent('writing-view-before-refresh'));host.replaceChildren(fragment);host.dataset.loaded='true';window.PlotDirectorContextMap?.mount(host);root.dispatchEvent(new CustomEvent('writing-component-loaded'));refresh(host);
}catch{if(host.isConnected&&host.dataset.componentUrl===requestedUrl){const p=document.createElement('p');p.textContent='Saved information could not be loaded. Your current scene is unchanged.';const retry=document.createElement('button');retry.type='button';retry.className='btn btn-sm btn-outline-secondary';retry.textContent='Retry';retry.addEventListener('click',()=>void load(host));host.replaceChildren(p,retry);}}
finally{delete host.dataset.loading;host.removeAttribute('aria-busy');host.style.minHeight='';if(host.isConnected&&host.dataset.componentUrl!==requestedUrl)update();}
}
function update(){for(const zoom of root.querySelectorAll('[data-context-floor-zoom]')){const floor=zoom.closest('article').querySelector('.writing-context-floor');if(floor)floor.style.setProperty('--floor-zoom',Number(zoom.value)/100);}
for(const host of root.querySelectorAll('[data-writing-component]')){if(!host.closest('[data-writing-context-panel]')?.hidden)void load(host);if(host.dataset.loaded)refresh(host);}}
root.addEventListener('writing-view-activated',update);root.addEventListener('writing-fragment-ready',update);
for(const event of ['wheel','pointerdown','keydown'])root.addEventListener(event,e=>{const scroll=e.target.closest('.writing-story-scroll');if(scroll){const host=scroll.closest('[data-writing-component]');if(host)host.dataset.exploring='true';}},{passive:true});
root.addEventListener('input',event=>{if(event.target.matches('[data-context-floor-zoom]')){update();return;}const host=event.target.closest('[data-writing-component]');if(!host)return;if(event.target.matches('[data-context-scrubber]')){host.dataset.exploring='true';explore(host,Number(event.target.value));}else refresh(host);});
root.addEventListener('change',event=>{const host=event.target.closest('[data-writing-component]');if(host)refresh(host);});
root.addEventListener('click',event=>{const host=event.target.closest('[data-writing-component]');if(!host)return;
const target=event.target.closest('button');if(!target)return;
if(target.hasAttribute('data-context-refresh')){root.dispatchEvent(new CustomEvent('writing-view-before-refresh'));delete host.dataset.loaded;void load(host);}
if(target.hasAttribute('data-context-explore')){host.dataset.exploring='true';explore(host,Number(target.dataset.contextExplore));}
if(target.hasAttribute('data-context-center')){delete host.dataset.exploring;delete host.dataset.currentSignature;refresh(host);center(host);}
if(target.hasAttribute('data-context-go-scene')){delete host.dataset.exploring;delete host.dataset.currentSignature;const select=root.querySelector('[data-writing-context-scene-select]');if(select&&[...select.options].some(o=>o.value===target.dataset.contextGoScene)){select.value=target.dataset.contextGoScene;}}
});
})();