PlotDirector/PlotLine/wwwroot/js/writer-objective.js

23 lines
5.4 KiB
JavaScript

(() => {
const root=document.querySelector('[data-writer-focus]'); if(!root)return;
const host=root.querySelector('#objective-setup');let loading=false;
window.addEventListener('beforeunload',event=>{const form=host.querySelector('[data-focus-wizard]');if(!host.hidden&&form&&(form.dataset.dirty==='true'||form.dataset.busy==='true')){event.preventDefault();event.returnValue='';}});
function initialise(){const form=host.querySelector('[data-focus-wizard]');if(!form)return;let step=0,busy=false;const fields=['Activity','ChapterID','SceneID','Intention'];let savedFields=Object.fromEntries(fields.map(name=>[name,form.elements[name].value]));
form.addEventListener('input',()=>form.dataset.dirty='true');form.addEventListener('change',()=>form.dataset.dirty='true');
const chapter=form.querySelector('[data-objective-chapter]'),scene=form.querySelector('[data-objective-scene]');
function filter(){for(const o of scene.options){o.hidden=!!o.value&&o.dataset.chapter!==chapter.value;o.disabled=o.hidden;}if(scene.selectedOptions[0]?.disabled)scene.value='';scene.disabled=!chapter.value;}
function render(){form.querySelectorAll('[data-focus-step]').forEach((x,i)=>x.hidden=i!==step);form.querySelector('[data-focus-back]').hidden=step===0;form.querySelector('[data-focus-next]').hidden=step===2;form.querySelector('[data-save-focus]').hidden=step!==2;form.querySelector('[data-focus-step-label]').textContent=`Step ${step+1} of 3`;form.querySelector('[data-focus-confirmation]').textContent=`Save ${form.elements.Activity.value.toLowerCase()} · ${scene.value?scene.selectedOptions[0].textContent:chapter.selectedOptions[0].textContent}. No session will be recorded.`;}
chapter.addEventListener('change',filter);filter();render();
for(const [selector,direction] of [['[data-focus-next]',1],['[data-focus-back]',-1]])form.querySelector(selector).addEventListener('click',()=>{step+=direction;render();form.querySelector('[data-focus-step]:not([hidden]) select, [data-focus-step]:not([hidden]) textarea')?.focus();});
form.querySelector('[data-focus-cancel]').addEventListener('click',()=>{if(busy)return;for(const name of fields)form.elements[name].value=savedFields[name];filter();step=0;render();form.dataset.dirty='false';host.hidden=true;const change=root.querySelector('[data-change-focus]');if(change)change.hidden=false;root.querySelector('[data-change-focus]')?.focus();});
form.addEventListener('submit',async e=>{e.preventDefault();if(e.defaultPrevented&&e.cancelBubble)return;if(busy)return;if(step!==2){step++;render();return;}busy=true;form.dataset.busy='true';form.querySelector('[data-focus-cancel]').disabled=true;const button=form.querySelector('[data-save-focus]');button.disabled=true;form.querySelector('[data-focus-error]').textContent='';try{const response=await fetch(form.action,{method:'POST',body:new FormData(form),headers:{'X-Requested-With':'XMLHttpRequest'}});if(!response.ok)throw Error();const data=await response.json();if(!data.saved)throw Error();
savedFields={Activity:data.activity,ChapterID:String(data.chapterID||''),SceneID:String(data.sceneID||''),Intention:data.intention||''};
root.dataset.savedActivity=data.activity;root.dataset.savedChapter=data.chapterID||'';root.dataset.savedSessionKind=data.sessionKind||'';
let summary=root.querySelector('[data-focus-summary]');if(!summary){summary=document.createElement('div');host.before(summary);}summary.replaceChildren();const h=document.createElement('h2');h.textContent=data.activity+' · '+data.title;const p=document.createElement('p');p.textContent=data.intention||'Your objective is saved. No recording has started.';const a=document.createElement('a');a.href=data.continueUrl;a.className='btn btn-primary';a.textContent=data.sceneID?'Open selected scene workspace':data.chapterID?'Open selected chapter workspace':'Open chapters';summary.append(h,p,a);form.dataset.dirty='false';host.hidden=true;const change=root.querySelector('[data-change-focus]');if(change)change.hidden=false;busy=false;form.dataset.busy='false';form.querySelector('[data-focus-cancel]').disabled=false;button.disabled=false;
const target=root.querySelector('[data-objective-location]');if(target){target.dataset.chapter=data.chapterID||'';target.dataset.scene=data.sceneID||'';target.textContent='Objective target: '+data.title+'. This target changes only when you edit the objective.';}const go=root.querySelector('[data-go-objective]');if(go){go.dataset.chapter=data.chapterID||'';go.dataset.scene=data.sceneID||'';go.hidden=!data.chapterID;}
window.dispatchEvent(new CustomEvent('plotdirector:focus-saved',{detail:data}));summary.tabIndex=-1;summary.focus();}catch{form.querySelector('[data-focus-error]').textContent='Your objective could not be confirmed. Your choices are retained; please try again.';busy=false;form.dataset.busy='false';form.querySelector('[data-focus-cancel]').disabled=false;button.disabled=false;}});
}
root.querySelector('[data-change-focus]')?.addEventListener('click',async e=>{e.preventDefault();if(loading)return;e.currentTarget.hidden=true;if(host.querySelector('form')){host.hidden=false;return;}loading=true;host.hidden=false;host.textContent='Loading your focus…';try{const r=await fetch(root.dataset.focusUrl);if(!r.ok)throw Error();host.innerHTML=await r.text();initialise();}catch{host.textContent='Focus is temporarily unavailable. Try Change focus again.';const change=root.querySelector('[data-change-focus]');if(change)change.hidden=false;}finally{loading=false;}});
initialise();
})();