Files
pages/static/js/task-dav-settings.js
T
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

70 lines
8.4 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') || document.getElementById('calendar-app');
if (!app) return;
const calendar = app.id === 'calendar-app';
if (calendar && !app.dataset.actor) return;
const ja = document.documentElement.lang.startsWith('ja');
const t = (en, jp) => ja ? jp : en;
const api = '/_/api/v1/caldav-credentials';
const el = (tag, text) => { const n = document.createElement(tag); if (text !== undefined) n.textContent = text; return n; };
const button = el('button', '⇄'); button.type = 'button'; button.id = 'tasks-dav-open';
button.title = button.ariaLabel = t('CalDAV / CLI access', 'CalDAV / CLI連携');
app.querySelector(calendar ? '.calendar-toolbar' : '.tasks-toolbar .tasks-actions').append(button);
const dialog = el('dialog'); dialog.id = 'tasks-dav-dialog'; dialog.setAttribute('aria-labelledby', 'tasks-dav-title');
const header = el('header'); header.className = 'tasks-toolbar';
const title = el('h2', t('CalDAV access', '予定・タスクのCalDAV連携')); title.id = 'tasks-dav-title';
const close = el('button', '×'); close.type = 'button'; close.ariaLabel = t('Close', '閉じる'); header.append(title, close);
const help = el('p', t('Choose calendars, tasks, or both. Calendar permissions still apply. Existing task passwords remain task-only. Wiki access is not granted. Use HTTPS.', '予定・タスクの対象を選んで発行します。予定には既存のカレンダー権限が適用されます。従来のパスワードはタスク専用のままです。Wikiへのアクセス権は付与されません。HTTPSで接続してください。'));
const url = el('input'); url.readOnly = true; url.value = location.origin + '/_/api/v1/caldav/'; url.ariaLabel = 'CalDAV URL'; url.onclick = () => url.select();
const form = el('form'); const label = el('input'); label.required = true; label.maxLength = 100; label.placeholder = t('Device / client name', '端末・クライアント名'); label.ariaLabel = label.placeholder;
const access = el('select'); access.ariaLabel = t('Access', 'アクセス権限');
for (const [value, text] of [['read', t('Read only', '読み取り専用')], ['write', t('Read / write', '読み書き')]]) { const option = el('option', text); option.value = value; if (value === 'write' && !calendar && app.dataset.writable !== 'true') option.disabled = true; access.append(option); }
const scope = el('select'); scope.ariaLabel = t('Scope', '対象');
const scopes = { tasks: t('Tasks only', 'タスクのみ'), calendar: t('Calendars only', '予定のみ'), all: t('Calendars and tasks', '予定とタスク') };
for (const [value, text] of Object.entries(scopes)) { const option = el('option', text); option.value = value; option.selected = value === (calendar ? 'calendar' : 'tasks'); scope.append(option); }
const days = el('select'); days.ariaLabel = t('Validity', '有効期間');
for (const n of [30, 90, 365]) { const option = el('option', n + t(' days', '日間')); option.value = String(n); option.selected = n === 90; days.append(option); }
const issue = el('button', t('Issue password', 'パスワードを発行')); issue.type = 'submit'; form.append(label, scope, access, days, issue);
const compatLabel = el('label'); compatLabel.className = 'dav-compat-toggle';
const compat = el('input'); compat.type = 'checkbox'; compat.id = 'tasks-dav-compat'; compat.setAttribute('aria-describedby', 'tasks-dav-compat-help');
compatLabel.append(compat, document.createTextNode(t('OneTask compatibility (calendar-access advertisement)', 'OneTask互換モード(calendar-accessを宣言)')));
const compatHelp = el('p', t('Opt in only for clients that require calendar-access. This is a limited implementation, not full RFC compliance. Task dates are date-only; recurrence, timezone definitions and alarms remain unsupported. Enabling this does not grant additional access.', 'calendar-accessを要求するクライアント向けの互換設定です。完全なRFC準拠ではありません。タスクの日付は日単位のみで、繰り返し・タイムゾーン定義・アラームは引き続き未対応です。アクセス権限は増えません。'));
compatHelp.id = 'tasks-dav-compat-help'; compatHelp.className = 'dav-compat-help'; form.append(compatLabel, compatHelp);
const secret = el('section'); secret.hidden = true;
const status = el('p'); status.role = 'status';
const tableWrap = el('div'); tableWrap.className = 'task-dav-table'; const table = el('table'); tableWrap.append(table);
const note = el('p', t('Clients: chroncal, vdirsyncer + Todoman. Task start/due accept dates only. Recurring events and alarms cannot be created via DAV. Calendar events are deleted; tasks are archived.', '連携例:chroncal、vdirsyncer + Todoman。タスクの開始・期限は日付のみ対応。DAV経由の繰り返し予定・アラームの新規作成は未対応です。削除操作では予定は削除、タスクはアーカイブされます。'));
dialog.append(header, help, url, form, secret, status, tableWrap, note); app.append(dialog);
let busy = false, generation = 0;
async function request(path = '', options = {}) {
const res = await fetch(api + path, { credentials: 'same-origin', ...options, headers: { 'Content-Type': 'application/json', ...options.headers } });
const data = await res.json().catch(() => ({}));
if (!res.ok || res.redirected) throw new Error(t('Request failed: ', '処理に失敗しました:') + (data.error || res.status));
return data;
}
async function load() {
const ticket = ++generation; const data = await request(); if (ticket !== generation || !dialog.open) return;
table.replaceChildren(); const head = el('tr');
[t('Name', '名称'), t('Scope', '対象'), t('Access', '権限'), t('Expires', '有効期限'), ''].forEach(v => head.append(el('th', v))); const thead = el('thead'); thead.append(head); table.append(thead); const body = el('tbody'); table.append(body);
for (const c of data.items) {
const row = el('tr'); row.append(el('td', c.label + (c.calendar_access_compat ? t(' · OneTask compatibility', ' · OneTask互換') : '')), el('td', scopes[c.scope || 'tasks']), el('td', c.write ? t('Read / write', '読み書き') : t('Read only', '読み取り専用')), el('td', new Date(c.expires_ms).toLocaleString()));
const cell = el('td'), revoke = el('button', t('Revoke', '失効')); revoke.type = 'button';
revoke.onclick = async () => { if (busy || !confirm(t('Revoke this password?', 'このパスワードを失効させますか?'))) return; busy = true; revoke.disabled = true; status.textContent = ''; try { await request('/' + encodeURIComponent(c.id), { method: 'DELETE' }); await load(); } catch (e) { status.textContent = e.message; } finally { busy = false; revoke.disabled = false; } };
cell.append(revoke); row.append(cell); body.append(row);
}
}
function hide() { if (busy) return; ++generation; dialog.close(); secret.replaceChildren(); secret.hidden = true; }
close.onclick = hide; dialog.addEventListener('cancel', e => { e.preventDefault(); hide(); });
button.onclick = async () => { if (dialog.open) return; status.textContent = ''; dialog.showModal(); try { await load(); } catch (e) { status.textContent = e.message; } };
form.onsubmit = async e => {
e.preventDefault(); if (busy) return; busy = true; issue.disabled = true; status.textContent = '';
try {
const data = await request('', { method: 'POST', body: JSON.stringify({ label: label.value, scope: scope.value, write: access.value === 'write', days: Number(days.value), calendar_access_compat: compat.checked }) });
secret.replaceChildren(el('p', t('Shown only once. Save both values in your password manager before closing.', 'この画面を閉じると再表示できません。両方をパスワードマネージャーなどに保存してください。')));
for (const [name, value] of [[t('Username (credential ID)', 'ユーザー名(認証情報ID)'), data.username], [t('Password', 'パスワード'), data.password]]) { const wrapper = el('label', name), field = el('input'); field.readOnly = true; field.value = value; field.autocomplete = 'off'; field.spellcheck = false; field.onclick = () => field.select(); wrapper.append(field); secret.append(wrapper); }
secret.hidden = false; label.value = ''; await load();
} catch (e) { status.textContent = e.message; } finally { busy = false; issue.disabled = false; }
};
})();