Files
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

36 lines
6.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(() => {
'use strict';
const app=document.getElementById('tasks-app');if(!app)return;
const ja=document.documentElement.lang.startsWith('ja'),t=(en,jp)=>ja?jp:en;
const el=(tag,value)=>{const n=document.createElement(tag);if(value!==undefined)n.textContent=value;return n;};
const panel=el('section');panel.id='tasks-focus';panel.ariaLabel='Pomodoro';
const heading=el('strong','Pomodoro'),state=el('span'),clock=el('output','25:00');state.id='tasks-focus-state';clock.id='tasks-focus-clock';clock.setAttribute('aria-live','off');
const phase=el('select');phase.ariaLabel=t('Phase','集中・休憩');[['focus',t('Focus','集中')],['break',t('Break','休憩')]].forEach(([v,label])=>{const o=el('option',label);o.value=v;phase.append(o);});
const minutes=el('input');minutes.type='number';minutes.min=1;minutes.max=180;minutes.value=25;minutes.ariaLabel=t('Minutes','分');
const buttons=el('div');buttons.className='tasks-actions';const actions={};[['start','▶',t('Start','開始')],['pause','Ⅱ',t('Pause','一時停止')],['resume','▶',t('Resume','再開')],['complete','✓',t('Finish session (not task)','セッションを終了(タスクは未完了のまま)')],['cancel','×',t('Cancel session','セッションを中断')]].forEach(([key,icon,label])=>{const b=el('button',icon);b.type='button';b.title=b.ariaLabel=label;b.dataset.action=key;b.addEventListener('click',()=>command(key));actions[key]=b;buttons.append(b);});
const link=el('a',t('Linked task','関連タスク'));link.hidden=true;const status=el('small');status.role='status';
panel.append(heading,state,clock,phase,minutes,el('span',t('min','分')),buttons,link,status);app.querySelector('.tasks-workspace').prepend(panel);
let current=null,loading=false,pendingStart=null,chosenTask='',serverAt=Date.now(),perfAt=performance.now(),ready=false,draftChoice=false;
const writable=app.dataset.writable==='true';
function remaining(){return !current?Number(minutes.value)*60000:current.state==='running'?Math.max(0,Math.min(current.remaining_ms,current.deadline_ms-serverAt-(performance.now()-perfAt))):current.remaining_ms;}
function render(){const active=current&&['running','paused'].includes(current.state);const ms=remaining(),secs=Math.ceil(ms/1000);clock.textContent=String(Math.floor(secs/60)).padStart(2,'0')+':'+String(secs%60).padStart(2,'0');
phase.disabled=minutes.disabled=!!active||loading||!writable||!!pendingStart;
for(const [key,b]of Object.entries(actions)){b.disabled=loading||!ready||!writable;b.hidden=key==='start'?!!active:key==='pause'?current?.state!=='running':key==='resume'?current?.state!=='paused':!active;}
if(current?.state==='running'&&ms<=0)actions.pause.disabled=true;
link.hidden=!current?.task_uuid;if(current?.task_uuid)link.href='/v/plan/task/'+encodeURIComponent(current.task_uuid);
panel.dataset.state=current?.state||'idle';
state.textContent=({running:t('Running','実行中'),paused:t('Paused','一時停止'),completed:t('Completed','終了'),cancelled:t('Cancelled','中断')})[current?.state]||'';
}
function accept(data){const previous=current;current=data.item;const active=current&&['running','paused'].includes(current.state);if(active)draftChoice=false;else if(draftChoice)current=null;serverAt=data.server_now_ms;perfAt=performance.now();ready=true;if(current&&(active||previous?.uuid!==current.uuid)){phase.value=current.phase;minutes.value=current.duration_ms/60000;}render();if(previous?.uuid!==current?.uuid||previous?.version!==current?.version)app.dispatchEvent(new Event('task-today-refresh'));}
async function request(options={}){const r=await fetch('/_/api/v1/extension/organizer.tasks/focus',{credentials:'same-origin',signal:AbortSignal.timeout(10000),...options,headers:{'Content-Type':'application/json'}});const d=await r.json().catch(()=>({}));if(!r.ok||r.redirected){const err=new Error(r.status===412?t('Session changed, or another Domain has an active timer. Review before retrying.','別の画面で変更されたか、他ドメインでタイマーが動作中です。状態を確認してください。'):t('Timer unavailable. Check permissions and extension settings.','タイマーを利用できません。権限や拡張の設定を確認してください。'));err.status=r.status;throw err;}return d;}
async function load(){if(loading)return;loading=true;try{accept(await request());}catch(e){status.textContent=e.message;ready=false;}finally{loading=false;render();}}
async function command(action){if(loading||!ready)return;if(action==='cancel'&&!confirm(t('Cancel this session?','このセッションを中断しますか?')))return;
let payload;if(action==='start'){if(!minutes.reportValidity())return;if(!pendingStart)pendingStart={uuid:crypto.randomUUID(),action,task_uuid:chosenTask,phase:phase.value,minutes:Number(minutes.value),version:0};payload=pendingStart;}else{if(!current)return;payload={uuid:current.uuid,action,version:current.version};}
loading=true;status.textContent='';render();try{accept(await request({method:'POST',body:JSON.stringify(payload)}));pendingStart=null;chosenTask='';status.textContent=t('Saved','保存しました');}catch(e){status.textContent=e.message;if(e.status>=400&&e.status<500){pendingStart=null;chosenTask='';}try{accept(await request());if(current?.uuid===pendingStart?.uuid){pendingStart=null;chosenTask='';}}catch{ready=false;}}finally{loading=false;render();}
}
phase.addEventListener('change',()=>{minutes.value=phase.value==='break'?5:25;if(!current||!['running','paused'].includes(current.state)){current=null;draftChoice=true;}render();});minutes.addEventListener('input',()=>{if(!current||!['running','paused'].includes(current.state)){current=null;draftChoice=true;}render();});
app.addEventListener('task-focus',async e=>{await load();if(pendingStart||(current&&['running','paused'].includes(current.state))){status.textContent=t('Resolve the pending request, or finish the current session first.','未完了の開始要求を再試行するか、現在のセッションを終了してください。');}else{chosenTask=e.detail.uuid;phase.value='focus';minutes.value=25;await command('start');}panel.scrollIntoView({block:'nearest',behavior:'smooth'});});
document.addEventListener('visibilitychange',()=>{if(!document.hidden)load();});
setInterval(()=>{if(!document.hidden)render();},1000);setInterval(()=>{if(!document.hidden)load();},30000);render();load();
})();