39 lines
6.0 KiB
JavaScript
39 lines
6.0 KiB
JavaScript
(() => {
|
|
for(const root of document.querySelectorAll('[data-writer-work]')){
|
|
const start=root.querySelector('[data-work-start]'),message=root.querySelector('[data-work-message]'),token=start.querySelector('[name=__RequestVerificationToken]').value;
|
|
const bookId=root.dataset.bookId;let current=null,busy=false,lastInput=0,startRequest=null,lastChoice=null;
|
|
const notify=text=>{message.textContent=text;};
|
|
const post=async(action,values)=>{const body=new URLSearchParams({bookId,__RequestVerificationToken:token,...values});const response=await fetch('/Writer/Work/'+action,{method:'POST',body});if(!response.ok){let data;try{data=await response.json();}catch{}throw Error(data?.message||'The session could not be updated. Check access and Reports recording settings.');}return response;};
|
|
for(const type of ['keydown','pointerdown','wheel','touchstart'])document.addEventListener(type,event=>{if(event.isTrusted)lastInput=Date.now();},{passive:true});
|
|
const render=session=>{
|
|
const changed=current?.sessionID!==session?.sessionID;current=session;
|
|
root.querySelector('[data-work-current]').hidden=!session;start.hidden=!!session;
|
|
root.querySelector('[data-work-audio]').hidden=session?.kind!=='AudioReview';
|
|
if(!session)return;
|
|
root.querySelector('[data-work-end]').textContent=session.kind==='AudioReview'?'End Audio Review & save note':'End session & save note';
|
|
root.querySelector('[data-work-label]').textContent=session.kind+' session';
|
|
root.querySelector('[data-work-elapsed]').textContent=' · '+Math.max(0,Math.round((Date.now()-new Date(session.startedUtc))/60000))+' minutes elapsed';
|
|
root.querySelector('[data-work-measured]').textContent=' · '+Math.floor(session.engagedSeconds/60)+' browser-engaged minutes';
|
|
if(changed)root.querySelector('[data-work-note]').value='';
|
|
if(session.kind==='AudioReview'){
|
|
root.querySelector('[data-audio-chapter]').textContent=session.chapterTitle||'Selected chapter';
|
|
root.querySelector('[data-audio-state]').textContent=session.live?session.playbackState+(session.lastError?' · '+session.lastError:''):'Media controls unavailable. Updated Windows Companion with Audio Review support is required.';
|
|
root.querySelector('[data-audio-position]').textContent=(session.positionSeconds==null?'Position unavailable':Math.round(session.positionSeconds)+' seconds'+(session.durationSeconds==null?'':' / '+Math.round(session.durationSeconds)+' seconds'))+' · '+Math.floor(session.playbackSeconds/60)+' observed playback minutes · '+Math.floor(session.uniqueSeconds/60)+' unique coverage minutes'+(session.coveragePartial?' (partial)':'');
|
|
root.querySelector('[data-pause-on-edit]').checked=session.pauseOnEdit;
|
|
for(const button of root.querySelectorAll('[data-media-command]'))button.disabled=!session.live||['unavailable','error'].includes(session.playbackState)||!session.capabilities?.split(',').includes(button.dataset.mediaCommand)||busy;
|
|
}
|
|
};
|
|
const refresh=async()=>{const response=await fetch('/Writer/Work/State?bookId='+encodeURIComponent(bookId));if(!response.ok)throw Error('Session history is private or temporarily unavailable.');const data=await response.json();render(data.session);if(!data.session)root.querySelector('[data-pause-on-edit]').checked=data.defaultPauseOnEdit;};
|
|
start.querySelector('[name=kind]').addEventListener('change',event=>{start.querySelector('button[type=submit]').textContent=event.target.value==='AudioReview'?'Start Audio Review':'Start session';});
|
|
start.addEventListener('submit',async event=>{event.preventDefault();if(busy)return;busy=true;try{const data=new FormData(start);const kind=data.get('kind'),chapterId=data.get('chapterId');if(kind==='AudioReview'&&!chapterId)throw Error('Choose a chapter for Audio Review.');const choice=kind+'/'+chapterId;if(lastChoice!==choice){startRequest=null;lastChoice=choice;}startRequest??=crypto.randomUUID();await post('Start',{kind,chapterId:chapterId||'',requestId:startRequest,pauseOnEdit:String(root.querySelector('[data-pause-on-edit]').checked)});startRequest=null;lastInput=Date.now();await refresh();notify('Session started. Leave it open only while you want to record this activity.');}catch(error){notify(error.message);}finally{busy=false;}});
|
|
root.querySelector('[data-work-end]').addEventListener('click',async()=>{if(!current||busy)return;busy=true;try{await post('End',{sessionId:current.sessionID,note:root.querySelector('[data-work-note]').value});await refresh();notify('Session ended. Your note was saved.');}catch(error){notify(error.message);}finally{busy=false;}});
|
|
root.querySelector('[data-pause-on-edit]').addEventListener('change',async event=>{if(!current)return;try{await post('Preference',{sessionId:current.sessionID,pauseOnEdit:String(event.target.checked)});}catch(error){notify(error.message);}});
|
|
for(const button of root.querySelectorAll('[data-media-command]'))button.addEventListener('click',async()=>{if(!current||busy)return;busy=true;try{await post('Command',{sessionId:current.sessionID,command:button.dataset.mediaCommand});notify('Command requested; waiting for the selected media session.');}catch(error){notify(error.message);}finally{busy=false;}});
|
|
let tick=0;const timer=setInterval(async()=>{if(busy||document.visibilityState!=='visible')return;busy=true;try{if(current&&++tick%3===0)await post('Heartbeat',{sessionId:current.sessionID,sequence:current.sequence+1,engaged:String(document.hasFocus()&&Date.now()-lastInput<60000)});await refresh();}catch(error){notify(error.message);}finally{busy=false;}},10000);
|
|
window.addEventListener('pagehide',()=>clearInterval(timer),{once:true});
|
|
refresh().catch(error=>notify(error.message));
|
|
}
|
|
document.querySelector('[data-writer-help]')?.addEventListener('click',event=>{document.getElementById('writer-help-options').hidden=false;event.currentTarget.setAttribute('aria-expanded','true');});
|
|
document.querySelector('[data-writer-help-close]')?.addEventListener('click',()=>{document.getElementById('writer-help-options').hidden=true;document.querySelector('[data-writer-help]').setAttribute('aria-expanded','false');document.querySelector('[data-writer-help]').focus();});
|
|
})();
|